Skip to content

Commit

Permalink
updated report metrics, functional
Browse files Browse the repository at this point in the history
  • Loading branch information
deronsmith committed Feb 22, 2024
1 parent c000b8f commit 8417b91
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 15 deletions.
15 changes: 11 additions & 4 deletions flaskr/metrics.py
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,7 @@ def calculate_metrics(objectids: list, year: int, day: int, historic_days: int =
"extent_units": "%",
# "magnitude_units": "cell concentration",
# "area_normalized_magnitude_units": "cells/km^2",
"chia_normalized_magnitude_units": "kg*km^-2"
"chia_normalized_magnitude_units": "mg*m^-2"
}
t1 = time.time()
print(f"Metric calculation runtime: {round(t1 - t0, 3)} sec")
Expand Down Expand Up @@ -143,7 +143,10 @@ def calculate_extent(data: pd.DataFrame, detect_columns: list, all_columns: list
all_cells = data[all_columns].sum(axis=1)
extent_i = (detections / all_cells).to_numpy()
extent = extent_i[extent_i.nonzero()]
extent_mean = np.round(100 * np.mean(extent), 2)
if len(extent) == 0:
extent_mean = 0.0
else:
extent_mean = np.round(100 * np.mean(extent), 2)

# objectids = [int(oid) for oid in list(data.OBJECTID.unique())]
#
Expand All @@ -153,7 +156,11 @@ def calculate_extent(data: pd.DataFrame, detect_columns: list, all_columns: list
detects = data[detect_columns].sum(axis=1)
oid_cells = data[all_columns].sum(axis=1)
oid_extent_i = (detects/oid_cells).to_numpy()
oid_extent = np.round(100 * np.mean(oid_extent_i[oid_extent_i.nonzero()]), 2)
oid_extent_i = oid_extent_i[oid_extent_i.nonzero()]
if len(oid_extent_i) == 0:
oid_extent = 0.0
else:
oid_extent = np.round(100 * np.mean(oid_extent_i), 2)
oid_extent = oid_extent if not np.isnan(oid_extent) else 0.0
wb_extent[int(oid)] = oid_extent
# for oid in objectids:
Expand Down Expand Up @@ -206,7 +213,7 @@ def calculate_magnitude(data: pd.DataFrame, detect_columns: list, all_columns: l
for comid in objectids:
wb_data = get_waterbody_by_fids(waterbody_fids[comid])
wb_area = wb_data[0][0]['properties']['AREASQKM']
pixel_area = 0.9 # 900sqmeters -> 0.9sqkm
pixel_area = 0.009 # 300sqmeters -> 0.3sqkm
area_normalized_magnitude[int(comid)] = round((pixel_area/wb_area) * magnitude_wb[str(comid)], 2)

chia_area_normalized_bloom = {}
Expand Down
20 changes: 10 additions & 10 deletions flaskr/report.py
Original file line number Diff line number Diff line change
Expand Up @@ -481,9 +481,9 @@ def get_waterbody_block(year: int, day: int, objectid: int, report_id: str, rang
waterbody_properties_cleaned["Elevation (m)"] = get_elevation(objectid=objectid, meters=True)
waterbody_properties_cleaned["Bloom Extent"] = f"{extent} %"
waterbody_properties_cleaned["Bloom Frequency"] = f"{frequency} %"
waterbody_properties_cleaned["Bloom Magnitude"] = f"{magnitude} cell concentration"
waterbody_properties_cleaned["Bloom Area Normalized Magnitude"] = f"{mag_area_norm} cells/km^2"
waterbody_properties_cleaned["Bloom Chia Area Normalized Magnitude"] = f"{chia_area_norm} kg*km^-2"
# waterbody_properties_cleaned["Bloom Magnitude"] = f"{magnitude} cell concentration"
# waterbody_properties_cleaned["Bloom Area Normalized Magnitude"] = f"{mag_area_norm} cells/km^2"
waterbody_properties_cleaned["Bloom Chia Area Normalized Magnitude"] = f"{chia_area_norm} mg*m^-3"

waterbody_raster = get_report_waterbody_raster(objectid=objectid, day=day, year=year, report_id=report_id)
waterbody_plots, data_table = get_waterbody_plots(objectid=objectid, day=day, year=year, report_id=report_id,
Expand Down Expand Up @@ -731,7 +731,7 @@ def get_collection_history30(stacked_data, object_list, color_mapping, report_id
'xanchor': 'center', 'yanchor': 'top',
'font': {'size': 26}},
xaxis_title="Date", yaxis_title="Percent by Category",
width=1200, height=600
width=1200, height=600, yaxis_tickformat='.2%'
)
# history_30_fig.show()
history_30 = f"{groupid}-{year}-{day}-history30.{IMAGE_FORMAT}"
Expand Down Expand Up @@ -916,8 +916,8 @@ def get_waterbody_pie(data, ranged_data, report_root, objectid: int, day: int, y
def get_waterbody_30bar(ranged_data, report_root, objectid: int, day: int, year: int, area: float, color_mapping,
p_days: int = 30):
current_date = datetime(year=year, month=1, day=1) + timedelta(days=day - 31)
all_columns = ["Date", "Low (sqkm)", "Medium (sqkm)", "High (sqkm)", "Very High (sqkm)", "Below Detection (sqkm)",
"Land (sqkm)", "No Data (sqkm)", f"Pixel Area<br>(sqkm)", "Geometry<br>Area<br>(sqkm)",
all_columns = ["Date", "Low Count", "Medium Count", "High Count", "Very High Count", "Below Detection Count",
"Land Count", "No Data Count", "Total Count", "Geometry<br>Area<br>(sqkm)",
"Low (%)", "Medium (%)", "High (%)", "Very High (%)", "Below Detection (%)", "Land (%)",
"No Data (%)"]
bold_columns = []
Expand Down Expand Up @@ -980,8 +980,8 @@ def get_waterbody_30bar(ranged_data, report_root, objectid: int, day: int, year:
def get_waterbody_30day_html(ranged_data, report_root, objectid: int, day: int, year: int, area: float, color_mapping,
p_days: int = 30):
current_date = datetime(year=year, month=1, day=1) + timedelta(days=day - 31)
all_columns = ["Date", "Low (sqkm)", "Medium (sqkm)", "High (sqkm)", "Very High (sqkm)", "Below Detection (sqkm)",
"Land (sqkm)", "No Data (sqkm)", f"Pixel Area<br>(sqkm)", "Geometry<br>Area<br>(sqkm)",
all_columns = ["Date", "Low Count", "Medium Count", "High Count", "Very High Count", "Below Detection Count",
"Land Count", "No Data Count", "Total Count", "Geometry<br>Area<br>(sqkm)",
"Low (%)", "Medium (%)", "High (%)", "Very High (%)", "Below Detection (%)", "Land (%)",
"No Data (%)"]
bold_columns = []
Expand Down Expand Up @@ -1010,8 +1010,8 @@ def get_waterbody_30day_html(ranged_data, report_root, objectid: int, day: int,
percentages = np.around(100 * (np.divide(ranged_data[i_k], int(np.sum(ranged_data[i_k])))), 2)
stacked_csv.append(
[f"{current_date.year}-{current_date.month}-{current_date.day}",
*np.around(0.09 * np.array(ranged_data[i_k]), 4),
np.around(0.09 * np.sum(ranged_data[i_k]), 4), round(area, 2), *percentages])
*np.around(np.array(ranged_data[i_k]), 4),
np.round(np.sum(ranged_data[i_k]), 0), round(area, 2), *percentages])
# current_date = current_date + timedelta(days=1)
orig_csv = copy.copy(stacked_csv)
stacked_csv = [*zip(*stacked_csv)] # transposing 2d matrix
Expand Down
2 changes: 1 addition & 1 deletion start_flask.sh
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
#!/bin/bash
exec uwsgi /etc/uwsgi/uwsgi.ini
exec uwsgi /etc/uwsgi/uwsgi.ini

0 comments on commit 8417b91

Please sign in to comment.