gensor 0.0.1__tar.gz

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
gensor-0.0.1/LICENSE ADDED
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2024, Mateusz Zawadzki
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
gensor-0.0.1/PKG-INFO ADDED
@@ -0,0 +1,75 @@
1
+ Metadata-Version: 2.1
2
+ Name: gensor
3
+ Version: 0.0.1
4
+ Summary: Library for handling groundwater sensor data.
5
+ Home-page: https://github.com/zawadzkim/gensor
6
+ Author: Mateusz Zawadzki
7
+ Author-email: fzawadzkimat@outlook.com
8
+ Requires-Python: >=3.11
9
+ Classifier: Programming Language :: Python :: 3
10
+ Classifier: Programming Language :: Python :: 3.11
11
+ Classifier: Programming Language :: Python :: 3.12
12
+ Requires-Dist: chardet (>=5.2.0,<6.0.0)
13
+ Requires-Dist: matplotlib (>=3.9.2,<4.0.0)
14
+ Requires-Dist: numpy (>=2.1.0,<3.0.0)
15
+ Requires-Dist: pandas (>=2.2.2,<3.0.0)
16
+ Requires-Dist: pandera (>=0.20.3,<0.21.0)
17
+ Requires-Dist: pydantic (>=2.8.2,<3.0.0)
18
+ Requires-Dist: pytz (>=2024.1,<2025.0)
19
+ Requires-Dist: scikit-learn (>=1.5.1,<2.0.0)
20
+ Requires-Dist: scipy (>=1.14.1,<2.0.0)
21
+ Requires-Dist: sqlalchemy (>=2.0.32,<3.0.0)
22
+ Project-URL: Documentation, https://zawadzkim.github.io/gensor/
23
+ Project-URL: Repository, https://github.com/zawadzkim/gensor
24
+ Description-Content-Type: text/markdown
25
+
26
+ # gensor
27
+
28
+ [![Release](https://img.shields.io/github/v/release/zawadzkim/gensor)](https://img.shields.io/github/v/release/zawadzkim/gensor)
29
+ [![Build status](https://img.shields.io/github/actions/workflow/status/zawadzkim/gensor/main.yml?branch=main)](https://github.com/zawadzkim/gensor/actions/workflows/main.yml?query=branch%3Amain)
30
+ [![codecov](https://codecov.io/gh/zawadzkim/gensor/branch/main/graph/badge.svg)](https://codecov.io/gh/zawadzkim/gensor)
31
+ [![Commit activity](https://img.shields.io/github/commit-activity/m/zawadzkim/gensor)](https://img.shields.io/github/commit-activity/m/zawadzkim/gensor)
32
+ [![License](https://img.shields.io/github/license/zawadzkim/gensor)](https://img.shields.io/github/license/zawadzkim/gensor)
33
+
34
+ Library for handling groundwater sensor data.
35
+
36
+ - **Github repository**: <https://github.com/zawadzkim/gensor/>
37
+ - **Documentation** <https://zawadzkim.github.io/gensor/>
38
+
39
+ ## Getting started with your project
40
+
41
+ First, create a repository on GitHub with the same name as this project, and then run the following commands:
42
+
43
+ ```bash
44
+ git init -b main
45
+ git add .
46
+ git commit -m "init commit"
47
+ git remote add origin git@github.com:zawadzkim/gensor.git
48
+ git push -u origin main
49
+ ```
50
+
51
+ Finally, install the environment and the pre-commit hooks with
52
+
53
+ ```bash
54
+ make install
55
+ ```
56
+
57
+ You are now ready to start development on your project!
58
+ The CI/CD pipeline will be triggered when you open a pull request, merge to main, or when you create a new release.
59
+
60
+ To finalize the set-up for publishing to PyPi or Artifactory, see [here](https://fpgmaas.github.io/cookiecutter-poetry/features/publishing/#set-up-for-pypi).
61
+ For activating the automatic documentation with MkDocs, see [here](https://fpgmaas.github.io/cookiecutter-poetry/features/mkdocs/#enabling-the-documentation-on-github).
62
+ To enable the code coverage reports, see [here](https://fpgmaas.github.io/cookiecutter-poetry/features/codecov/).
63
+
64
+ ## Releasing a new version
65
+
66
+ - Create an API Token on [Pypi](https://pypi.org/).
67
+ - Add the API Token to your projects secrets with the name `PYPI_TOKEN` by visiting [this page](https://github.com/zawadzkim/gensor/settings/secrets/actions/new).
68
+ - Create a [new release](https://github.com/zawadzkim/gensor/releases/new) on Github.
69
+ - Create a new tag in the form `*.*.*`.
70
+ - For more details, see [here](https://fpgmaas.github.io/cookiecutter-poetry/features/cicd/#how-to-trigger-a-release).
71
+
72
+ ---
73
+
74
+ Repository initiated with [fpgmaas/cookiecutter-poetry](https://github.com/fpgmaas/cookiecutter-poetry).
75
+
gensor-0.0.1/README.md ADDED
@@ -0,0 +1,49 @@
1
+ # gensor
2
+
3
+ [![Release](https://img.shields.io/github/v/release/zawadzkim/gensor)](https://img.shields.io/github/v/release/zawadzkim/gensor)
4
+ [![Build status](https://img.shields.io/github/actions/workflow/status/zawadzkim/gensor/main.yml?branch=main)](https://github.com/zawadzkim/gensor/actions/workflows/main.yml?query=branch%3Amain)
5
+ [![codecov](https://codecov.io/gh/zawadzkim/gensor/branch/main/graph/badge.svg)](https://codecov.io/gh/zawadzkim/gensor)
6
+ [![Commit activity](https://img.shields.io/github/commit-activity/m/zawadzkim/gensor)](https://img.shields.io/github/commit-activity/m/zawadzkim/gensor)
7
+ [![License](https://img.shields.io/github/license/zawadzkim/gensor)](https://img.shields.io/github/license/zawadzkim/gensor)
8
+
9
+ Library for handling groundwater sensor data.
10
+
11
+ - **Github repository**: <https://github.com/zawadzkim/gensor/>
12
+ - **Documentation** <https://zawadzkim.github.io/gensor/>
13
+
14
+ ## Getting started with your project
15
+
16
+ First, create a repository on GitHub with the same name as this project, and then run the following commands:
17
+
18
+ ```bash
19
+ git init -b main
20
+ git add .
21
+ git commit -m "init commit"
22
+ git remote add origin git@github.com:zawadzkim/gensor.git
23
+ git push -u origin main
24
+ ```
25
+
26
+ Finally, install the environment and the pre-commit hooks with
27
+
28
+ ```bash
29
+ make install
30
+ ```
31
+
32
+ You are now ready to start development on your project!
33
+ The CI/CD pipeline will be triggered when you open a pull request, merge to main, or when you create a new release.
34
+
35
+ To finalize the set-up for publishing to PyPi or Artifactory, see [here](https://fpgmaas.github.io/cookiecutter-poetry/features/publishing/#set-up-for-pypi).
36
+ For activating the automatic documentation with MkDocs, see [here](https://fpgmaas.github.io/cookiecutter-poetry/features/mkdocs/#enabling-the-documentation-on-github).
37
+ To enable the code coverage reports, see [here](https://fpgmaas.github.io/cookiecutter-poetry/features/codecov/).
38
+
39
+ ## Releasing a new version
40
+
41
+ - Create an API Token on [Pypi](https://pypi.org/).
42
+ - Add the API Token to your projects secrets with the name `PYPI_TOKEN` by visiting [this page](https://github.com/zawadzkim/gensor/settings/secrets/actions/new).
43
+ - Create a [new release](https://github.com/zawadzkim/gensor/releases/new) on Github.
44
+ - Create a new tag in the form `*.*.*`.
45
+ - For more details, see [here](https://fpgmaas.github.io/cookiecutter-poetry/features/cicd/#how-to-trigger-a-release).
46
+
47
+ ---
48
+
49
+ Repository initiated with [fpgmaas/cookiecutter-poetry](https://github.com/fpgmaas/cookiecutter-poetry).
@@ -0,0 +1,14 @@
1
+ from .compensation import Compensator, compensate
2
+ from .dtypes import Dataset, Timeseries
3
+ from .getters import read_from_csv
4
+ from .preprocessing import OutlierDetection, Transform
5
+
6
+ __all__ = [
7
+ "Dataset",
8
+ "Timeseries",
9
+ "read_from_csv",
10
+ "OutlierDetection",
11
+ "Transform",
12
+ "Compensator",
13
+ "compensate",
14
+ ]
@@ -0,0 +1,110 @@
1
+ """Compensating the raw data from the absolute pressure transducer to the actual water
2
+ level using the barometric pressure data.
3
+
4
+ Because van Essen Instrument divers are non-vented pressure transducers, to obtain the
5
+ pressure resulting from the water column above the logger (i.e. the water level), the
6
+ barometric pressure must be subtracted from the raw pressure measurements. In the
7
+ first step the function aligns the two series to the same time step and then subtracts
8
+ the barometric pressure from the raw pressure measurements. For short time periods (when
9
+ for instance a slug test is performed) the barometric pressure can be provided as a
10
+ single float value.
11
+
12
+ Subsequently the function filters out all records where the absolute water column is
13
+ less than or equal to the cutoff value. This is because when the logger is out of the
14
+ water when the measurement is taken, the absolute water column is close to zero,
15
+ producing erroneous results and spikes in the plots. The cutoff value is set to 5 cm by
16
+ default, but can be adjusted using the cutoff_wc kwarg.
17
+
18
+ Functions:
19
+
20
+ compensate: Compensate raw sensor pressure measurement with barometric pressure.
21
+ """
22
+
23
+ from typing import Self
24
+
25
+ import pydantic as pyd
26
+
27
+ from .dtypes import Timeseries
28
+ from .exceptions import (
29
+ InvalidMeasurementTypeError,
30
+ MissingInputError,
31
+ )
32
+
33
+
34
+ class Compensator(pyd.BaseModel):
35
+ ts: Timeseries
36
+ barometric: Timeseries | float
37
+ drop_low_wc: bool = True
38
+
39
+ @pyd.field_validator("ts", "barometric", mode="before")
40
+ def validate_timeseries_type(cls, v):
41
+ if isinstance(v, Timeseries) and v.variable != "pressure":
42
+ raise InvalidMeasurementTypeError(v.location)
43
+ return v
44
+
45
+ @pyd.field_validator("ts")
46
+ def validate_sensor_information(cls, v: Timeseries):
47
+ if v.sensor is not None and not v.sensor_alt:
48
+ raise MissingInputError("sensor_alt")
49
+ return v
50
+
51
+ def compensate(self, **kwargs) -> Self | None:
52
+ """Compensate raw sensor pressure measurement with barometric pressure.
53
+
54
+ Parameters:
55
+ ts (Timeseries): Raw sensor timeseries
56
+ barometric (Timeseries or float): Barometric pressure timeseries or a single
57
+ float value. If a float value is provided, it is assumed to be in cmH2O.
58
+ drop_low_wc (bool): Whether to drop records where the absolute water column is
59
+ less than or equal to the cutoff value. Defaults to True.
60
+ inplace (bool): Whether to update the timeseries in place. Defaults to True.
61
+
62
+ Keyword Arguments:
63
+ alignment_period (str): The alignment period for the timeseries.
64
+ Default is 'H' (hourly).
65
+ threshold_wc (float): The threshold for the absolute water column.
66
+ Defaults to 0.5 m.
67
+
68
+ Returns:
69
+ Timeseries: A new Timeseries instance with the compensated data.
70
+ """
71
+
72
+ alignment_period = kwargs.get("alignment_period", "h")
73
+ threshold_wc = kwargs.get("threshold_wc", 0.5)
74
+ resample_params = {"freq": alignment_period, "agg_func": "mean"}
75
+
76
+ if isinstance(self.barometric, Timeseries):
77
+ if self.ts == self.barometric:
78
+ print("Skipping compensation: both timeseries are the same.")
79
+ return None
80
+ baro = self.barometric.resample(**resample_params).ts
81
+ else:
82
+ baro = self.barometric
83
+
84
+ resampled_ts = self.ts.resample(**resample_params)
85
+
86
+ # dividing by 100 to convert water column from cmH2O to mH2O
87
+ watercolumn_ts = resampled_ts.ts.sub(baro).divide(100).dropna()
88
+
89
+ if self.drop_low_wc:
90
+ watercolumn_ts_filtered = watercolumn_ts[
91
+ watercolumn_ts.abs() > threshold_wc
92
+ ]
93
+ print(
94
+ f"{len(watercolumn_ts) - len(watercolumn_ts_filtered)} records \
95
+ dropped due to low water column."
96
+ )
97
+ gwl = watercolumn_ts_filtered.add(float(resampled_ts.sensor_alt))
98
+ else:
99
+ gwl = watercolumn_ts.add(float(resampled_ts.sensor_alt))
100
+
101
+ compensated = resampled_ts.model_copy(
102
+ update={"ts": gwl, "unit": "m asl", "variable": "head"}
103
+ )
104
+
105
+ return compensated
106
+
107
+
108
+ def compensate(ts, barometric, drop_low_wc, **kwargs) -> Timeseries:
109
+ comp = Compensator(ts=ts, barometric=barometric, drop_low_wc=drop_low_wc)
110
+ return comp.compensate(**kwargs)
@@ -0,0 +1,3 @@
1
+ from .connection import DatabaseConnection
2
+
3
+ __all__ = ["DatabaseConnection"]
@@ -0,0 +1,49 @@
1
+ """Module for database connection."""
2
+
3
+ from pathlib import Path
4
+
5
+ import pydantic as pyd
6
+ from sqlalchemy import Engine, create_engine
7
+ from sqlalchemy.orm import Session, sessionmaker
8
+
9
+ from ..exceptions import DatabaseNotFound
10
+
11
+
12
+ class DatabaseConnection(pyd.BaseModel):
13
+ """Class for handling the database connection.
14
+ If no database exists at the specified path, it will be created.
15
+ If no database is specified, an in-memory database will be used.
16
+
17
+ The user should specify the database directory and name separately. If directory is not specified,
18
+ current directory and a default name are used. ."""
19
+
20
+ model_config = pyd.ConfigDict(
21
+ arbitrary_types_allowed=True, validate_assignment=True
22
+ )
23
+
24
+ in_memory: bool = False
25
+ db_directory: Path = Path.cwd()
26
+ db_name: str = "gensor.db"
27
+ engine: Engine | None = None
28
+ session: Session | None = None
29
+
30
+ def __post_init__(self) -> None:
31
+ self.connect()
32
+
33
+ def _verify_path(self) -> str:
34
+ if self.in_memory:
35
+ return "sqlite:///:memory:"
36
+ else:
37
+ if not self.db_directory.exists():
38
+ raise DatabaseNotFound()
39
+ else:
40
+ return f"sqlite:///{self.db_directory}/{self.db_name}"
41
+
42
+ def connect(self) -> Session:
43
+ sqlite_path = self._verify_path()
44
+
45
+ self.engine = create_engine(sqlite_path)
46
+ session = sessionmaker(bind=self.engine)
47
+ self.session = session()
48
+
49
+ return session()