Skip to content

Commit

Permalink
added some verbose option
Browse files Browse the repository at this point in the history
  • Loading branch information
JannisHoch committed Mar 29, 2021
1 parent 5195638 commit ea7e3ad
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 16 deletions.
18 changes: 10 additions & 8 deletions copro/scripts/postprocessing/avg_over_time.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,11 +13,12 @@
@click.option('-c', '--column', help='column name', default='chance_of_conflict', type=str)
@click.option('--geojson/--no-geojson', help='save output to geojson or not', default=False)
@click.option('--png/--no-png', help='save output to png or not', default=True)
@click.option('--verbose/--no-verbose', help='verbose on/off', default=False)
@click.argument('input_dir', type=click.Path())
@click.argument('output_dir',type=click.Path())
@click.argument('selected_polygons', type=click.Path())

def main(input_dir=None, output_dir=None, selected_polygons=None, start_year=None, end_year=None, geojson=None, png=None, column=None):
def main(input_dir=None, output_dir=None, selected_polygons=None, start_year=None, end_year=None, geojson=None, png=None, column=None, verbose=None):
"""Post-processing script to calculate average model output over a user-specifeid period or all output geojson-files.
"""

Expand Down Expand Up @@ -50,7 +51,7 @@ def main(input_dir=None, output_dir=None, selected_polygons=None, start_year=Non
# if the year-suffix of geojson-file matches year in period, add to list
year = int(str(str(os.path.basename(fo)).rsplit('.')[0]).rsplit('_')[-1])
if year in period:
print('adding to selection')
if verbose: print('adding to selection')
selected_files.append(fo)
# if not end/start time is specified, use all geojson-files in input-dir
else:
Expand All @@ -65,7 +66,7 @@ def main(input_dir=None, output_dir=None, selected_polygons=None, start_year=Non
# go through all geojson-files left over after selection step
for geojson in selected_files:
# read files and convert to datatrame
click.echo('reading file {}'.format(geojson))
if verbose: click.echo('reading file {}'.format(geojson))
gdf = gpd.read_file(geojson, driver='GeoJSON')
df = pd.DataFrame(gdf)
# append to dataframe
Expand All @@ -76,7 +77,8 @@ def main(input_dir=None, output_dir=None, selected_polygons=None, start_year=Non
y_out = pd.DataFrame()
# get all unique IDs of polygons
y_out['ID'] = y_df.ID.unique()
if column = 'chance_of_conflict'
click.echo('reading from column {}'.format(column))
if column == 'chance_of_conflict':
# add number of predictiosn made over all selected years
y_out = pd.merge(y_out, y_df.nr_predictions.groupby(y_df.ID).sum().to_frame(), on='ID')
# add number of predicted conflicts over all selected years
Expand All @@ -99,8 +101,8 @@ def main(input_dir=None, output_dir=None, selected_polygons=None, start_year=Non

# if specified, save as geojson-file to output-dir
if geojson:
click.echo('saving to {}'.format(os.path.abspath(os.path.join(output_dir, 'merged_output_{}.geojson'.format(suffix)))))
gdf_out.to_file(os.path.abspath(os.path.join(output_dir, 'merged_output_{}.geojson'.format(suffix))), driver='GeoJSON')
click.echo('saving to {}'.format(os.path.abspath(os.path.join(output_dir, '{}_merged_{}.geojson'.format(column, suffix)))))
gdf_out.to_file(os.path.abspath(os.path.join(output_dir, '{}_merged_{}.geojson'.format(column, suffix))), driver='GeoJSON')

# if specified, save as png-file to output-dir
if png:
Expand All @@ -111,8 +113,8 @@ def main(input_dir=None, output_dir=None, selected_polygons=None, start_year=Non
legend=True,
legend_kwds={'label': column, 'orientation': "vertical"})

click.echo('saving to {}'.format(os.path.abspath(os.path.join(output_dir, 'merged_output_{}.png'.format(suffix)))))
plt.savefig(os.path.abspath(os.path.join(output_dir, 'merged_output_{}.png'.format(suffix))), dpi=300, bbox_inches='tight')
click.echo('saving to {}'.format(os.path.abspath(os.path.join(output_dir, '{}_merged_{}.png'.format(column, suffix)))))
plt.savefig(os.path.abspath(os.path.join(output_dir, '{}_merged_{}.png'.format(column, suffix))), dpi=300, bbox_inches='tight')
plt.close()

if __name__ == '__main__':
Expand Down
6 changes: 3 additions & 3 deletions copro/scripts/postprocessing/geojson2gif.py
Original file line number Diff line number Diff line change
Expand Up @@ -47,12 +47,12 @@ def main(input_dir=None, column=None, color_map=None, minimum_value=None, maximu

ax.set_title(str(year))
click.echo('saving plot to png-folder')
plt.savefig(os.path.join(png_dir, 'plt{}.png'.format(year)), dpi=300, bbox_inches='tight')
plt.savefig(os.path.join(png_dir, 'plt_{}_{}.png'.format(column, year)), dpi=300, bbox_inches='tight')

click.echo('creating GIF from saved plots')
# based on: https://pillow.readthedocs.io/en/stable/handbook/image-file-formats.html#gif
fp_in = os.path.join(png_dir, '*.png')
fp_out = os.path.join(output_dir, 'conflict_over_time.gif')
fp_in = os.path.join(png_dir, '*_{}_{}.png'.format(column, year))
fp_out = os.path.join(output_dir, '{}_over_time.gif'.format(column))
img, *imgs = [Image.open(f) for f in sorted(glob.glob(fp_in))]
img.save(fp=fp_out, format='GIF', append_images=imgs, save_all=True, duration=500, loop=0)

Expand Down
11 changes: 6 additions & 5 deletions copro/scripts/postprocessing/plot_prediction_dev.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,11 @@
@click.option('-id', '--polygon-id', multiple=True, type=int)
@click.option('-c', '--column', help='column name', default='chance_of_conflict', type=str)
@click.option('-t', '--title', help='title for plot and file_object name', type=str)
@click.option('--verbose/--no-verbose', help='verbose on/off', default=False)
@click.argument('input-dir', type=click.Path())
@click.argument('output-dir', type=click.Path())

def main(input_dir=None, polygon_id=None, column=None, title=None, output_dir=None):
def main(input_dir=None, polygon_id=None, column=None, title=None, output_dir=None, verbose=None):
"""Quick and dirty function to plot the develoment of a column in the outputted geojson-files over time.
"""

Expand All @@ -32,15 +33,15 @@ def main(input_dir=None, polygon_id=None, column=None, title=None, output_dir=No

print('retrieving values from column {}'.format(column))
for geojson in all_files:
print('reading file {}'.format(geojson))
if verbose: print('reading file {}'.format(geojson))
gdf = gpd.read_file(geojson, driver='GeoJSON')
df = pd.DataFrame(gdf.drop(columns='geometry'))

year = int(str(str(os.path.basename(geojson)).rsplit('.')[0]).rsplit('_')[-1])
years.append(year)

for idx in polygon_id:
print('sampling ID {}'.format(idx))
if verbose: print('sampling ID {}'.format(idx))

if idx not in df.ID.values:
print('WARNING: ID {} is not in {} - NaN set'.format(idx, geojson))
Expand All @@ -59,7 +60,7 @@ def main(input_dir=None, polygon_id=None, column=None, title=None, output_dir=No
click.echo('creating output folder {}'.format(os.path.abspath(output_dir)))
os.makedirs(os.path.abspath(output_dir))

df.to_csv(os.path.abspath(os.path.join(output_dir, 'prediction_dev.csv')))
df.to_csv(os.path.abspath(os.path.join(output_dir, '{}_dev.csv'.format(column))))

fig, axes = plt.subplots(nrows=len(polygon_id), ncols=1, sharex=True)
df.plot(subplots=True, ax=axes)
Expand All @@ -68,7 +69,7 @@ def main(input_dir=None, polygon_id=None, column=None, title=None, output_dir=No
ax.set_yticks(np.arange(0, 1.1, 1))
if title != None:
ax.set_title(str(title))
plt.savefig(os.path.abspath(os.path.join(output_dir, 'prediction_dev.png')), dpi=300, bbox_inches='tight')
plt.savefig(os.path.abspath(os.path.join(output_dir, '{}_dev.png'.format(column))), dpi=300, bbox_inches='tight')

if __name__ == '__main__':

Expand Down

0 comments on commit ea7e3ad

Please sign in to comment.