4343import pandas as pd , numpy as np
4444#import datetime
4545import matplotlib .pyplot as plt
46- import seaborn as sns
4746import warnings
4847import traceback as _traceback
4948
@@ -179,9 +178,6 @@ def __call__(self, path= "",
179178 _m .logbook_write ("Generating density variables" )
180179 self .get_density ()
181180
182- # _m.logbook_write("Creating comparison plots")
183- # self.make_plots()
184-
185181 _m .logbook_write ("Finished running 4Ds" )
186182
187183 def get_intersection_count (self ):
@@ -297,115 +293,4 @@ def density_function(mgra_in):
297293 mgra_landuse [self .base_cols + self .new_cols ].to_csv (os .path .join (self .path , self .mgradata_file ), index = False , float_format = '%.4f' )
298294
299295 self .mgra_data = mgra_landuse
300- print ( "*** Finished ***" )
301-
302- #plot comparisons of build and old density values and create heat maps
303- def make_plots (self ):
304- if len (self .mgra_data ) == 0 :
305- self .build = pd .read_csv (os .path .join (self .path , self .mgradata_file ))
306- else :
307- self .build = self .mgra_data
308-
309- def plot_continuous (field ):
310- #colors
311- rsg_orange = '#f68b1f'
312- rsg_marine = '#006fa1'
313- #rsg_leaf = '#63af5e'
314- #rsg_grey = '#48484a'
315- #rsg_mist = '#dcddde'
316-
317- max = self .base [field ].max () + self .base [field ].max ()% 5
318- div = max / 5 if max / 5 >= 10 else max / 2
319- bins = np .linspace (0 ,max ,div )
320- plt .hist (self .base [field ], bins , density = True , alpha = 0.5 , label = 'Base' , color = rsg_marine )
321- plt .hist (self .build [field ], bins , density = True , alpha = 0.5 , label = 'Build' , color = rsg_orange )
322- mean_base = self .base [field ].mean ()
323- mean = self .build [field ].mean ()
324- median_base = self .base [field ].median ()
325- median = self .build [field ].median ()
326- plt .axvline (mean_base , color = 'b' , linestyle = '-' , label = 'Base Mean' )
327- plt .axvline (median_base , color = 'b' , linestyle = '--' , label = 'Base Median' )
328- plt .axvline (mean , color = 'r' , linestyle = '-' , label = 'Build Mean' )
329- plt .axvline (median , color = 'r' , linestyle = '--' ,label = 'Build Median' )
330- plt .legend (loc = 'upper right' )
331- ylims = plt .ylim ()[1 ]
332- plt .text (mean_base + div / 4 , ylims - ylims / 32 , "mean: {:0.2f}" .format (mean_base ), color = 'b' )
333- plt .text (mean_base + div / 4 , ylims - 5 * ylims / 32 , "median: {:0.0f}" .format (median_base ), color = 'b' )
334- plt .text (mean_base + div / 4 , ylims - 2 * ylims / 32 , "mean: {:0.2f}" .format (mean ), size = 'medium' ,color = 'r' )
335- plt .text (mean_base + div / 4 , ylims - 6 * ylims / 32 , "median: {:0.0f}" .format (median ), color = 'r' )
336- plt .text (self .base [field ].min () , ylims / 32 , "min: {:0.0f}" .format (self .base [field ].min ()), color = 'b' )
337- plt .text (self .base [field ].max ()- div , ylims / 32 , "max: {:0.0f}" .format (self .base [field ].max ()), color = 'b' )
338- plt .text (self .build [field ].min () , 2 * ylims / 32 , "min: {:0.0f}" .format (self .build [field ].min ()), color = 'r' )
339- plt .text (self .base [field ].max ()- div , 2 * ylims / 32 , "max: {:0.0f}" .format (self .build [field ].max ()), color = 'r' )
340-
341- plt .xlabel (field )
342- plt .ylabel ("MGRA's" )
343- plt .title (field .replace ('den' ,'' ) + ' Density' )
344- outfile = _join (self .path , "output" , '4Ds_{}_plot.png' .format (field ))
345- if os .path .isfile (outfile ):
346- os .remove (outfile )
347- plt .savefig (outfile )
348- plt .clf ()
349-
350- def plot_discrete (field ):
351- fig , ax = plt .subplots ()
352- df1 = discretedf_base .groupby (field , as_index = False ).agg ({'mgra' :'count' ,'type' :'first' })
353- df2 = discretedf_build .groupby (field , as_index = False ).agg ({'mgra' :'count' ,'type' :'first' })
354- df = df1 .append (df2 )
355- ax = sns .barplot (x = field , y = 'mgra' , hue = 'type' , data = df )
356- ax .set_title (field )
357- outfile = _join (self .path , "output" , '4Ds_{}_plot.png' .format (field ))
358- if os .path .isfile (outfile ):
359- os .remove (outfile )
360- ax .get_figure ().savefig (outfile )
361-
362- self .base = pd .read_csv (os .path .join (self .ref_path , self .mgradata_file ))
363- self .base ['type' ] = 'base'
364- self .build ['type' ] = 'build'
365-
366- discretedf_base = self .base [['mgra' ,'type' ]+ self .discrete_fields ]
367- discretedf_build = self .build [['mgra' ,'type' ]+ self .discrete_fields ]
368-
369- for f in self .continuous_fields :
370- plot_continuous (f )
371- for f in self .discrete_fields :
372- plot_discrete (f )
373-
374- if self .maps :
375- import geopandas as gpd
376- import folium
377- from branca .colormap import linear
378- compare_int = self .base .merge (self .build , how = 'outer' , on = 'mgra' , suffixes = ['_base' ,'_build' ])
379- compare_int ['diff' ] = compare_int ['TotInt' ] - compare_int ['totint' ]
380-
381- compare_int = gpd .read_file (self .mgra_shape_file ).rename (columns = {'MGRA' :'mgra' }).merge (compare_int , how = 'left' , on = 'mgra' )
382- compare_int = compare_int .to_crs ({'init' : 'epsg:4326' })
383-
384- colormap = linear .OrRd_09 .scale (
385- compare_int .TotInt .min (),
386- compare_int .TotInt .max ())
387- colormapA = linear .RdBu_04 .scale (
388- compare_int ['diff' ].min (),
389- compare_int ['diff' ].min ()* - 1 )
390-
391- compare_int ['colordiff' ] = compare_int ['diff' ].map (lambda n : colormapA (n ))
392- compare_int ['colororig' ] = compare_int ['TotInt' ].map (lambda n : colormap (n ))
393- compare_int ['colornew' ] = compare_int ['totint' ].map (lambda n : colormap (n ))
394-
395- def makeheatmap (self ,df , colormp ,color_field ,caption ):
396- mapname = folium .Map (location = [32.76 , - 117.15 ], zoom_start = 13.459 )
397- folium .GeoJson (compare_int ,
398- style_function = lambda feature : {
399- 'fillColor' : feature ['properties' ][color_field ],
400- 'color' : rsg_marine ,
401- 'weight' : 0 ,
402- 'fillOpacity' : 0.75 ,
403- }).add_to (mapname )
404-
405- colormp .caption = caption
406- colormp .add_to (mapname )
407- return mapname
408-
409- makeheatmap (compare_int ,colormapA ,'colordiff' ,'Intersection Diff (base - build)' ).save ('diff_intersections.html' )
410- makeheatmap (compare_int ,colormap ,'colororig' ,'Intersections' ).save ('base_intersections.html' )
411- makeheatmap (compare_int ,colormap ,'colororig' ,'Intersections' ).save ('build_intersections.html' )
296+ print ( "*** Finished ***" )
0 commit comments