Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Adding alert module compatible output option #9

Merged
merged 2 commits into from
Dec 17, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 4 additions & 1 deletion component/message/en.json
Original file line number Diff line number Diff line change
Expand Up @@ -41,8 +41,11 @@
"conf_thld": {
"label": "Confidence threshold for accepting detected changes",
"no_conf_thld": "No value for area threshold has been provided"
},
"alert_module": {
"label": "Export files compatible with the Alert Module",
"no_conf_thld": "No value for area threshold has been provided"
}

},
"cumsum": {
"folder": "Select time series folder",
Expand Down
61 changes: 59 additions & 2 deletions component/scripts/process.py
Original file line number Diff line number Diff line change
Expand Up @@ -162,9 +162,11 @@ def main(args_list):
dst.write(out_stack, window=window)


def run_cumsum(ts_folder, outdir, tiles, period, bstraps, area_thld, conf_thld, out):

out.add_live_msg("Preparing processing")
def run_cumsum(ts_folder, outdir, tiles, period, bstraps, area_thld, conf_thld, out, alert_module=False):

out.add_live_msg('Preparing processing')

# create output folder and file
outdir = cp.result_dir / outdir
outdir.mkdir(exist_ok=True, parents=True)
Expand Down Expand Up @@ -253,7 +255,62 @@ def run_cumsum(ts_folder, outdir, tiles, period, bstraps, area_thld, conf_thld,
for file in outfiles:
Path(file).unlink()

if alert_module:
prepare_for_alert_module(
result_file,
result_file.with_suffix('.alert_date.tif'),
result_file.with_suffix('.alert_mask.tif'),
result_file.with_suffix('.alert_aoi.shp')
)

def prepare_for_alert_module(cusum_change_file, out_date, out_mask, out_aoi):

import rasterio as rio
import numpy as np
from shapely.geometry import box, mapping
import fiona

with rio.open(cusum_change_file) as src:
meta = src.meta
arr = src.read(1)
bounds = src.bounds
crs = src.crs.to_dict()

# create a datetime array that is year 1
year2000array = np.array((np.ones(arr.shape).ravel()).astype('int32').astype('str'), dtype='datetime64[D]')

# create a datetime array with the YEAR of change
yearChangeArray = np.array(np.floor(arr.ravel()).astype('int32').astype('str'), dtype='datetime64[D]')

# create an array with the doy
add_change_days = np.round((arr.ravel() - np.floor(arr.ravel())) * 365).astype('int32')

# create timedelta
timedelta_days = ((yearChangeArray - year2000array).astype('int32') - add_change_days).reshape(arr.shape)

# reset 0s to 0s
timedelta_days[arr == 0] = 0
mask = np.nan_to_num(timedelta_days/timedelta_days).astype('uint8')

meta.update(count=1, tiled=True)
with rio.open(out_date, 'w', **meta) as dst:
dst.write(timedelta_days.astype('float32'), 1)

meta.update(dtype='uint8')
with rio.open(out_mask, 'w', **meta) as dst:
dst.write(mask, 1)

# AOI
# create a Polygon from the raster bounds
bbox = box(*bounds)
# create a schema with no properties
schema = {'geometry': 'Polygon', 'properties': {}}

# create shapefile
with fiona.open(out_aoi, 'w', driver='ESRI Shapefile', crs=crs, schema=schema) as c:
c.write({'geometry': mapping(bbox), 'properties': {}})


def write_logs(log_file, start, end):

with log_file.open("w") as f:
Expand Down
11 changes: 9 additions & 2 deletions component/tile/cumsum_tile.py
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,8 @@ def __init__(self):
class_="mt-5",
)
self.period = cw.DateRangeSlider(label=cm.widget.period.label)
self.alert_module_out = v.Switch(class_ = "ml-5",label = cm.widget.alert_module.label, v_model = False)


# stack the advance parameters in a expandpanel
advance_params = v.ExpansionPanels(
Expand Down Expand Up @@ -69,6 +71,7 @@ def __init__(self):
advance_params,
v.Html(tag="h2", children=[cm.cumsum.periods]),
self.period,
self.alert_module_out
],
alert=cw.CustomAlert(),
btn=sw.Btn(cm.cumsum.btn),
Expand All @@ -91,7 +94,8 @@ def _start_process(self, widget, event, data):
area_thld = 0
conf_thld = self.conf_thld.v_model
period = self.period.v_model

alert_module_out = self.alert_module_out

# check the inputs
if not self.alert.check_input(folder, cm.widget.folder.no_folder):
return
Expand All @@ -105,7 +109,9 @@ def _start_process(self, widget, event, data):
return
if not self.alert.check_input(conf_thld, cm.widget.conf_thld.no_conf_thld):
return

if not self.alert.check_input(alert_module_out, cm.widget.conf_thld.no_conf_thld):
return

# run the cumsum process
cs.run_cumsum(
Path(folder),
Expand All @@ -116,6 +122,7 @@ def _start_process(self, widget, event, data):
area_thld,
conf_thld,
self.alert,
self.alert_module_out
)

# display the end of computation message
Expand Down
60 changes: 52 additions & 8 deletions no_ui.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
"cells": [
{
"cell_type": "code",
"execution_count": null,
"execution_count": 1,
"metadata": {},
"outputs": [],
"source": [
Expand All @@ -15,7 +15,7 @@
},
{
"cell_type": "code",
"execution_count": null,
"execution_count": 2,
"metadata": {},
"outputs": [],
"source": [
Expand All @@ -28,17 +28,46 @@
},
{
"cell_type": "code",
"execution_count": null,
"execution_count": 3,
"metadata": {},
"outputs": [],
"outputs": [
{
"data": {
"application/vnd.jupyter.widget-view+json": {
"model_id": "42d058678a3f4ccc92164c2c407a8ca0",
"version_major": 2,
"version_minor": 0
},
"text/plain": [
"Styles()"
]
},
"metadata": {},
"output_type": "display_data"
},
{
"data": {
"application/vnd.jupyter.widget-view+json": {
"model_id": "dda0667643ae45d7af4823204c3135c9",
"version_major": 2,
"version_minor": 0
},
"text/plain": [
"ResizeTrigger(template=\"\\n <script>\\n {methods: {\\n jupyter_resiz…"
]
},
"metadata": {},
"output_type": "display_data"
}
],
"source": [
"# load the patial files\n",
"%run 'cumsum_ui.ipynb'"
]
},
{
"cell_type": "code",
"execution_count": null,
"execution_count": 4,
"metadata": {},
"outputs": [],
"source": [
Expand All @@ -47,9 +76,24 @@
},
{
"cell_type": "code",
"execution_count": null,
"execution_count": 5,
"metadata": {},
"outputs": [],
"outputs": [
{
"data": {
"application/vnd.jupyter.widget-view+json": {
"model_id": "8a9a0ffaae164de7b78d3218b4460c70",
"version_major": 2,
"version_minor": 0
},
"text/plain": [
"CumSumTile(align_center=True, children=[Card(children=[Html(children=['Select time series folder'], tag='h2'),…"
]
},
"metadata": {},
"output_type": "display_data"
}
],
"source": [
"cumsum_tile"
]
Expand Down Expand Up @@ -78,7 +122,7 @@
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.6.9"
"version": "3.8.10"
}
},
"nbformat": 4,
Expand Down