From 3d64fd689e611a70e9d1f1432a3db8cfa8cdd7da Mon Sep 17 00:00:00 2001
From: Olivier Jossoud <olivier.jossoud@lsce.ipsl.fr>
Date: Tue, 8 Oct 2019 15:45:55 +0200
Subject: [PATCH] Instant_config_df: fill NAs with previous values, to match
 new CFA conductcalib setup.

---
 src/dataprovider/conductcalibprovider.py | 16 ++++++++++++++++
 1 file changed, 16 insertions(+)

diff --git a/src/dataprovider/conductcalibprovider.py b/src/dataprovider/conductcalibprovider.py
index bc8e245..730309e 100644
--- a/src/dataprovider/conductcalibprovider.py
+++ b/src/dataprovider/conductcalibprovider.py
@@ -61,18 +61,34 @@ class CalibRun:
 
     def update_steps_df(self):
         config_df = self.instant_config_df.copy()
+
+        # From long to wide format
         config_df = config_df.pivot(index='datetime', columns='name', values='value')
+
+        # Forward fill NAs. Values not changing are not recorded in instant_config_df, so fill with previous value.
+        config_df = config_df.fillna(method='ffill')
+
+        # Compute step number for each line
         config_df["step"] = np.arange(len(config_df)) + 1
+
+        # Set start/end datetime for each step
         config_df["start_datetime"] = config_df.index
         config_df["end_datetime"] = config_df["start_datetime"].shift(-1)
+
+        # For the last step, compute the end_datetime from expected length written in User-defined config file.
         config_df.loc[config_df.index[-1], 'end_datetime'] = \
             config_df.loc[config_df.index[-1], 'start_datetime'] \
             + datetime.timedelta(seconds=int(self.user_config_df.loc[self.user_config_df.index[-1], 'length_sec']))
         config_df.reset_index(drop=True, inplace=True)
+
+        # Reshape: separate column for excite and headgain
         config_df = pd.wide_to_long(config_df, ["excite", "headgain"], i="step", j="channel", sep="_").reset_index()
+
+        # Add "stable" timeframe limit for each step, i.e. remove transition phases at the beginning/end of each step.
         config_df["stable_start_datetime"] = config_df["start_datetime"] + datetime.timedelta(seconds=2)
         config_df["stable_end_datetime"] = config_df["end_datetime"] - datetime.timedelta(seconds=2)
 
+        # Compute mean, standard deviation and validity for each step/channelComment
         for channel in range(1, 6):
             channel_config_df = config_df[config_df["channel"] == channel].copy()
             for index, row in channel_config_df.iterrows():
-- 
GitLab