diff --git a/reView/index.py b/reView/index.py index 606f824..804e187 100644 --- a/reView/index.py +++ b/reView/index.py @@ -24,7 +24,7 @@ def main(): host=APP_HOST, port=APP_PORT, debug=False, - dev_tools_hot_reload=False + dev_tools_hot_reload=True ) diff --git a/reView/pages/rev/controller/callbacks.py b/reView/pages/rev/controller/callbacks.py index 87ca359..b32d086 100644 --- a/reView/pages/rev/controller/callbacks.py +++ b/reView/pages/rev/controller/callbacks.py @@ -1573,11 +1573,11 @@ def retrieve_signal( trigger = callback_trigger() # If no x is specified - if x.startswith("capacity") and "density" not in x: - if x != config.capacity_column: - x = config.capacity_column if not x: x = config.capacity_column + elif x.startswith("capacity") and "density" not in x: + if x != config.capacity_column: + x = config.capacity_column # Fix y if y is capacity if y.startswith("capacity") and "density" not in y: @@ -1987,12 +1987,3 @@ def toggle_timeseries_below_options(n_clicks, is_open): if n_clicks: return not is_open return is_open - - -# @app.callback( -# Output("last_project", "children"), -# Input("project", "value"), -# ) -# def store_last_project(project): -# """Store last project to reView knows when we switch.""" -# return project diff --git a/reView/utils/config.py b/reView/utils/config.py index eac5149..b5611b9 100644 --- a/reView/utils/config.py +++ b/reView/utils/config.py @@ -13,6 +13,7 @@ from itertools import chain from pathlib import Path +import h5py import pandas as pd from reView import REVIEW_DATA_DIR @@ -54,16 +55,49 @@ def contains(pattern, patterns): return any(p in pattern for p in patterns) -def infer_capcol(file): +def decode(df): + """Decode the columns of a meta data object from a reV output.""" + def decode_single(x): + """Try to decode a single value, pass if fail.""" + try: + x = x.decode() + except UnicodeDecodeError: + x = "indecipherable" + return x + + for c in df.columns: + x = df[c].iloc[0] + if isinstance(x, bytes): + try: + df[c] = df[c].apply(decode_single) + except Exception: + df[c] = None + print("Column " + c + " could not be decoded.") + elif isinstance(x, str): + try: + if isinstance(ast.literal_eval(x), bytes): + try: + self._obj[c] = self._obj[c].apply( + lambda x: ast.literal_eval(x).decode() + ) + except Exception: + df[c] = None + print("Column " + c + " could not be decoded.") + except: + pass + return df + + +def infer_capcol(fpath): """Infer the capacity column from a data frame (prefer ac).""" - cols = read_rev(file, nrows=0).columns + cols = read_rev(fpath, nrows=0).columns skippers = ["density", "turbine", "system"] capcols = [col for col in cols if "capacity" in col] capcols = [col for col in capcols if not contains(col, skippers)] if len(capcols) == 1: capcol = capcols[0] elif any("_ac" in col for col in capcols): - capcol = [col for col in capcols if "ac" in col][0] + capcol = [col for col in capcols if "_ac" in col][0] else: raise KeyError("Could not find capacity column") return capcol @@ -73,8 +107,12 @@ def read_rev(fpath, nrows=None): """Infer the appropriate read method for a reV supply curve.""" if Path(fpath).name.endswith("parquet"): sc = pd.read_parquet(fpath, nrows=nrows) - else: + elif Path(fpath).name.endswith("csv"): sc = pd.read_csv(fpath, nrows=nrows) + elif Path(fpath).name.endswith(".h5"): + sc = pd.DataFrame(h5py.File(fpath)["meta"][:nrows]) + if sc.shape[0] > 0: + sc = decode(sc) return sc @@ -108,9 +146,9 @@ def __repr__(self): @property def capacity_column(self): """Return the most appropriate capacity column.""" - for name, file in self._project_files: + for _, fpath in self._project_files: break - capcol = infer_capcol(file) + capcol = infer_capcol(fpath) return capcol @property