def update(num_growth_pix): # print(f"Num_growth_pix: {num_growth_pix}") nrows = IGrid.nrows ncols = IGrid.ncols total_pixels = nrows * ncols road_pixel_count = IGrid.get_road_pixel_count( Processing.get_current_year()) excluded_pixel_count = IGrid.get_excld_count() # Compute this year stats Stats.compute_cur_year_stats() # Set num growth pixels Stats.set_num_growth_pixels(num_growth_pix) # Calibrate growth rate Stats.cal_growth_rate() # Calibrate Percent Urban Stats.cal_percent_urban(total_pixels, road_pixel_count, excluded_pixel_count) output_dir = Scenario.get_scen_value('output_dir') cur_run = Processing.get_current_run() cur_year = Processing.get_current_year() if IGrid.test_for_urban_year(Processing.get_current_year()): Stats.cal_leesalee() filename = f"{output_dir}grow_{cur_run}_{cur_year}.log" Stats.save(filename) if Processing.get_processing_type() == Globals.mode_enum['predict']: filename = f"{output_dir}grow_{cur_run}_{cur_year}.log" Stats.save(filename)
def grow_landuse(land1, num_growth_pix): nrows = IGrid.nrows ncols = IGrid.ncols ticktock = Processing.get_current_year() landuse0_year = IGrid.igrid.get_landuse_year(0) landuse1_year = IGrid.igrid.get_landuse_year(1) urban_code = LandClass.get_urban_code() new_indices = LandClass.get_new_indices() landuse_classes = LandClass.get_landclasses() class_indices = LandClass.get_reduced_classes() background = IGrid.igrid.get_background() slope = IGrid.igrid.get_slope() deltatron = PGrid.get_deltatron() z = PGrid.get_z() land2 = PGrid.get_land2() class_slope = Transition.get_class_slope() ftransition = Transition.get_ftransition() if ticktock >= landuse0_year: # Place the New Urban Simulation into the Land Use Image Utilities.condition_gt_gif(z.gridData, 0, land1.gridData, urban_code) Deltatron.deltatron(new_indices, landuse_classes, class_indices, deltatron, land1, land2, slope, num_growth_pix, class_slope, ftransition) # Switch the old to the new for i in range(len(land2.gridData)): land1.gridData[i] = land2.gridData[i] if Processing.get_processing_type() == Globals.mode_enum['predict'] or \ (Processing.get_processing_type() == Globals.mode_enum['test'] and Processing.get_current_monte() == Processing.get_last_monte()): #Write land1 to file if IGrid.using_gif: filename = f"{Scenario.get_scen_value('output_dir')}{IGrid.igrid.location}_land_n_urban" \ f".{Processing.get_current_year()}.gif" else: filename = f"{Scenario.get_scen_value('output_dir')}{IGrid.igrid.location}_land_n_urban" \ f".{Processing.get_current_year()}.tif" IGrid.echo_meta( f"{Scenario.get_scen_value('output_dir')}{IGrid.igrid.location}_land_n_urban." f"{Processing.get_current_year()}.tfw", "landuse") date = f"{Processing.get_current_year()}" ImageIO.write_gif(land1, Color.get_landuse_table(), filename, date, nrows, ncols) # Compute final match statistics for landuse Utilities.condition_gt_gif(z.gridData, 0, land1.gridData, urban_code)
def set_base_stats(): Stats.record = Record() urban_num = IGrid.igrid.get_num_urban() slope = IGrid.igrid.get_slope().gridData for i in range(urban_num): urban = IGrid.igrid.get_urban_idx(i).gridData stats_info = StatsInfo() Stats.compute_stats(urban, slope, stats_info) road_pixel_count = IGrid.get_road_pixel_count( Processing.get_current_year()) excluded_pixel_count = IGrid.get_excld_count() percent_urban = 100.0 * 100.0 * (stats_info.pop + road_pixel_count) / \ (IGrid.nrows * IGrid.ncols - road_pixel_count - excluded_pixel_count) stats_info.percent_urban = percent_urban Stats.actual.append(stats_info)
def save(filename): Stats.record.run = Processing.get_current_run() Stats.record.monte_carlo = Processing.get_current_monte() Stats.record.year = Processing.get_current_year() index = 0 if Processing.get_processing_type() != Globals.mode_enum['predict']: index = IGrid.igrid.urban_yr_to_idx(Stats.record.year) Stats.update_running_total(index) # Now we are writing the record to file for now... if Stats.record.monte_carlo == 0: # Create file with open(filename, 'wb') as output: # Overwrites any existing file. _pickle.dump(Stats.record, output, -1) else: with open(filename, 'ab') as output: _pickle.dump(Stats.record, output, -1)
def grow(z, land1): deltatron = PGrid.get_deltatron() avg_slope = 0 if Processing.get_processing_type() == Globals.mode_enum['predict']: Processing.set_current_year( Scenario.get_scen_value('prediction_start_date')) else: Processing.set_current_year(IGrid.igrid.get_urban_year(0)) Utilities.init_grid(z.gridData) # print(z.gridData) if len(Scenario.get_scen_value('landuse_data_file')) > 0: Grow.landuse_init(deltatron.gridData, land1.gridData) seed = IGrid.igrid.get_urban_grid(0) Utilities.condition_gif(seed, z.gridData) if Scenario.get_scen_value('echo'): print("******************************************") if Processing.get_processing_type( ) == Globals.mode_enum['calibrate']: c_run = Processing.get_current_run() t_run = Processing.get_total_runs() print(f"Run = {c_run} of {t_run}" f" ({100 * c_run / t_run:8.1f} percent complete)") print( f"Monte Carlo = {int(Processing.get_current_monte()) + 1} of " f"{Scenario.get_scen_value('monte_carlo_iterations')}") print(f"Processing.current_year = {Processing.get_current_year()}") print(f"Processing.stop_year = {Processing.get_stop_year()}") if Scenario.get_scen_value('logging') and int( Scenario.get_scen_value('log_processing_status')) > 0: Grow.completion_status() while Processing.get_current_year() < Processing.get_stop_year(): # Increment Current Year Processing.increment_current_year() cur_yr = Processing.get_current_year() if Scenario.get_scen_value('echo'): print(f" {cur_yr}", end='') sys.stdout.flush() if (cur_yr + 1) % 10 == 0 or cur_yr == Processing.get_stop_year(): print() if Scenario.get_scen_value('logging'): Logger.log(f" {cur_yr}") if (cur_yr + 1) % 10 == 0 or cur_yr == Processing.get_stop_year(): Logger.log("") # Apply the Cellular Automaton Rules for this Year avg_slope, num_growth_pix, sng, sdc, og, rt, pop = Spread.spread( z, avg_slope) #print(f"rt: {rt}") sdg = 0 # this isn't passed into spread, but I don't know why then it's here Stats.set_sng(sng) Stats.set_sdg(sdc) #Stats.set_sdc(sdc) Stats.set_og(og) Stats.set_rt(rt) Stats.set_pop(pop) if Scenario.get_scen_value('view_growth_types'): if IGrid.using_gif: filename = f"{Scenario.get_scen_value('output_dir')}z_growth_types" \ f"_{Processing.get_current_run()}_{Processing.get_current_monte()}_" \ f"{Processing.get_current_year()}.gif" else: filename = f"{Scenario.get_scen_value('output_dir')}z_growth_types" \ f"_{Processing.get_current_run()}_{Processing.get_current_monte()}_" \ f"{Processing.get_current_year()}.tif" date = str(Processing.get_current_year()) ImageIO.write_gif(z, Color.get_growth_table(), filename, date, IGrid.nrows, IGrid.ncols) if len(Scenario.get_scen_value('landuse_data_file')) > 0: Grow.grow_landuse(land1, num_growth_pix) else: Grow.grow_non_landuse(z.gridData) seed = IGrid.igrid.get_urban_grid(0) Utilities.condition_gif(seed, z.gridData) # do Statistics Stats.update(num_growth_pix) # do Self Modification Coeff.self_modify(Stats.get_growth_rate(), Stats.get_percent_urban()) Coeff.write_current_coeff(Processing.get_current_run(), Processing.get_current_monte(), Processing.get_current_year())
def cal_leesalee(): z = PGrid.get_z() urban = IGrid.igrid.get_urban_grid_by_yr(Processing.get_current_year()) Stats.record.this_year.leesalee = 1.0 if Processing.get_processing_type() != Globals.mode_enum['predict']: Stats.compute_leesalee(z.gridData, urban)
def spread(z, avg_slope): TimerUtility.start_timer('spr_spread') sng = 0 sdc = 0 og = 0 rt = 0 nrows = IGrid.nrows ncols = IGrid.ncols total_pixels = nrows * ncols road_gravity = Coeff.get_current_road_gravity() diffusion = Coeff.get_current_diffusion() breed = Coeff.get_current_breed() spread = Coeff.get_current_spread() excld = IGrid.igrid.get_excld_grid() roads = IGrid.igrid.get_road_grid_by_year( Processing.get_current_year()) slope = IGrid.igrid.get_slope_grid() nrows = IGrid.nrows ncols = IGrid.ncols # Zero the growth array for this time period delta = [0] * (nrows * ncols) # Get slope rates slope_weights = Spread.get_slope_weights() # Phase 1N3 - Spontaneous Neighborhood Growth and Spreading sng, sdc = Spread.phase1n3(diffusion, breed, z.gridData, delta, slope, excld, slope_weights, sng, sdc) # Phase 4 - Organic Growth og = Spread.phase4(spread, z.gridData, excld, delta, slope, slope_weights, og) # Phase 5 - Road Influence Growth rt = Spread.phase5(road_gravity, diffusion, breed, z.gridData, delta, slope, excld, roads, slope_weights, rt) Utilities.condition_gt_gif(delta, UGMDefines.PHASE5G, delta, 0) Utilities.condition_ge_gif(excld, 100, delta, 0) # Now place growth array into current array num_growth_pix = 0 avg_slope = 0.0 for i in range(total_pixels): if z.gridData[i] == 0 and delta[i] > 0: # New growth being placed into array avg_slope += slope[i] z.gridData[i] = delta[i] num_growth_pix += 1 pop = 0 for pixels in z.gridData: if pixels >= UGMDefines.PHASE0G: pop += 1 if num_growth_pix == 0: avg_slope = 0.0 else: avg_slope /= num_growth_pix TimerUtility.stop_timer('spr_spread') return avg_slope, num_growth_pix, sng, sdc, og, rt, pop