Skip to content

Commit

Permalink
jupyter notebook widget for qc
Browse files Browse the repository at this point in the history
  • Loading branch information
rbardaji committed Jan 16, 2021
1 parent 881e1d3 commit 02ffb51
Show file tree
Hide file tree
Showing 3 changed files with 164 additions and 2 deletions.
3 changes: 2 additions & 1 deletion mooda/__init__.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
""" Implementation of mooda """
from .waterframe import WaterFrame
from .input import read_nc_emodnet, read_nc, read_nc_imos, read_pkl, read_nc_moist
from .util import concat, iplot_location, iplot_timeseries, md5, es_create_indexes
from .util import concat, iplot_location, iplot_timeseries, md5, \
es_create_indexes, Widgets

__version__ = '1.7.0'
3 changes: 2 additions & 1 deletion mooda/util/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,5 @@
from .iplot import iplot_location, iplot_timeseries
from .md5 import md5
from .es_create_indexes import es_create_indexes
from .emso import EMSO
from .emso import EMSO
from .md_widgets import Widgets
160 changes: 160 additions & 0 deletions mooda/util/md_widgets.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,160 @@
import ipywidgets as widgets
from IPython.display import clear_output
from typing import List, List
from ..input import read_pkl
import plotly.offline as pyo

pyo.init_notebook_mode()

class Widgets:

@staticmethod
def qc(filename, parameter: str,
range_test: List[float]=[-1000, 1000], spike_window: int=100,
spike_threshold: float=3.5, spike_influence: float=0.5):
"""
It makes a QC Jupyter Widget
Parameters
----------
filename: str
Location of the pickle file with the dataset
parameter: str
Parameter
range_test: List[float]
Limits for the range test [min value, max value]
Returns
-------
main_box: ipwidgets.VBox
Jupyter notebook widget
"""

def show_result(wf, parameter_in, chart_title=''):
# Change name of flags
wf2 = wf.copy()
qc_labels = {0: 'No QC', 1: 'Good data', 4: 'Bad data'}
wf2.data[f'{parameter}_QC'].replace(qc_labels, inplace=True)

fig = wf2.iplot_line(
parameter,
color='TEMP_QC',
marginal_y=None,
line_shape='linear',
rangeslider_visible=False,
line_dash_sequence=['dot', 'dot'],
title=chart_title)
fig.show()

# Flat test
flat_label = widgets.Label('Flat test:')
window_flat = widgets.IntText(value=2,
description='Window:',
disabled=False,
color='black')
flat_checkbox = widgets.Checkbox(description='Do it!')

# Range test
range_label = widgets.Label('Range test:')
range_checkbox = widgets.Checkbox(description='Do it!')
limits = widgets.FloatRangeSlider(value=[range_test[0], range_test[1]],
min=range_test[0],
max=range_test[1],
step=0.1,
description='Limits:',
disabled=False,
continuous_update=False,
orientation='horizontal',
readout=True,
readout_format='.1f')

# Spike test
spike_label = widgets.Label('Spike test:')
spike_checkbox = widgets.Checkbox(description='Do it!')
window_spike = widgets.IntText(value=spike_window,
description='Window:', disabled=False,
color='black')
threshold = widgets.FloatText(value=spike_threshold,
description='Threshold:', disabled=False,
color='black')
influence = widgets.FloatSlider(
value=spike_influence,
min=0,
max=1,
step=0.1,
description='Influence:',
disabled=False,
continuous_update=False,
orientation='horizontal',
readout=True,
readout_format='.1f',
slider_color='white'
)

# Replace
replace_label = widgets.Label('Replace QC:')
replace_checkbox = widgets.Checkbox(description='Do it!')

# Button
button = widgets.Button(description='Run tests')
out = widgets.Output()
def on_button_clicked(_):
# "linking function with output"
with out:
# what happens when we press the button
clear_output()
wf = read_pkl(filename)

if parameter in wf.parameters:
print(wf)
print()
show_result(wf, parameter,
'Values without apply any QC test')
print()

if flat_checkbox.value:
print(window_flat.value)
wf.qc_flat_test(window=window_flat.value)
show_result(wf, parameter,
'Values after apply the flat test. ' + \
' Configuration flat test: window = ' + \
f'{window_flat.value}')
print()
if range_checkbox.value:
wf.qc_range_test(limits=limits.value)
show_result(wf, parameter,
'Values after apply the flat test and ' + \
'the range test. Configuration ' + \
f'range test: limits = {limits.value}')
print()
if spike_checkbox.value:
wf.qc_spike_test(window=window_spike.value,
influence=influence.value,
threshold=threshold.value)
show_result(wf, parameter,
'Values after apply the flat test, the ' \
'range test and the spike test.' + \
' Configuration spike test: window ' \
f'= {window_spike.value}, ' \
f'influence = {influence.value}, ' + \
f'threshold = {threshold.value}')
print()
if replace_checkbox.value:
wf.qc_replace()
show_result(wf, parameter, f'Final result')
else:
print(f'Parameter {parameter} not in {wf.parameters}')

# linking button and function together using a button's method
button.on_click(on_button_clicked)

flat_box = widgets.HBox([flat_label, flat_checkbox, window_flat])
range_box = widgets.HBox([range_label, range_checkbox, limits])
spike_column = widgets.VBox([window_spike, threshold, influence])
spike_box = widgets.HBox([spike_label, spike_checkbox, spike_column])
replace_box = widgets.HBox([replace_label, replace_checkbox])

main_box = widgets.VBox([flat_box, range_box, spike_box, replace_box,
button, out])

return main_box

0 comments on commit 02ffb51

Please sign in to comment.