# --------------------------------------------------------------------------- # morans_I_plus_general_G_variable_distance_better_flow_control.py # Created on: Sat Feb 25 2006 09:29:17 AM # (generated by ArcGIS/ModelBuilder) # --------------------------------------------------------------------------- ## This version is actually running variable fixed distance in increments of 1 km. # Import system modules import sys, string, os, win32com.client # Create the Geoprocessor object gp = win32com.client.Dispatch("esriGeoprocessing.GpDispatch.1") # Load required toolboxes... gp.AddToolbox("C:/Program Files/ArcGIS/ArcToolbox/Toolboxes/Spatial Statistics Tools.tbx") weed = ["mAGRRE","mAGSSTAGST","mAMAPOAMAR","mANTCO","mAVESAAVEF","mBROMUSspp","mCAPBP","mCatsearli","mCIRAR","mCONAR","mDACGL_log"] # 0 to 10 weed += ["mDAUCA","mECNOR","mEQUAR","mFESAR_log","mGALAP","mHOLLA","mHOLMO","mKICEL","mLACSE","mLOLMU_log","mLOLPE_log","mLOWLNDCUD"] # 11 to 22 weed += ["mMATMT","mPOAAN_log","mPOAPR","mPOATR","mPOLPE","mRUBDI","mRUMOBRUMC","mSENVU","mSINAR","mSONAR","mTYPAR","mVLPMY","mWheat_log"] # 23 to 35 weed += ["wAGSSTAGST","wDACGL","wFESAR","wLOLMU","wLOLPE","wPOAPR","wPOATR"] #36 to 42 # started out with only a few estimates of likely values for the peak autocorrelation and maximum range. # This batch file was only run using the original 0,1,2,3,4 weed severity conversion scale, not the newer one. init_dist_peak_MI_prob = [9.0,1.0,0.0,0.0,2.5,2.0,0.0,0.0,2.5,4.0,0.0,0.0,0.0,0.0,0.0,0.0,21.0,8.0,0.0,0.0,8.0,0.0,0.0,0.0,9.0,0.0,4.0,0.0,0.0,0.0] # 0 to 29 init_dist_peak_MI_prob += [0.0,0.0,0.0,0.0,0.0,0.0,1.0,0.0,0.0,9.0,0.0,0.0,4.0] # 30 to 42 - values are in miles init_dist_lose_MI_prob = [31.0,25.0,0.0,0.0,25.0,26.0,0.0,0.0,28.0,20.0,0.0,0.0,0.0,0.0,0.0,0.0,50.0,47.0,0.0,0.0,26.0,0.0,0.0,0.0,28.0,0.0,20.0,0.0,0.0,0.0] # 0 to 29 init_dist_lose_MI_prob += [0.0,0.0,0.0,0.0,0.0,0.0,25.0,0.0,0.0,23.0,0.0,0.0,20.0] # 30 to 42 - values are in miles LinnBoundaries = "C:\\DataFiles\\OSUSeedCert\\SpatialStats\\LinnBoundaries.shp" gp_count = 0 summary_file_name = "C:\\DataFiles\\OSUSeedCert\\SpatialStats\\MI_distance_summary.txt" summary_file = open(summary_file_name, "w") # write the observed peak range and outer bound of significance range to a file - will rewrite existing file if opened in "w" mode GGsummary_file_name = "C:\\DataFiles\\OSUSeedCert\\SpatialStats\\GG_distance_summary.txt" GGsummary_file = open(GGsummary_file_name, "w") # write the observed peak range and outer bound of significance range to a file - will rewrite existing file if opened in "w" mode # algorithm to find distance of maximum MI Z-score first_try = 0 using_defaults = 0 number_done = 0 # need to increment this value in future restarts of program for weed_num in range(number_done,43): # setting this to 1 for now, but does eventually need to be 43 test_distance = init_dist_peak_MI_prob[weed_num] lose_sig_test_dist = init_dist_lose_MI_prob[weed_num] if test_distance == 0.0: test_distance = 6.67 # average of 12 already nearly done first_try = 1 using_defaults = 1 test_distance *= 1.609344 # convert into kilometers test_distance = int(test_distance) # convert to integers in kilometers for use in dictionary if lose_sig_test_dist == 0.0: lose_sig_test_dist = 29.08 # of 12 already partially done lose_sig_test_dist *= 1.609344 # convert into kilometers lose_sig_test_dist = int(lose_sig_test_dist) # convert to integers in kilometers for use in dictionary weed_name = weed[weed_num] MI_results_name = "C:\\DataFiles\\OSUSeedCert\\SpatialStats\\MI_"+weed_name+"_variable_distance_results_for_max_Z_score.txt" MI_results_file = open(MI_results_name,"a+") print MI_results_name+" "+weed_name print "lose sig test dist from program data list = "+str(lose_sig_test_dist) peak_reached = 0 floor_reached = 0 Z_scores_observed = {-1:0.0,0:0.0} # dummy values of Z = 0,0 for distance of influence = -1,0 ## -1 needed to give full range around possible solution of 1000 m # now load any existing data from previously written file already_made = MI_results_file.readline() max_Z = 0 temp_peak = 0 while (len(already_made) > 0) and (already_made != "\n"): # skip this if nothing is in file #convert line into dictionary entry space1 = already_made.find(" ") space2 = already_made.find(" ",space1 + 1) space3 = already_made.find(" ",space2 + 1) space4 = already_made.find(" ",space3 + 1) final_separator = already_made.find("; ",space4 + 1) existing_distance = int(already_made[space1 + 1:space2]) existing_zscore = float(already_made[final_separator + 2:]) if existing_zscore < 0: existing_zscore = 0 Z_scores_observed[existing_distance] = existing_zscore # print str(existing_distance)+" "+str(existing_zscore) already_made = MI_results_file.readline() # keep reading in more lines until while loop stops first_try = 0 # have some results in stored text file if existing_zscore > max_Z: max_Z = existing_zscore temp_peak = existing_distance print Z_scores_observed print str(max_Z)+" = max Z-score in text file "+str(temp_peak)+" = distance in km" MI_results_file.close() MI_results_file = open(MI_results_name,"a") if temp_peak > 0: if test_distance == 10: # change to better value if we've already run program for this weed and written data to text file test_distance = temp_peak if test_distance != 10: print "not changing test distance from "+str(test_distance)+" km in local data to "+str(temp_peak)+" in results stored in text file" # leave test_distance alone unless we add more code while peak_reached == 0: for this_shift in range(-2,3): # plus and minus 1 and 2 km from test_distance except during first_try on new weed if first_try == 0: shifting = this_shift else: shifting = (2 + this_shift) * 10 # generate wide range 10,20,30,40,50 km if no preliminary search for peak local_distance = test_distance + shifting print "local distance value is "+str(local_distance) if local_distance <= 0: local_distance = 1 # do not run at less than 1000 m range if not Z_scores_observed.has_key(local_distance): # only run if this distance hasn't already been done # Local variables... Index = "0" ZScore = "0" MI_results_file.write(weed_name+" "+str(local_distance)+" in kilometers ") param_distance = 3280.839895 * local_distance # conversion factor is feet in a kilometer # Process: Spatial Autocorrelation (Morans I)... curr_results = gp.SpatialAutocorrelation_stats(LinnBoundaries, weed_name, "false", "Fixed Distance Band", "Euclidean Distance", "None", param_distance, "") gp_count +=1 MI_results_file.writelines(curr_results) MI_results_file.write("\n") MI_results_file.flush() myZ = float(curr_results[curr_results.find(";")+1:]) # print " Z-score = "+str(myZ)+" for distance in km of "+str(local_distance) if myZ < 0: myZ = 0 # do need keep negative Z-scores as they make no sense in our context and might break the search procedure for maximum Z-score Z_scores_observed[local_distance] = myZ values_so_far = Z_scores_observed.values() max_value = max(values_so_far) distances_so_far = Z_scores_observed.keys() peak = 0 for where_is_peak in distances_so_far: if Z_scores_observed[where_is_peak] == max_value: peak = where_is_peak # this logic will get last value of several duplicate maximum values, but duplicates are very unlikely!!! contiguity = 1 for testing_contiguity in range(-2,3): # testing whether data exists in a contiguous range of -2,-1,+1,+2 around observed peak this_distance = peak + testing_contiguity if not Z_scores_observed.has_key(this_distance): contiguity = 0 if contiguity == 1: peak_reached = 1 else: # still haven't found the peak, need to keep moving up or down in distance test_distance = peak first_try = 0 print "new test distance in kilometers = "+str(test_distance) print "peak in MI Z-scores found at "+str(peak)+" with Z-value = "+str(Z_scores_observed[peak]) summary_file.write(weed_name+" peak MI distance = "+str(peak)+" kilometers with Z-score = "+str(Z_scores_observed[peak])+"\n") summary_file.write(str(gp_count)+" gp calls this run."+"\n") summary_file.flush() # now trying to find distance for minimum Z-score > 1.96 with adjacent larger distances non-significant and shorter one more significant print "now running the find distance at which significance is first lost algorithm" new_min = Z_scores_observed[peak] # start new_min as high as any Z-score calculated so far if new_min < 1.96: print "warning, will robinson, danger!!!! - maximum observed Z-score was less than that required for 5% significance level" floor_reached = 0 # forgot to give this a default value shortest_found = 0 shortest_ns = max(distances_so_far) # a safe default value! if shortest_ns < lose_sig_test_dist: shortest_ns = lose_sig_test_dist # fixes case of peak search not loading dictionary with distance as large as initial estimate of lose_sig_test_dist if (lose_sig_test_dist == 46) and (using_defaults == 1): # the default value loaded for the no-data case - resetting to better behaved starting values lose_sig_test_dist -= 2 # drop distance slightly from tentative loss of significance distance distances_so_far = Z_scores_observed.keys() distances_so_far.sort() for where_is_first_floor in distances_so_far: if (Z_scores_observed[where_is_first_floor] < 1.96) and (where_is_first_floor > 0) and (shortest_found == 0)\ and (where_is_first_floor > peak): shortest_ns = where_is_first_floor shortest_found = 1 # this code finds the first distance dropping below significance if one already exists in data lose_sig_test_dist = shortest_ns - 2 # shift back to try to catch passage from signif. to NS #if Z_scores_observed[where_is_first_floor] <= 0: #lose_sig_test_dist = shortest_ns - 2 # shift back even further if Z-score was below zero! if (shortest_found == 0) and (lose_sig_test_dist == 44) and (using_defaults == 1) and (lose_sig_test_dist != (peak + 5)): lose_sig_test_dist = peak + 5 # NS values were not present and using default average for initial distance target floor = shortest_ns # new control variable old_floor = floor stuck = 0 escape_count = 0 while floor_reached == 0: escape_count += 1 if escape_count > 200: print "trouble at line 141" break for this_shift in range(-2,3): # plus and minus 1 and 2 km from lose_sig_test_dist escape_count += 1 if escape_count > 200: print "trouble at line 143" break local_distance = lose_sig_test_dist + this_shift print "local distance value is "+str(local_distance) if local_distance <= 0: local_distance = 1 # do not run at less than 1000 m range if not Z_scores_observed.has_key(local_distance): # only run if this distance hasn't already been done print "in the floor reach -2,-1,0,1,2 loop" # Local variables... Index = "0" ZScore = "0" MI_results_file.write(weed_name+" "+str(local_distance)+" in kilometers ") param_distance = 3280.839895 * local_distance # conversion factor is feet in a kilometer # Process: Spatial Autocorrelation (Morans I)... curr_results = gp.SpatialAutocorrelation_stats(LinnBoundaries, weed_name, "false", "Fixed Distance Band", "Euclidean Distance", "None", param_distance, "") gp_count += 1 MI_results_file.writelines(curr_results) MI_results_file.write("\n") MI_results_file.flush() myZ = float(curr_results[curr_results.find(";")+1:]) # print " Z-score = "+str(myZ)+" for distance in km of "+str(local_distance) if myZ < 0: myZ = 0 # do need keep negative Z-scores as they make no sense in our context and might break the search procedure for maximum Z-score Z_scores_observed[local_distance] = myZ values_so_far = Z_scores_observed.values() print values_so_far # dubugging printout old_min = new_min #for temp_Z in values_so_far: # need to track non-zero minimums # need to limit next test to just cases where distance > peak # if (temp_Z < new_min) and (temp_Z >= 1.96): # using loss of 5% significance level for floor in finding maximum range of spatial autocorrelation # new_min = temp_Z ############################# status condition ######################## distances_so_far = Z_scores_observed.keys() testing_ns = max(distances_so_far) consecutive_peak_to_curr_count = 0 peak_to_curr_gt196_count = 0 contiguity_peak_to_curr = 1 two_in_a_row_Z_lt_196 = 0 pos_first_ns = testing_ns + 1 # safe default value pos_two_in_a_row = 0 # safe default least_dist_past_peak_Z_lt_196 = 0 # safe default distances_so_far.sort() for test_contiguity in range(peak,lose_sig_test_dist+1): if not(Z_scores_observed.has_key(test_contiguity)): contiguity_peak_to_curr = 0 # contiguity does not yet exist in range peak to lose_sig_test_dist for where_is in distances_so_far: # loop thru distances in order, focusing on conditions past the peak if Z_scores_observed.has_key(where_is + 1) and Z_scores_observed.has_key(where_is + 2): # verify data exists at next two step on out if (Z_scores_observed[where_is] > 1.96) and (Z_scores_observed[where_is + 1] < 1.96) and (Z_scores_observed[where_is + 2] < 1.96) and (floor_reached == 0) and (where_is >= peak): floor_reached = 1 floor = where_is # this completes things that needed to be done once floor location has been found if (where_is > peak) and (where_is <= lose_sig_test_dist): consecutive_peak_to_curr_count += 1 if (Z_scores_observed[where_is] > 1.96): peak_to_curr_gt196_count += 1 if (Z_scores_observed[where_is] < 1.96) and (two_in_a_row_Z_lt_196 == 1) and (where_is == pos_first_ns + 1): two_in_a_row_Z_lt_196 = 2 pos_two_in_a_row = where_is # this is trigger for Case 5 else: # reset count of two in a row NS Z-scores to 0 if (two_in_a_row_Z_lt_196 == 1): two_in_a_row_Z_lt_196 = 0 if (Z_scores_observed[where_is] < 1.96) and (two_in_a_row_Z_lt_196 == 0): two_in_a_row_Z_lt_196 = 1 pos_first_ns = where_is if (Z_scores_observed[where_is] < 1.96) and (least_dist_past_peak_Z_lt_196 == 0) and (where_is > peak): least_dist_past_peak_Z_lt_196 = where_is ###################### new improved case control flow method ############################### # case 1 – floor has been found where Z(floor) > 1.96 and Z(floor + 1 km) and Z(floor + 2 km) are both non-significant if floor_reached == 1: lose_sig_dist_test = floor # value of floor was set earlier when test status loop determined that floor had been reached print "maximum range of significance has been found at distance in km = "+str(floor) # case 2 – current lose_sig_dist_test value is way below likely final value as all Z-scores are > 10 elif (consecutive_peak_to_curr_count == peak_to_curr_gt196_count) and (Z_scores_observed[lose_sig_test_dist] > 10): lose_sig_test_dist += 12 # big jump out of 12 km from previous target # case 3 – current lose_sig_dist_test value is below likely final value elif (consecutive_peak_to_curr_count == peak_to_curr_gt196_count): lose_sig_test_dist += 5 # case 4 – contiguous dataset from peak to current lose_sig_test_dist value already exists elif (contiguity_peak_to_curr == 1): lose_sig_test_dist += 5 # case 5 – two values in range peak to curr position have Z-scores less than 1.96 elif (two_in_a_row_Z_lt_196 == 2): lose_sig_test_dist -= 5 # drop back down to fill in non-contiguous values # case 6 – should never hit this condition if coding is correct elif (pos_two_in_a_row > lose_sig_test_dist): lose_sig_test_dist += 5 # case 7 – aware of some distance past current test position where Z is non-significant elif (least_dist_past_peak_Z_lt_196 > lose_sig_test_dist): lose_sig_test_dist += 5 # case 8 – aware of some distance short of current test position where Z is non-significant elif (least_dist_past_peak_Z_lt_196 < lose_sig_test_dist): lose_sig_test_dist -= 5 # all other possibilities seem remote!!! else: print "how did we ever reach this spot? " print "done now with weed "+weed_name if escape_count > 200: print "trouble at line 196" print escape_count print "done fini kaput" print "maximum range of MI sigificance found at "+str(floor)+" with Z-value = "+str(Z_scores_observed[floor]) summary_file.write(weed_name+" maximum range of MI significance = "+str(floor)+" kilometers with Z-score = "+str(Z_scores_observed[floor])+"\n") # summary_file.write(weed_name+" shortest non-signif. MI distance = "+str(shortest_ns)+" kilometers with Z-score = "+str(Z_scores_observed[shortest_ns])+"\n") summary_file.write(str(gp_count)+" gp calls this run. "+str(escape_count)+" = escape_count value"+"\n") summary_file.flush() MI_results_file.close() ############# here we try Getis-Ord General G instead of Morans I # algorithm to find distance of maximum GG Z-score first_try = 0 # do we ever want to use wider test range of first try = 1? test_distance = peak # use MI peak distance as initial GG target lose_sig_test_dist = floor # use final max sig distance from MI as GG target GG_results_name = "C:\\DataFiles\\OSUSeedCert\\SpatialStats\\GG_"+weed_name+"_variable_distance_results_for_max_Z_score.txt" GG_results_file = open(GG_results_name,"a+") print GG_results_name+" "+weed_name print "GG target lose sig test dist from MI floor = "+str(lose_sig_test_dist) peak_reached = 0 floor_reached = 0 Z_scores_observed = {-1:0.0,0:0.0} # dummy values of Z = 0,0 for distance of influence = -1,0 ## -1 needed to give full range around possible solution of 1000 m # now load any existing data from previously written file already_made = GG_results_file.readline() max_Z = 0 temp_peak = 0 while (len(already_made) > 0) and (already_made != "\n"): # skip this if nothing is in file #convert line into dictionary entry space1 = already_made.find(" ") space2 = already_made.find(" ",space1 + 1) space3 = already_made.find(" ",space2 + 1) space4 = already_made.find(" ",space3 + 1) final_separator = already_made.find("; ",space4 + 1) existing_distance = int(already_made[space1 + 1:space2]) existing_zscore = float(already_made[final_separator + 2:]) if existing_zscore < 0: existing_zscore = 0 Z_scores_observed[existing_distance] = existing_zscore # print str(existing_distance)+" "+str(existing_zscore) already_made = GG_results_file.readline() # keep reading in more lines until while loop stops first_try = 0 # have some results in stored text file if existing_zscore > max_Z: max_Z = existing_zscore temp_peak = existing_distance print Z_scores_observed print str(max_Z)+" = max Z-score in text file "+str(temp_peak)+" = distance in km" GG_results_file.close() GG_results_file = open(GG_results_name,"a") # if temp_peak > 0: # if test_distance == 10: # change to better value if we've already run program for this weed and written data to text file # test_distance = temp_peak # if test_distance != 10: # print "not changing test distance from "+str(test_distance)+" km in local data to "+str(temp_peak)+" in results stored in text file" # # leave test_distance alone unless we add more code while peak_reached == 0: for this_shift in range(-3,4): # plus and minus 1, 2, and 3 km from test_distance except during first_try on new weed if first_try == 0: shifting = this_shift else: shifting = (2 + this_shift) * 10 # generate wide range 10,20,30,40,50 km if no preliminary search for peak local_distance = test_distance + shifting print "local distance value is "+str(local_distance) if local_distance <= 0: local_distance = 1 # do not run at less than 1000 m range if not Z_scores_observed.has_key(local_distance): # only run if this distance hasn't already been done # Local variables... Observed_General_G = "0" ZScore = "0" GG_results_file.write(weed_name+" "+str(local_distance)+" in kilometers ") param_distance = 3280.839895 * local_distance # conversion factor is feet in a kilometer # Process: High/Low Clustering (General G)... curr_results = gp.HighLowClustering_stats(LinnBoundaries, weed_name, "false", "Fixed Distance Band", "Euclidean Distance", "None", param_distance, "") gp_count +=1 GG_results_file.writelines(curr_results) GG_results_file.write("\n") GG_results_file.flush() myZ = float(curr_results[curr_results.find(";")+1:]) if myZ < 0: myZ = 0 # do need keep negative Z-scores as they make no sense in our context and might break the search procedure for maximum Z-score Z_scores_observed[local_distance] = myZ values_so_far = Z_scores_observed.values() max_value = max(values_so_far) distances_so_far = Z_scores_observed.keys() peak = 0 for where_is_peak in distances_so_far: if Z_scores_observed[where_is_peak] == max_value: peak = where_is_peak # this logic will get last value of several duplicate maximum values, but duplicates are very unlikely!!! contiguity = 1 for testing_contiguity in range(-2,3): # testing whether data exists in a contiguous range of -2,-1,+1,+2 around observed peak this_distance = peak + testing_contiguity if not Z_scores_observed.has_key(this_distance): contiguity = 0 if contiguity == 1: peak_reached = 1 else: # still haven't found the peak, need to keep moving up or down in distance test_distance = peak first_try = 0 # had been set to 0 before start of Gen G peak test anyway print "new test distance in kilometers = "+str(test_distance) print "peak in GG Z-scores found at "+str(peak)+" with Z-value = "+str(Z_scores_observed[peak]) GGsummary_file.write(weed_name+" peak GG distance = "+str(peak)+" kilometers with Z-score = "+str(Z_scores_observed[peak])+"\n") GGsummary_file.write(str(gp_count)+" gp calls this run."+"\n") GGsummary_file.flush() # now trying to find distance for minimum Z-score > 1.96 with adjacent larger distances non-significant and shorter one more significant print "now running the find distance at which significance is first lost algorithm" new_min = Z_scores_observed[peak] # start new_min as high as any Z-score calculated so far if new_min < 1.96: print "warning, will robinson, danger!!!! - maximum observed Z-score was less than that required for 5% significance level" floor_reached = 0 # forgot to give this a default value shortest_found = 0 shortest_ns = max(distances_so_far) # a safe default value! if shortest_ns < lose_sig_test_dist: shortest_ns = lose_sig_test_dist # fixes case of peak search not loading dictionary with distance as large as initial estimate of lose_sig_test_dist old_floor = shortest_ns - 1 # new control variable stuck = 0 escape_count = 0 while floor_reached == 0: escape_count += 1 if escape_count > 200: print "trouble at line 141" break for this_shift in range(-3,4): # plus and minus 1, 2, and 3 km from lose_sig_test_dist escape_count += 1 if escape_count > 200: print "trouble at line 143" break local_distance = lose_sig_test_dist + this_shift # starts with lose_sig_test_dist = 53 for AGRRE print "local distance value is "+str(local_distance) if local_distance <= 0: local_distance = 1 # do not run at less than 1000 m range if not Z_scores_observed.has_key(local_distance): # only run if this distance hasn't already been done print "in the floor reach -3,-2,-1,0,1,2,3 loop" # Local variables... Observed_General_G = "0" ZScore = "0" GG_results_file.write(weed_name+" "+str(local_distance)+" in kilometers ") param_distance = 3280.839895 * local_distance # conversion factor is feet in a kilometer # Process: High/Low Clustering (General G)... curr_results = gp.HighLowClustering_stats(LinnBoundaries, weed_name, "false", "Fixed Distance Band", "Euclidean Distance", "None", param_distance, "") gp_count += 1 GG_results_file.writelines(curr_results) GG_results_file.write("\n") GG_results_file.flush() myZ = float(curr_results[curr_results.find(";")+1:]) if myZ < 0: myZ = 0 # do need keep negative Z-scores as they make no sense in our context and might break the search procedure for maximum Z-score Z_scores_observed[local_distance] = myZ values_so_far = Z_scores_observed.values() values_so_far.sort() print values_so_far # dubugging printout old_min = new_min #for temp_Z in values_so_far: # need to track non-zero minimums # need to limit next test to just cases where distance > peak # if (temp_Z < new_min) and (temp_Z >= 1.96): # using loss of 5% significance level for floor in finding maximum range of spatial autocorrelation # new_min = temp_Z ############################# status condition ######################## distances_so_far = Z_scores_observed.keys() testing_ns = max(distances_so_far) consecutive_peak_to_curr_count = 0 peak_to_curr_gt196_count = 0 contiguity_peak_to_curr = 1 two_in_a_row_Z_lt_196 = 0 pos_first_ns = testing_ns + 1 # safe default value pos_two_in_a_row = 0 # safe default least_dist_past_peak_Z_lt_196 = 0 # safe default distances_so_far.sort() for test_contiguity in range(peak,lose_sig_test_dist+1): if not(Z_scores_observed.has_key(test_contiguity)): contiguity_peak_to_curr = 0 # contiguity does not yet exist in range peak to lose_sig_test_dist for where_is in distances_so_far: # loop thru distances in order, focusing on conditions past the peak if Z_scores_observed.has_key(where_is + 1) and Z_scores_observed.has_key(where_is + 2): # verify data exists at next two step on out if (Z_scores_observed[where_is] > 1.96) and (Z_scores_observed[where_is + 1] < 1.96) and (Z_scores_observed[where_is + 2] < 1.96) and (floor_reached == 0) and (where_is >= peak): floor_reached = 1 floor = where_is # this completes things that needed to be done once floor location has been found if (where_is > peak) and (where_is <= lose_sig_test_dist): consecutive_peak_to_curr_count += 1 if (Z_scores_observed[where_is] > 1.96): peak_to_curr_gt196_count += 1 if (Z_scores_observed[where_is] < 1.96) and (two_in_a_row_Z_lt_196 == 1) and (where_is == pos_first_ns + 1): two_in_a_row_Z_lt_196 = 2 pos_two_in_a_row = where_is # this is trigger for Case 5 else: # reset count of two in a row NS Z-scores to 0 if (two_in_a_row_Z_lt_196 == 1): two_in_a_row_Z_lt_196 = 0 if (Z_scores_observed[where_is] < 1.96) and (two_in_a_row_Z_lt_196 == 0): two_in_a_row_Z_lt_196 = 1 pos_first_ns = where_is if (Z_scores_observed[where_is] < 1.96) and (least_dist_past_peak_Z_lt_196 == 0) and (where_is > peak): least_dist_past_peak_Z_lt_196 = where_is ###################### new improved case control flow method ############################### # case 1 – floor has been found where Z(floor) > 1.96 and Z(floor + 1 km) and Z(floor + 2 km) are both non-significant if floor_reached == 1: lose_sig_dist_test = floor # value of floor was set earlier when test status loop determined that floor had been reached print "maximum range of significance has been found at distance in km = "+str(floor) # case 2 – current lose_sig_dist_test value is way below likely final value as all Z-scores are > 10 elif (consecutive_peak_to_curr_count == peak_to_curr_gt196_count) and (Z_scores_observed[lose_sig_test_dist] > 10): lose_sig_test_dist += 12 # big jump out of 12 km from previous target # case 3 – current lose_sig_dist_test value is below likely final value elif (consecutive_peak_to_curr_count == peak_to_curr_gt196_count): lose_sig_test_dist += 5 # case 4 – contiguous dataset from peak to current lose_sig_test_dist value already exists elif (contiguity_peak_to_curr == 1): lose_sig_test_dist += 5 # case 5 – two values in range peak to curr position have Z-scores less than 1.96 elif (two_in_a_row_Z_lt_196 == 2): lose_sig_test_dist -= 5 # drop back down to fill in non-contiguous values # case 6 – should never hit this condition if coding is correct elif (pos_two_in_a_row > lose_sig_test_dist): lose_sig_test_dist += 5 # case 7 – aware of some distance past current test position where Z is non-significant elif (least_dist_past_peak_Z_lt_196 > lose_sig_test_dist): lose_sig_test_dist += 5 # case 8 – aware of some distance short of current test position where Z is non-significant elif (least_dist_past_peak_Z_lt_196 < lose_sig_test_dist): lose_sig_test_dist -= 5 # all other possibilities seem remote!!! else: print "how did we ever reach this spot? " print "done now with weed "+weed_name if escape_count > 200: print "trouble at line 196" print escape_count print "done fini kaput" print "maximum range of GG sigificance found at "+str(floor)+" with Z-value = "+str(Z_scores_observed[floor]) GGsummary_file.write(weed_name+" maximum range of GG significance = "+str(floor)+" kilometers with Z-score = "+str(Z_scores_observed[floor])+"\n") #GGsummary_file.write(weed_name+" shortest non-signif. GG distance = "+str(shortest_ns)+" kilometers with Z-score = "+str(Z_scores_observed[shortest_ns])+"\n") #GGsummary_file.write(weed_name+" true_max non-signif. GG distance = "+str(true_max)+" kilometers with Z-score = "+str(Z_scores_observed[true_max])+"\n") GGsummary_file.write(str(gp_count)+" gp calls this run. "+str(escape_count)+" = escape_count value"+"\n") GGsummary_file.flush() GG_results_file.close() summary_file.close() GGsummary_file.close()