Hi Xander Bakker I have used this script and got two rasters "correlation" and "pearson". Which one is the P-value?
Branched from https://community.esri.com/thread/200534-re-correlation-between-two-different-rasters
Hi margarita stevia ,
Could you validate if this works for you? I will create the additional p-value raster and a mask raster containing the pixels with value 1 where the condition (p-value <= 0.05 and positive correlation) is met. The calculation of the p-value is based on the code provided by Dan Patterson
It seems that the resulting areas (blue pixels) are near the rivers . Does that make sense?
See code below:
<SPAN class="keyword token">def</SPAN> <SPAN class="token function">main</SPAN><SPAN class="punctuation token">(</SPAN><SPAN class="punctuation token">)</SPAN><SPAN class="punctuation token">:</SPAN> <SPAN class="keyword token">print</SPAN> <SPAN class="string token">"load modules..."</SPAN> <SPAN class="keyword token">import</SPAN> arcpy arcpy<SPAN class="punctuation token">.</SPAN>env<SPAN class="punctuation token">.</SPAN>overwriteOutput <SPAN class="operator token">=</SPAN> <SPAN class="token boolean">True</SPAN> <SPAN class="keyword token">import</SPAN> numpy <SPAN class="keyword token">as</SPAN> np template1 <SPAN class="operator token">=</SPAN> r<SPAN class="string token">'C:\GeoNet\Pvalue\Send\NDVI\r{0}_WUE.TIF'</SPAN> template2 <SPAN class="operator token">=</SPAN> r<SPAN class="string token">'C:\GeoNet\Pvalue\Send\temp\r{0}_TEM.TIF'</SPAN> nodata <SPAN class="operator token">=</SPAN> <SPAN class="operator token">-</SPAN><SPAN class="number token">3.4028235e+38</SPAN> out_ras_corr <SPAN class="operator token">=</SPAN> r<SPAN class="string token">'C:\GeoNet\Pvalue\Send\Results\correlation_v01.TIF'</SPAN> out_ras_pear <SPAN class="operator token">=</SPAN> r<SPAN class="string token">'C:\GeoNet\Pvalue\Send\Results\pearson_v01.TIF'</SPAN> out_ras_pval <SPAN class="operator token">=</SPAN> r<SPAN class="string token">'C:\GeoNet\Pvalue\Send\Results\pvalue_v01.TIF'</SPAN> out_ras_mask <SPAN class="operator token">=</SPAN> r<SPAN class="string token">'C:\GeoNet\Pvalue\Send\Results\mask_v01.TIF'</SPAN> <SPAN class="comment token"># settings</SPAN> max_pval <SPAN class="operator token">=</SPAN> <SPAN class="number token">0.05</SPAN> min_corr <SPAN class="operator token">=</SPAN> <SPAN class="number token">0</SPAN> mask_val <SPAN class="operator token">=</SPAN> <SPAN class="number token">1</SPAN> no_mask_val <SPAN class="operator token">=</SPAN> <SPAN class="number token">0</SPAN> <SPAN class="keyword token">print</SPAN> <SPAN class="string token">"create nested numpy array list..."</SPAN> lst_np_ras <SPAN class="operator token">=</SPAN> <SPAN class="punctuation token">[</SPAN><SPAN class="punctuation token">]</SPAN> <SPAN class="keyword token">for</SPAN> i <SPAN class="keyword token">in</SPAN> range<SPAN class="punctuation token">(</SPAN><SPAN class="number token">1</SPAN><SPAN class="punctuation token">,</SPAN> <SPAN class="number token">5</SPAN><SPAN class="punctuation token">)</SPAN><SPAN class="punctuation token">:</SPAN> ras_path1 <SPAN class="operator token">=</SPAN> template1<SPAN class="punctuation token">.</SPAN>format<SPAN class="punctuation token">(</SPAN><SPAN class="string token">"%03d"</SPAN> <SPAN class="operator token">%</SPAN> <SPAN class="punctuation token">(</SPAN>i<SPAN class="punctuation token">,</SPAN><SPAN class="punctuation token">)</SPAN><SPAN class="punctuation token">)</SPAN> <SPAN class="keyword token">print</SPAN> <SPAN class="string token">" - "</SPAN><SPAN class="punctuation token">,</SPAN> ras_path1 ras_np1 <SPAN class="operator token">=</SPAN> arcpy<SPAN class="punctuation token">.</SPAN>RasterToNumPyArray<SPAN class="punctuation token">(</SPAN>ras_path1<SPAN class="punctuation token">)</SPAN> ras_path2 <SPAN class="operator token">=</SPAN> template2<SPAN class="punctuation token">.</SPAN>format<SPAN class="punctuation token">(</SPAN><SPAN class="string token">"%03d"</SPAN> <SPAN class="operator token">%</SPAN> <SPAN class="punctuation token">(</SPAN>i<SPAN class="punctuation token">,</SPAN><SPAN class="punctuation token">)</SPAN><SPAN class="punctuation token">)</SPAN> <SPAN class="keyword token">print</SPAN> <SPAN class="string token">" - "</SPAN><SPAN class="punctuation token">,</SPAN> ras_path2 ras_np2 <SPAN class="operator token">=</SPAN> arcpy<SPAN class="punctuation token">.</SPAN>RasterToNumPyArray<SPAN class="punctuation token">(</SPAN>ras_path2<SPAN class="punctuation token">)</SPAN> lst_np_ras<SPAN class="punctuation token">.</SPAN>append<SPAN class="punctuation token">(</SPAN><SPAN class="punctuation token">[</SPAN>ras_np1<SPAN class="punctuation token">,</SPAN> ras_np2<SPAN class="punctuation token">]</SPAN><SPAN class="punctuation token">)</SPAN> <SPAN class="keyword token">print</SPAN> <SPAN class="string token">"read props numpy raster..."</SPAN> ras_np <SPAN class="operator token">=</SPAN> lst_np_ras<SPAN class="punctuation token">[</SPAN><SPAN class="number token">0</SPAN><SPAN class="punctuation token">]</SPAN><SPAN class="punctuation token">[</SPAN><SPAN class="number token">0</SPAN><SPAN class="punctuation token">]</SPAN> <SPAN class="comment token"># take first numpy array from list</SPAN> rows <SPAN class="operator token">=</SPAN> ras_np<SPAN class="punctuation token">.</SPAN>shape<SPAN class="punctuation token">[</SPAN><SPAN class="number token">0</SPAN><SPAN class="punctuation token">]</SPAN> cols <SPAN class="operator token">=</SPAN> ras_np<SPAN class="punctuation token">.</SPAN>shape<SPAN class="punctuation token">[</SPAN><SPAN class="number token">1</SPAN><SPAN class="punctuation token">]</SPAN> <SPAN class="keyword token">print</SPAN> <SPAN class="string token">" - rows:"</SPAN><SPAN class="punctuation token">,</SPAN> rows <SPAN class="keyword token">print</SPAN> <SPAN class="string token">" - cols:"</SPAN><SPAN class="punctuation token">,</SPAN> cols <SPAN class="keyword token">print</SPAN> <SPAN class="string token">"create output numpy array..."</SPAN> ras_path <SPAN class="operator token">=</SPAN> template1<SPAN class="punctuation token">.</SPAN>format<SPAN class="punctuation token">(</SPAN><SPAN class="string token">"%03d"</SPAN> <SPAN class="operator token">%</SPAN> <SPAN class="punctuation token">(</SPAN><SPAN class="number token">1</SPAN><SPAN class="punctuation token">,</SPAN><SPAN class="punctuation token">)</SPAN><SPAN class="punctuation token">)</SPAN> raster <SPAN class="operator token">=</SPAN> arcpy<SPAN class="punctuation token">.</SPAN>Raster<SPAN class="punctuation token">(</SPAN>ras_path<SPAN class="punctuation token">)</SPAN> ras_np_res_corr <SPAN class="operator token">=</SPAN> np<SPAN class="punctuation token">.</SPAN>ndarray<SPAN class="punctuation token">(</SPAN><SPAN class="punctuation token">(</SPAN>rows<SPAN class="punctuation token">,</SPAN> cols<SPAN class="punctuation token">)</SPAN><SPAN class="punctuation token">)</SPAN> ras_np_res_pear <SPAN class="operator token">=</SPAN> np<SPAN class="punctuation token">.</SPAN>ndarray<SPAN class="punctuation token">(</SPAN><SPAN class="punctuation token">(</SPAN>rows<SPAN class="punctuation token">,</SPAN> cols<SPAN class="punctuation token">)</SPAN><SPAN class="punctuation token">)</SPAN> ras_np_res_pval <SPAN class="operator token">=</SPAN> np<SPAN class="punctuation token">.</SPAN>ndarray<SPAN class="punctuation token">(</SPAN><SPAN class="punctuation token">(</SPAN>rows<SPAN class="punctuation token">,</SPAN> cols<SPAN class="punctuation token">)</SPAN><SPAN class="punctuation token">)</SPAN> ras_np_res_mask <SPAN class="operator token">=</SPAN> np<SPAN class="punctuation token">.</SPAN>ndarray<SPAN class="punctuation token">(</SPAN><SPAN class="punctuation token">(</SPAN>rows<SPAN class="punctuation token">,</SPAN> cols<SPAN class="punctuation token">)</SPAN><SPAN class="punctuation token">)</SPAN> <SPAN class="keyword token">print</SPAN> <SPAN class="string token">" - out rows:"</SPAN><SPAN class="punctuation token">,</SPAN> ras_np_res_corr<SPAN class="punctuation token">.</SPAN>shape<SPAN class="punctuation token">[</SPAN><SPAN class="number token">0</SPAN><SPAN class="punctuation token">]</SPAN> <SPAN class="keyword token">print</SPAN> <SPAN class="string token">" - out cols:"</SPAN><SPAN class="punctuation token">,</SPAN> ras_np_res_corr<SPAN class="punctuation token">.</SPAN>shape<SPAN class="punctuation token">[</SPAN><SPAN class="number token">1</SPAN><SPAN class="punctuation token">]</SPAN> <SPAN class="keyword token">print</SPAN> <SPAN class="string token">"loop through pixels..."</SPAN> pix_cnt <SPAN class="operator token">=</SPAN> <SPAN class="number token">0</SPAN> <SPAN class="keyword token">for</SPAN> row <SPAN class="keyword token">in</SPAN> range<SPAN class="punctuation token">(</SPAN>rows<SPAN class="punctuation token">)</SPAN><SPAN class="punctuation token">:</SPAN> <SPAN class="keyword token">for</SPAN> col <SPAN class="keyword token">in</SPAN> range<SPAN class="punctuation token">(</SPAN>cols<SPAN class="punctuation token">)</SPAN><SPAN class="punctuation token">:</SPAN> pix_cnt <SPAN class="operator token">+=</SPAN> <SPAN class="number token">1</SPAN> <SPAN class="keyword token">if</SPAN> pix_cnt <SPAN class="operator token">%</SPAN> <SPAN class="number token">5000</SPAN> <SPAN class="operator token">==</SPAN> <SPAN class="number token">0</SPAN><SPAN class="punctuation token">:</SPAN> <SPAN class="keyword token">print</SPAN> <SPAN class="string token">" - row:"</SPAN><SPAN class="punctuation token">,</SPAN> row<SPAN class="punctuation token">,</SPAN> <SPAN class="string token">" col:"</SPAN><SPAN class="punctuation token">,</SPAN> col<SPAN class="punctuation token">,</SPAN> <SPAN class="string token">" pixel:"</SPAN><SPAN class="punctuation token">,</SPAN> pix_cnt lst_vals1 <SPAN class="operator token">=</SPAN> <SPAN class="punctuation token">[</SPAN><SPAN class="punctuation token">]</SPAN> lst_vals2 <SPAN class="operator token">=</SPAN> <SPAN class="punctuation token">[</SPAN><SPAN class="punctuation token">]</SPAN> <SPAN class="keyword token">try</SPAN><SPAN class="punctuation token">:</SPAN> <SPAN class="keyword token">for</SPAN> lst_pars <SPAN class="keyword token">in</SPAN> lst_np_ras<SPAN class="punctuation token">:</SPAN> lst_vals1<SPAN class="punctuation token">.</SPAN>append<SPAN class="punctuation token">(</SPAN>lst_pars<SPAN class="punctuation token">[</SPAN><SPAN class="number token">0</SPAN><SPAN class="punctuation token">]</SPAN><SPAN class="punctuation token">[</SPAN>row<SPAN class="punctuation token">,</SPAN> col<SPAN class="punctuation token">]</SPAN><SPAN class="punctuation token">)</SPAN> lst_vals2<SPAN class="punctuation token">.</SPAN>append<SPAN class="punctuation token">(</SPAN>lst_pars<SPAN class="punctuation token">[</SPAN><SPAN class="number token">1</SPAN><SPAN class="punctuation token">]</SPAN><SPAN class="punctuation token">[</SPAN>row<SPAN class="punctuation token">,</SPAN> col<SPAN class="punctuation token">]</SPAN><SPAN class="punctuation token">)</SPAN> lst_vals1 <SPAN class="operator token">=</SPAN> ReplaceNoData<SPAN class="punctuation token">(</SPAN>lst_vals1<SPAN class="punctuation token">,</SPAN> nodata<SPAN class="punctuation token">)</SPAN> lst_vals2 <SPAN class="operator token">=</SPAN> ReplaceNoData<SPAN class="punctuation token">(</SPAN>lst_vals2<SPAN class="punctuation token">,</SPAN> nodata<SPAN class="punctuation token">)</SPAN> <SPAN class="comment token"># perform calculation on list</SPAN> correlation <SPAN class="operator token">=</SPAN> CalculateCorrelation<SPAN class="punctuation token">(</SPAN>lst_vals1<SPAN class="punctuation token">,</SPAN> lst_vals2<SPAN class="punctuation token">,</SPAN> nodata<SPAN class="punctuation token">)</SPAN> ras_np_res_corr<SPAN class="punctuation token">[</SPAN>row<SPAN class="punctuation token">,</SPAN> col<SPAN class="punctuation token">]</SPAN> <SPAN class="operator token">=</SPAN> correlation pearson <SPAN class="operator token">=</SPAN> CalculatePearsons<SPAN class="punctuation token">(</SPAN>lst_vals1<SPAN class="punctuation token">,</SPAN> lst_vals2<SPAN class="punctuation token">,</SPAN> nodata<SPAN class="punctuation token">)</SPAN> ras_np_res_pear<SPAN class="punctuation token">[</SPAN>row<SPAN class="punctuation token">,</SPAN> col<SPAN class="punctuation token">]</SPAN> <SPAN class="operator token">=</SPAN> pearson pvalue <SPAN class="operator token">=</SPAN> CalculatePvalue<SPAN class="punctuation token">(</SPAN>lst_vals1<SPAN class="punctuation token">,</SPAN> lst_vals2<SPAN class="punctuation token">,</SPAN> nodata<SPAN class="punctuation token">)</SPAN> ras_np_res_pval<SPAN class="punctuation token">[</SPAN>row<SPAN class="punctuation token">,</SPAN> col<SPAN class="punctuation token">]</SPAN> <SPAN class="operator token">=</SPAN> pvalue <SPAN class="comment token"># compare correlation and p-value</SPAN> <SPAN class="keyword token">if</SPAN> pvalue <SPAN class="operator token">==</SPAN> nodata<SPAN class="punctuation token">:</SPAN> mask <SPAN class="operator token">=</SPAN> nodata <SPAN class="keyword token">elif</SPAN> <SPAN class="punctuation token">(</SPAN>correlation <SPAN class="operator token">>=</SPAN> min_corr<SPAN class="punctuation token">)</SPAN> <SPAN class="operator token">and</SPAN> <SPAN class="punctuation token">(</SPAN>pvalue <SPAN class="operator token"><=</SPAN> max_pval<SPAN class="punctuation token">)</SPAN><SPAN class="punctuation token">:</SPAN> mask <SPAN class="operator token">=</SPAN> mask_val <SPAN class="keyword token">else</SPAN><SPAN class="punctuation token">:</SPAN> mask <SPAN class="operator token">=</SPAN> no_mask_val ras_np_res_mask<SPAN class="punctuation token">[</SPAN>row<SPAN class="punctuation token">,</SPAN> col<SPAN class="punctuation token">]</SPAN> <SPAN class="operator token">=</SPAN> mask <SPAN class="keyword token">except</SPAN> Exception <SPAN class="keyword token">as</SPAN> e<SPAN class="punctuation token">:</SPAN> <SPAN class="keyword token">print</SPAN> <SPAN class="string token">"ERR:"</SPAN><SPAN class="punctuation token">,</SPAN> e <SPAN class="keyword token">print</SPAN> <SPAN class="string token">" - row:"</SPAN><SPAN class="punctuation token">,</SPAN> row<SPAN class="punctuation token">,</SPAN> <SPAN class="string token">" col:"</SPAN><SPAN class="punctuation token">,</SPAN> col<SPAN class="punctuation token">,</SPAN> <SPAN class="string token">" pixel:"</SPAN><SPAN class="punctuation token">,</SPAN> pix_cnt <SPAN class="keyword token">print</SPAN> <SPAN class="string token">" - lst_vals1:"</SPAN><SPAN class="punctuation token">,</SPAN> lst_vals1 <SPAN class="keyword token">print</SPAN> <SPAN class="string token">" - lst_vals2:"</SPAN><SPAN class="punctuation token">,</SPAN> lst_vals2 pnt <SPAN class="operator token">=</SPAN> arcpy<SPAN class="punctuation token">.</SPAN>Point<SPAN class="punctuation token">(</SPAN>raster<SPAN class="punctuation token">.</SPAN>extent<SPAN class="punctuation token">.</SPAN>XMin<SPAN class="punctuation token">,</SPAN> raster<SPAN class="punctuation token">.</SPAN>extent<SPAN class="punctuation token">.</SPAN>YMin<SPAN class="punctuation token">)</SPAN> <SPAN class="comment token"># - raster.meanCellHeight</SPAN> xcellsize <SPAN class="operator token">=</SPAN> raster<SPAN class="punctuation token">.</SPAN>meanCellWidth ycellsize <SPAN class="operator token">=</SPAN> raster<SPAN class="punctuation token">.</SPAN>meanCellHeight <SPAN class="keyword token">print</SPAN> <SPAN class="string token">"Write output rasters..."</SPAN> <SPAN class="keyword token">print</SPAN> <SPAN class="string token">" - "</SPAN><SPAN class="punctuation token">,</SPAN> out_ras_corr ras_res_corr <SPAN class="operator token">=</SPAN> arcpy<SPAN class="punctuation token">.</SPAN>NumPyArrayToRaster<SPAN class="punctuation token">(</SPAN>ras_np_res_corr<SPAN class="punctuation token">,</SPAN> lower_left_corner<SPAN class="operator token">=</SPAN>pnt<SPAN class="punctuation token">,</SPAN> x_cell_size<SPAN class="operator token">=</SPAN>xcellsize<SPAN class="punctuation token">,</SPAN> y_cell_size<SPAN class="operator token">=</SPAN>ycellsize<SPAN class="punctuation token">,</SPAN> value_to_nodata<SPAN class="operator token">=</SPAN>nodata<SPAN class="punctuation token">)</SPAN> arcpy<SPAN class="punctuation token">.</SPAN>CopyRaster_management<SPAN class="punctuation token">(</SPAN>ras_res_corr<SPAN class="punctuation token">,</SPAN> out_ras_corr<SPAN class="punctuation token">)</SPAN> arcpy<SPAN class="punctuation token">.</SPAN>DefineProjection_management<SPAN class="punctuation token">(</SPAN>in_dataset<SPAN class="operator token">=</SPAN>out_ras_corr<SPAN class="punctuation token">,</SPAN> coor_system<SPAN class="operator token">=</SPAN><SPAN class="string token">"GEOGCS['GCS_WGS_1984',DATUM['D_WGS_1984',SPHEROID['WGS_1984',6378137.0,298.257223563]],PRIMEM['Greenwich',0.0],UNIT['Degree',0.0174532925199433]]"</SPAN><SPAN class="punctuation token">)</SPAN> <SPAN class="keyword token">print</SPAN> <SPAN class="string token">" - "</SPAN><SPAN class="punctuation token">,</SPAN> out_ras_pear ras_res_pear <SPAN class="operator token">=</SPAN> arcpy<SPAN class="punctuation token">.</SPAN>NumPyArrayToRaster<SPAN class="punctuation token">(</SPAN>ras_np_res_pear<SPAN class="punctuation token">,</SPAN> lower_left_corner<SPAN class="operator token">=</SPAN>pnt<SPAN class="punctuation token">,</SPAN> x_cell_size<SPAN class="operator token">=</SPAN>xcellsize<SPAN class="punctuation token">,</SPAN> y_cell_size<SPAN class="operator token">=</SPAN>ycellsize<SPAN class="punctuation token">,</SPAN> value_to_nodata<SPAN class="operator token">=</SPAN>nodata<SPAN class="punctuation token">)</SPAN> arcpy<SPAN class="punctuation token">.</SPAN>CopyRaster_management<SPAN class="punctuation token">(</SPAN>ras_res_pear<SPAN class="punctuation token">,</SPAN> out_ras_pear<SPAN class="punctuation token">)</SPAN> arcpy<SPAN class="punctuation token">.</SPAN>DefineProjection_management<SPAN class="punctuation token">(</SPAN>in_dataset<SPAN class="operator token">=</SPAN>out_ras_pear<SPAN class="punctuation token">,</SPAN> coor_system<SPAN class="operator token">=</SPAN><SPAN class="string token">"GEOGCS['GCS_WGS_1984',DATUM['D_WGS_1984',SPHEROID['WGS_1984',6378137.0,298.257223563]],PRIMEM['Greenwich',0.0],UNIT['Degree',0.0174532925199433]]"</SPAN><SPAN class="punctuation token">)</SPAN> <SPAN class="keyword token">print</SPAN> <SPAN class="string token">" - "</SPAN><SPAN class="punctuation token">,</SPAN> out_ras_pval ras_res_pval <SPAN class="operator token">=</SPAN> arcpy<SPAN class="punctuation token">.</SPAN>NumPyArrayToRaster<SPAN class="punctuation token">(</SPAN>ras_np_res_pval<SPAN class="punctuation token">,</SPAN> lower_left_corner<SPAN class="operator token">=</SPAN>pnt<SPAN class="punctuation token">,</SPAN> x_cell_size<SPAN class="operator token">=</SPAN>xcellsize<SPAN class="punctuation token">,</SPAN> y_cell_size<SPAN class="operator token">=</SPAN>ycellsize<SPAN class="punctuation token">,</SPAN> value_to_nodata<SPAN class="operator token">=</SPAN>nodata<SPAN class="punctuation token">)</SPAN> arcpy<SPAN class="punctuation token">.</SPAN>CopyRaster_management<SPAN class="punctuation token">(</SPAN>ras_res_pval<SPAN class="punctuation token">,</SPAN> out_ras_pval<SPAN class="punctuation token">)</SPAN> arcpy<SPAN class="punctuation token">.</SPAN>DefineProjection_management<SPAN class="punctuation token">(</SPAN>in_dataset<SPAN class="operator token">=</SPAN>out_ras_pval<SPAN class="punctuation token">,</SPAN> coor_system<SPAN class="operator token">=</SPAN><SPAN class="string token">"GEOGCS['GCS_WGS_1984',DATUM['D_WGS_1984',SPHEROID['WGS_1984',6378137.0,298.257223563]],PRIMEM['Greenwich',0.0],UNIT['Degree',0.0174532925199433]]"</SPAN><SPAN class="punctuation token">)</SPAN> <SPAN class="keyword token">print</SPAN> <SPAN class="string token">" - "</SPAN><SPAN class="punctuation token">,</SPAN> out_ras_mask ras_res_mask <SPAN class="operator token">=</SPAN> arcpy<SPAN class="punctuation token">.</SPAN>NumPyArrayToRaster<SPAN class="punctuation token">(</SPAN>ras_np_res_mask<SPAN class="punctuation token">,</SPAN> lower_left_corner<SPAN class="operator token">=</SPAN>pnt<SPAN class="punctuation token">,</SPAN> x_cell_size<SPAN class="operator token">=</SPAN>xcellsize<SPAN class="punctuation token">,</SPAN> y_cell_size<SPAN class="operator token">=</SPAN>ycellsize<SPAN class="punctuation token">,</SPAN> value_to_nodata<SPAN class="operator token">=</SPAN>nodata<SPAN class="punctuation token">)</SPAN> arcpy<SPAN class="punctuation token">.</SPAN>CopyRaster_management<SPAN class="punctuation token">(</SPAN>ras_res_mask<SPAN class="punctuation token">,</SPAN> out_ras_mask<SPAN class="punctuation token">)</SPAN> arcpy<SPAN class="punctuation token">.</SPAN>DefineProjection_management<SPAN class="punctuation token">(</SPAN>in_dataset<SPAN class="operator token">=</SPAN>out_ras_mask<SPAN class="punctuation token">,</SPAN> coor_system<SPAN class="operator token">=</SPAN><SPAN class="string token">"GEOGCS['GCS_WGS_1984',DATUM['D_WGS_1984',SPHEROID['WGS_1984',6378137.0,298.257223563]],PRIMEM['Greenwich',0.0],UNIT['Degree',0.0174532925199433]]"</SPAN><SPAN class="punctuation token">)</SPAN> <SPAN class="keyword token">def</SPAN> <SPAN class="token function">CalculateCorrelation</SPAN><SPAN class="punctuation token">(</SPAN>a<SPAN class="punctuation token">,</SPAN> b<SPAN class="punctuation token">,</SPAN> nodata<SPAN class="punctuation token">)</SPAN><SPAN class="punctuation token">:</SPAN> <SPAN class="keyword token">import</SPAN> numpy <SPAN class="keyword token">try</SPAN><SPAN class="punctuation token">:</SPAN> coef <SPAN class="operator token">=</SPAN> numpy<SPAN class="punctuation token">.</SPAN>corrcoef<SPAN class="punctuation token">(</SPAN>a<SPAN class="punctuation token">,</SPAN>b<SPAN class="punctuation token">)</SPAN> <SPAN class="keyword token">return</SPAN> coef<SPAN class="punctuation token">[</SPAN><SPAN class="number token">0</SPAN><SPAN class="punctuation token">]</SPAN><SPAN class="punctuation token">[</SPAN><SPAN class="number token">1</SPAN><SPAN class="punctuation token">]</SPAN> <SPAN class="keyword token">except</SPAN><SPAN class="punctuation token">:</SPAN> <SPAN class="keyword token">return</SPAN> nodata <SPAN class="keyword token">def</SPAN> <SPAN class="token function">ReplaceNoData</SPAN><SPAN class="punctuation token">(</SPAN>lst<SPAN class="punctuation token">,</SPAN> nodata<SPAN class="punctuation token">)</SPAN><SPAN class="punctuation token">:</SPAN> res <SPAN class="operator token">=</SPAN> <SPAN class="punctuation token">[</SPAN><SPAN class="punctuation token">]</SPAN> <SPAN class="keyword token">for</SPAN> a <SPAN class="keyword token">in</SPAN> lst<SPAN class="punctuation token">:</SPAN> <SPAN class="keyword token">if</SPAN> a <SPAN class="operator token"><</SPAN> nodata <SPAN class="operator token">/</SPAN> <SPAN class="number token">2.0</SPAN><SPAN class="punctuation token">:</SPAN> res<SPAN class="punctuation token">.</SPAN>append<SPAN class="punctuation token">(</SPAN>None<SPAN class="punctuation token">)</SPAN> <SPAN class="keyword token">else</SPAN><SPAN class="punctuation token">:</SPAN> res<SPAN class="punctuation token">.</SPAN>append<SPAN class="punctuation token">(</SPAN>a<SPAN class="punctuation token">)</SPAN> <SPAN class="keyword token">return</SPAN> res <SPAN class="keyword token">def</SPAN> <SPAN class="token function">CalculatePearsons</SPAN><SPAN class="punctuation token">(</SPAN>a<SPAN class="punctuation token">,</SPAN> b<SPAN class="punctuation token">,</SPAN> nodata<SPAN class="punctuation token">)</SPAN><SPAN class="punctuation token">:</SPAN> <SPAN class="keyword token">import</SPAN> numpy <SPAN class="keyword token">as</SPAN> np <SPAN class="keyword token">from</SPAN> scipy<SPAN class="punctuation token">.</SPAN>stats <SPAN class="keyword token">import</SPAN> pearsonr <SPAN class="keyword token">try</SPAN><SPAN class="punctuation token">:</SPAN> x <SPAN class="operator token">=</SPAN> np<SPAN class="punctuation token">.</SPAN>array<SPAN class="punctuation token">(</SPAN>a<SPAN class="punctuation token">)</SPAN> y <SPAN class="operator token">=</SPAN> np<SPAN class="punctuation token">.</SPAN>array<SPAN class="punctuation token">(</SPAN>b<SPAN class="punctuation token">)</SPAN> r<SPAN class="punctuation token">,</SPAN> p <SPAN class="operator token">=</SPAN> pearsonr<SPAN class="punctuation token">(</SPAN>a<SPAN class="punctuation token">,</SPAN> y<SPAN class="punctuation token">)</SPAN> <SPAN class="keyword token">return</SPAN> p <SPAN class="keyword token">except</SPAN><SPAN class="punctuation token">:</SPAN> <SPAN class="keyword token">return</SPAN> nodata <SPAN class="keyword token">def</SPAN> <SPAN class="token function">CalculatePvalue</SPAN><SPAN class="punctuation token">(</SPAN>a<SPAN class="punctuation token">,</SPAN> b<SPAN class="punctuation token">,</SPAN> nodata<SPAN class="punctuation token">)</SPAN><SPAN class="punctuation token">:</SPAN> <SPAN class="keyword token">import</SPAN> numpy <SPAN class="keyword token">as</SPAN> np <SPAN class="keyword token">from</SPAN> scipy<SPAN class="punctuation token">.</SPAN>stats <SPAN class="keyword token">import</SPAN> stats <SPAN class="keyword token">try</SPAN><SPAN class="punctuation token">:</SPAN> x <SPAN class="operator token">=</SPAN> np<SPAN class="punctuation token">.</SPAN>array<SPAN class="punctuation token">(</SPAN>a<SPAN class="punctuation token">)</SPAN> y <SPAN class="operator token">=</SPAN> np<SPAN class="punctuation token">.</SPAN>array<SPAN class="punctuation token">(</SPAN>b<SPAN class="punctuation token">)</SPAN> r<SPAN class="punctuation token">,</SPAN> p_val <SPAN class="operator token">=</SPAN> stats<SPAN class="punctuation token">.</SPAN>pearsonr<SPAN class="punctuation token">(</SPAN>x<SPAN class="punctuation token">,</SPAN> y<SPAN class="punctuation token">)</SPAN> <SPAN class="keyword token">return</SPAN> p_val <SPAN class="keyword token">except</SPAN><SPAN class="punctuation token">:</SPAN> <SPAN class="keyword token">return</SPAN> nodata <SPAN class="keyword token">if</SPAN> __name__ <SPAN class="operator token">==</SPAN> <SPAN class="string token">'__main__'</SPAN><SPAN class="punctuation token">:</SPAN> main<SPAN class="punctuation token">(</SPAN><SPAN class="punctuation token">)</SPAN><SPAN class="line-numbers-rows"><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN></SPAN>
Hi margaritastevia
Can you share a sample of your data and the script you have so far?
Hi Xander Bakker please help me with how i can extract correlation values at 0.05 significance level. I'm running out of time.
I want to extract the pixels where correlation is positive and p-value is < 0.05 How i can do that? How i can estimate P as a raster?
Neither, p-value is a measure of the strength of the correlation coefficient calculated from sample size and the measured 'r' and a sampling distribution
scipy.stats.pearsonr — SciPy v1.3.0 Reference Guide
example for variants of simple arrays, using this
<SPAN class="keyword token">from</SPAN> scipy <SPAN class="keyword token">import</SPAN> stats a <SPAN class="operator token">=</SPAN> np<SPAN class="punctuation token">.</SPAN>arange<SPAN class="punctuation token">(</SPAN><SPAN class="number token">9</SPAN><SPAN class="punctuation token">)</SPAN> b <SPAN class="operator token">=</SPAN> np<SPAN class="punctuation token">.</SPAN>arange<SPAN class="punctuation token">(</SPAN><SPAN class="number token">9</SPAN><SPAN class="punctuation token">,</SPAN> <SPAN class="number token">0</SPAN><SPAN class="punctuation token">,</SPAN> <SPAN class="operator token">-</SPAN><SPAN class="number token">1</SPAN><SPAN class="punctuation token">)</SPAN> c <SPAN class="operator token">=</SPAN> np<SPAN class="punctuation token">.</SPAN>array<SPAN class="punctuation token">(</SPAN><SPAN class="punctuation token">[</SPAN><SPAN class="number token">9</SPAN><SPAN class="punctuation token">,</SPAN> <SPAN class="number token">7</SPAN><SPAN class="punctuation token">,</SPAN> <SPAN class="number token">8</SPAN><SPAN class="punctuation token">,</SPAN> <SPAN class="number token">6</SPAN><SPAN class="punctuation token">,</SPAN> <SPAN class="number token">5</SPAN><SPAN class="punctuation token">,</SPAN> <SPAN class="number token">4</SPAN><SPAN class="punctuation token">,</SPAN> <SPAN class="number token">3</SPAN><SPAN class="punctuation token">,</SPAN> <SPAN class="number token">2</SPAN><SPAN class="punctuation token">,</SPAN> <SPAN class="number token">1</SPAN><SPAN class="punctuation token">]</SPAN><SPAN class="punctuation token">)</SPAN> <SPAN class="comment token"># ---- like b, but 2 numbers switched</SPAN> r<SPAN class="punctuation token">,</SPAN> p_val <SPAN class="operator token">=</SPAN> stats<SPAN class="punctuation token">.</SPAN>pearsonr<SPAN class="punctuation token">(</SPAN>a<SPAN class="punctuation token">,</SPAN> b<SPAN class="punctuation token">)</SPAN> <SPAN class="comment token"># ---- perfect inverse, r = -1, p = 0.0000</SPAN> r<SPAN class="punctuation token">,</SPAN> p_val <SPAN class="punctuation token">(</SPAN><SPAN class="operator token">-</SPAN><SPAN class="number token">1.0</SPAN><SPAN class="punctuation token">,</SPAN> <SPAN class="number token">0.0</SPAN><SPAN class="punctuation token">)</SPAN> r<SPAN class="punctuation token">,</SPAN> p_val <SPAN class="operator token">=</SPAN> stats<SPAN class="punctuation token">.</SPAN>pearsonr<SPAN class="punctuation token">(</SPAN>a<SPAN class="punctuation token">,</SPAN> c<SPAN class="punctuation token">)</SPAN> r<SPAN class="punctuation token">,</SPAN> p_val <SPAN class="punctuation token">(</SPAN><SPAN class="operator token">-</SPAN><SPAN class="number token">0.9833333333333333</SPAN><SPAN class="punctuation token">,</SPAN> <SPAN class="number token">1.936196303745927e-06</SPAN><SPAN class="punctuation token">)</SPAN><SPAN class="line-numbers-rows"><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN></SPAN>
Les membres connectés peuvent publier, suivre les mises à jour, et plus encore. Nouveau ici ? Inscrivez-vous gratuitement.
Find useful guides, FAQs, and documents to help you navigate and make the most of Esri Community.