Skip to content

Commit

Permalink
Merge pull request #404 from jnsebgosselin/refactor_wldataset
Browse files Browse the repository at this point in the history
PR: Rename WLDataFrame to WLDataset and WLDataset to WLDataFrame
  • Loading branch information
jnsebgosselin committed Mar 2, 2022
2 parents 237eaae + b5c5bad commit 5f843be
Show file tree
Hide file tree
Showing 10 changed files with 56 additions and 53 deletions.
4 changes: 2 additions & 2 deletions gwhat/brf_mod/tests/test_brf_mod.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
from gwhat.brf_mod.kgs_gui import (
BRFManager, KGSBRFInstaller, QMessageBox, QFileDialog)
from gwhat.projet.reader_projet import ProjetReader
from gwhat.projet.reader_waterlvl import WLDataFrame
from gwhat.projet.reader_waterlvl import WLDataset
from gwhat.config.main import CONF


Expand All @@ -40,7 +40,7 @@ def wldataset(project):
# Create a wldset object from a file.
rootpath = osp.dirname(osp.realpath(__file__))
filepath = osp.join(rootpath, 'data', 'sample_water_level_datafile.csv')
wldset = WLDataFrame(filepath)
wldset = WLDataset(filepath)

# Add the wldset to the project.
project.add_wldset('test_brf_wldset', wldset)
Expand Down
4 changes: 2 additions & 2 deletions gwhat/projet/manager_data.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@
from gwhat.utils import icons
import gwhat.common.widgets as myqt
from gwhat.common.utils import calc_dist_from_coord
from gwhat.projet.reader_waterlvl import WLDataFrame
from gwhat.projet.reader_waterlvl import WLDataset
from gwhat.projet.reader_projet import INVALID_CHARS, is_dsetname_valid
from gwhat.meteo.weather_reader import WXDataFrame
from gwhat.widgets.buttons import ToolBarWidget
Expand Down Expand Up @@ -707,7 +707,7 @@ def load_dataset(self, filename):

try:
if self._datatype == 'water level':
self._dataset = WLDataFrame(filename)
self._dataset = WLDataset(filename)
elif self._datatype == 'daily weather':
self._dataset = WXDataFrame(filename)
except Exception as e:
Expand Down
12 changes: 6 additions & 6 deletions gwhat/projet/reader_projet.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@

# ---- Local library imports
from gwhat.meteo.weather_reader import WXDataFrameBase, METEO_VARIABLES
from gwhat.projet.reader_waterlvl import WLDataFrameBase, WLDataset
from gwhat.projet.reader_waterlvl import WLDatasetBase, WLDataFrame
from gwhat.gwrecharge.glue import GLUEDataFrameBase
from gwhat.common.utils import save_content_to_file
from gwhat.utils.math import nan_as_text_tolist, calcul_rmse
Expand Down Expand Up @@ -220,7 +220,7 @@ def get_wldset(self, name):
if name in self.wldsets:
self.set_last_opened_wldset(name)
print('done')
return WLDataFrameHDF5(self.db['wldsets/%s' % name])
return WLDatasetHDF5(self.db['wldsets/%s' % name])
else:
print('failed')
return None
Expand Down Expand Up @@ -282,7 +282,7 @@ def add_wldset(self, name, df):
print('Unable to save dataset to project db')
del self.db['wldsets'][name]

return WLDataFrameHDF5(grp)
return WLDatasetHDF5(grp)

def del_wldset(self, name):
"""Delete the specified water level dataset."""
Expand Down Expand Up @@ -389,7 +389,7 @@ def del_wxdset(self, name):
self.db.flush()


class WLDataFrameHDF5(WLDataFrameBase):
class WLDatasetHDF5(WLDatasetBase):
"""
This is a wrapper around the h5py group that is used to store
water level datasets. It mimick the structure of the DataFrame that
Expand All @@ -398,7 +398,7 @@ class WLDataFrameHDF5(WLDataFrameBase):
"""

def __init__(self, hdf5group, *args, **kwargs):
super(WLDataFrameHDF5, self).__init__(*args, **kwargs)
super().__init__(*args, **kwargs)
self.__load_dataset__(hdf5group)

def __load_dataset__(self, hdf5group):
Expand All @@ -413,7 +413,7 @@ def __load_dataset__(self, hdf5group):
columns.append(colname)
data = np.vstack(tuple(data)).transpose()
columns = tuple(columns)
self._dataf = WLDataset(data, columns)
self._dataf = WLDataFrame(data, columns)

# Setup the structure for the Master Recession Curve
if 'mrc' not in list(self.dset.keys()):
Expand Down
59 changes: 31 additions & 28 deletions gwhat/projet/reader_waterlvl.py
Original file line number Diff line number Diff line change
Expand Up @@ -39,9 +39,13 @@
])
COLUMNS = list(COL_REGEX.keys())

HEADER = {'Well': '', 'Well ID': '',
'Province': '', 'Municipality': '',
'Latitude': 0, 'Longitude': 0, 'Elevation': 0}
HEADER = {'Well': '',
'Well ID': '',
'Province': '',
'Municipality': '',
'Latitude': 0,
'Longitude': 0,
'Elevation': 0}
HEADER_REGEX = {
'Well': r'(?<!\S)(wellname|name)(:|=)?(?!\S)',
'Well ID': r'(?<!\S)(wellid|id)(:|=)?(?!\S)',
Expand All @@ -53,26 +57,27 @@
}


class EmptyWLDataset(pd.DataFrame):
def __init__(self):
super().__init__(np.empty((0, len(COLUMNS))), columns=COLUMNS)
class WLDataFrame(pd.DataFrame):
def __init__(self, data: list = None, columns: list = None):
super().__init__(data=[], columns=COLUMNS)
self.set_index([INDEX], drop=True, inplace=True)


class WLDataset(EmptyWLDataset):
def __init__(self, data, columns):
super().__init__()
df = pd.DataFrame(data, columns=columns)
for column in columns:
for colname, regex in COL_REGEX.items():
str_ = column.replace(" ", "").replace("_", "")
if re.search(regex, str_, re.IGNORECASE):
self[colname] = df[column].copy()
break
del df
if data is not None and columns is not None:
df = pd.DataFrame(data, columns=columns)
for column in columns:
for colname, regex in COL_REGEX.items():
str_ = column.replace(" ", "").replace("_", "")
if re.search(regex, str_, re.IGNORECASE):
self[colname] = df[column].copy()
break
del df
self.format_numeric_data()
self.format_datetime_data()

@property
def _constructor(self):
return WLDataFrame

def format_numeric_data(self):
"""Format the data to floats type."""
for colname in COLUMNS:
Expand Down Expand Up @@ -139,8 +144,6 @@ def open_water_level_datafile(filename):
sheet.iter_rows(min_col=1, values_only=True)]
finally:
workbook.close()
print(data)

return data


Expand Down Expand Up @@ -177,7 +180,7 @@ def read_water_level_datafile(filename):
return None

# Cast the data into a Pandas dataframe.
dataf = WLDataset(reader[i+1:], columns=row)
dataf = WLDataFrame(reader[i+1:], columns=row)

# Add the metadata to the dataframe.
for key in header.keys():
Expand Down Expand Up @@ -261,16 +264,16 @@ def generate_HTML_table(name, lat, lon, alt, mun):
return table


class WLDataFrameBase(Mapping):
class WLDatasetBase(Mapping):
"""
A water level data frame base class.
"""

def __init__(self, *args, **kwargs):
super().__init__(*args, **kwargs)
super().__init__()
self.dset = None
self._undo_stack = []
self._dataf = EmptyWLDataset()
self._dataf = WLDataFrame()

def __load_dataset__(self):
"""Loads the dataset and save it in a store."""
Expand Down Expand Up @@ -359,7 +362,7 @@ def _add_to_undo_stack(self, indexes):
self._undo_stack.append(self._dataf['WL'].iloc[indexes].copy())


class WLDataFrame(WLDataFrameBase):
class WLDataset(WLDatasetBase):
"""
A water level dataset container that loads its data from a csv
or an Excel file.
Expand Down Expand Up @@ -389,9 +392,9 @@ def __load_dataset__(self, filename):

if __name__ == "__main__":
from gwhat import __rootdir__
df = WLDataFrame(
df = WLDataset(
osp.join(__rootdir__, 'tests', "water_level_datafile.csv"))
df2 = WLDataFrame(
df2 = WLDataset(
osp.join(__rootdir__, 'tests', "water_level_datafile.xls"))
df3 = WLDataFrame(
df3 = WLDataset(
osp.join(__rootdir__, 'tests', "water_level_datafile.xlsx"))
8 changes: 4 additions & 4 deletions gwhat/projet/tests/test_manager_data.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@

# ---- Local Libraries Imports
from gwhat.meteo.weather_reader import WXDataFrame
from gwhat.projet.reader_waterlvl import WLDataFrame
from gwhat.projet.reader_waterlvl import WLDataset
from gwhat.projet.reader_projet import ProjetReader
from gwhat.projet.manager_data import (DataManager, QFileDialog, QMessageBox,
QCheckBox)
Expand Down Expand Up @@ -219,8 +219,8 @@ def test_delete_waterlevel_data(datamanager, mocker, qtbot):
"""
Test deleting water level datasets from the project.
"""
datamanager.new_wldset_imported('wldset1', WLDataFrame(WLFILENAME))
datamanager.new_wldset_imported('wldset2', WLDataFrame(WLFILENAME))
datamanager.new_wldset_imported('wldset1', WLDataset(WLFILENAME))
datamanager.new_wldset_imported('wldset2', WLDataset(WLFILENAME))
assert datamanager.wldataset_count() == 2

# Click to delete the current water level dataset, but cancel.
Expand Down Expand Up @@ -262,7 +262,7 @@ def test_last_opened_datasets(qtbot, projectpath):

# Add some water level dataset.
for name in ['wldset1', 'wldset2', 'wldset3']:
datamanager.new_wldset_imported(name, WLDataFrame(WLFILENAME))
datamanager.new_wldset_imported(name, WLDataset(WLFILENAME))
assert datamanager.get_current_wldset().name == 'wldset3'

# Add some weather dataset.
Expand Down
6 changes: 3 additions & 3 deletions gwhat/projet/tests/test_project.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@
from gwhat.projet.reader_projet import ProjetReader
from gwhat.projet.manager_projet import (
ProjetManager, QFileDialog, QMessageBox, CONF)
from gwhat.projet.reader_waterlvl import WLDataFrame
from gwhat.projet.reader_waterlvl import WLDataset
from gwhat.utils.math import nan_as_text_tolist

NAME = "test @ prô'jèt!"
Expand Down Expand Up @@ -409,7 +409,7 @@ def test_store_mrc(project, testfile):
Test that MRC data and results are saved and retrieved as expected
in GWHAT project files.
"""
project.add_wldset('dataset_test', WLDataFrame(testfile))
project.add_wldset('dataset_test', WLDataset(testfile))
wldset = project.get_wldset('dataset_test')
assert wldset.mrc_exists() is False

Expand Down Expand Up @@ -449,7 +449,7 @@ def test_mrc_backward_compatibility(project, testfile):
See jnsebgosselin/gwhat#377.
"""
# Add the dataset to the project.
project.add_wldset('dataset_test', WLDataFrame(testfile))
project.add_wldset('dataset_test', WLDataset(testfile))
wldset = project.get_wldset('dataset_test')

# Make sure that the namespace was created automatically for the mrc
Expand Down
4 changes: 2 additions & 2 deletions gwhat/recession/recession_calc.py
Original file line number Diff line number Diff line change
Expand Up @@ -144,10 +144,10 @@ def calculate_mrc(t, h, periods: list(tuple), mrctype: int = 1):


if __name__ == '__main__':
from gwhat.projet.reader_waterlvl import WLDataFrame
from gwhat.projet.reader_waterlvl import WLDataset
import matplotlib.pyplot as plt

wldset = WLDataFrame(
wldset = WLDataset(
"C:/Users/User/gwhat/gwhat/tests/data/sample_water_level_datafile.csv")
periods = [
(41384.260416666664, 41414.114583333336),
Expand Down
4 changes: 2 additions & 2 deletions gwhat/tests/test_hydrocalc.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@

# ---- Local Libraries Imports
from gwhat.meteo.weather_reader import WXDataFrame
from gwhat.projet.reader_waterlvl import WLDataFrame
from gwhat.projet.reader_waterlvl import WLDataset
from gwhat.HydroCalc2 import WLCalc
from gwhat.projet.manager_data import DataManager
from gwhat.projet.reader_projet import ProjetReader
Expand Down Expand Up @@ -50,7 +50,7 @@ def project(projectpath):
project.add_wxdset(wxdset.metadata['Station Name'], wxdset)

# Add the water level dataset to the project.
wldset = WLDataFrame(WLFILENAME)
wldset = WLDataset(WLFILENAME)
project.add_wldset(wldset['Well'], wldset)
return project

Expand Down
4 changes: 2 additions & 2 deletions gwhat/tests/test_hydroprint.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@

# ---- Local Libraries Imports
from gwhat.meteo.weather_reader import WXDataFrame
from gwhat.projet.reader_waterlvl import WLDataFrame
from gwhat.projet.reader_waterlvl import WLDataset
from gwhat.HydroPrint2 import (HydroprintGUI, PageSetupWin, QFileDialog,
QMessageBox)
from gwhat.projet.manager_data import DataManager
Expand Down Expand Up @@ -52,7 +52,7 @@ def project(projectpath):
project.add_wxdset(wxdset.metadata['Station Name'], wxdset)

# Add the water level dataset to the project.
wldset = WLDataFrame(WLFILENAME)
wldset = WLDataset(WLFILENAME)
project.add_wldset(wldset['Well'], wldset)
return project

Expand Down
4 changes: 2 additions & 2 deletions gwhat/tests/test_read_waterlvl.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
# ---- Local library imports
from gwhat.common.utils import save_content_to_excel, save_content_to_csv
from gwhat.projet.reader_waterlvl import (
load_waterlvl_measures, init_waterlvl_measures, WLDataFrame)
load_waterlvl_measures, init_waterlvl_measures, WLDataset)

DATA = [['Well name = ', "êi!@':i*"],
['well id : ', '1234ABC'],
Expand Down Expand Up @@ -72,7 +72,7 @@ def datatmpdir(tmp_path):
# =============================================================================
@pytest.mark.parametrize("ext", ['.csv', '.xls', '.xlsx'])
def test_reading_waterlvl(datatmpdir, ext):
df = WLDataFrame(osp.join(datatmpdir, 'water_level_datafile' + ext))
df = WLDataset(osp.join(datatmpdir, 'water_level_datafile' + ext))

expected_results = {
'Well': "êi!@':i*",
Expand Down

0 comments on commit 5f843be

Please sign in to comment.