From 7f3b3b0a7526dbc99461f93eaae39b0ec4669194 Mon Sep 17 00:00:00 2001 From: Akio Taniguchi Date: Sat, 17 Aug 2024 04:52:23 +0000 Subject: [PATCH 1/3] #204 Add utility to get phase label of 1D DataArray --- decode/utils.py | 34 +++++++++++++++++++++++++++++++++- tests/test_utils.py | 6 ++++++ 2 files changed, 39 insertions(+), 1 deletion(-) diff --git a/decode/utils.py b/decode/utils.py index 11df1a6..606ddf7 100644 --- a/decode/utils.py +++ b/decode/utils.py @@ -1,4 +1,4 @@ -__all__ = ["mad"] +__all__ = ["mad", "phaseof"] # dependencies @@ -38,3 +38,35 @@ def median(da: xr.DataArray) -> xr.DataArray: ) return median(cast(xr.DataArray, np.abs(da - median(da)))) + + +def phaseof( + da: xr.DataArray, + /, + *, + keep_attrs: bool = False, + keep_coords: bool = False, +) -> xr.DataArray: + """Assign a phase to each value in a 1D DataArray. + + The function assigns a unique phase (int64) to consecutive + identical values in the DataArray. The phase increases + sequentially whenever the value in the DataArray changes. + + Args: + da: Input 1D DataArray. + keep_attrs: Whether to keep attributes of the input. + keep_coords: Whether to keep coordinates of the input. + + Returns: + 1D int64 DataArray of phases. + + """ + if da.ndim != 1: + raise ValueError("Input DataArray must be 1D.") + + is_transision = xr.zeros_like(da, bool) + is_transision.data[1:] = da.data[1:] != da.data[:-1] + + phase = is_transision.cumsum(keep_attrs=keep_attrs) + return phase if keep_coords else phase.reset_coords(drop=True) diff --git a/tests/test_utils.py b/tests/test_utils.py index 177a62c..2415773 100644 --- a/tests/test_utils.py +++ b/tests/test_utils.py @@ -8,3 +8,9 @@ def test_mad() -> None: dems = MS.new(np.arange(25).reshape(5, 5)) assert (utils.mad(dems, "time") == 5.0).all() + + +def test_phaseof() -> None: + tester = xr.DataArray([0, 1, 1, 2, 2, 2, 1, 0]) + expected = xr.DataArray([0, 1, 1, 2, 2, 2, 3, 4]) + assert (utils.phaseof(tester) == expected).all() From 84975a14ca1e868be099952a09f94fe72a8ef5e5 Mon Sep 17 00:00:00 2001 From: Akio Taniguchi Date: Sat, 17 Aug 2024 04:53:06 +0000 Subject: [PATCH 2/3] =?UTF-8?q?#204=20Update=20package=20version=20(2024.8?= =?UTF-8?q?.3=20=E2=86=92=202024.8.4)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- CITATION.cff | 2 +- README.md | 2 +- decode/__init__.py | 2 +- pyproject.toml | 2 +- 4 files changed, 4 insertions(+), 4 deletions(-) diff --git a/CITATION.cff b/CITATION.cff index b1d2ddd..4fc9d8f 100644 --- a/CITATION.cff +++ b/CITATION.cff @@ -3,7 +3,7 @@ message: "If you use this software, please cite it as below." title: "de:code" abstract: "DESHIMA code for data analysis" -version: 2024.8.3 +version: 2024.8.4 date-released: 2024-08-10 license: "MIT" doi: "10.5281/zenodo.3384216" diff --git a/README.md b/README.md index 089e025..8abdf64 100644 --- a/README.md +++ b/README.md @@ -11,7 +11,7 @@ DESHIMA code for data analysis ## Installation ```shell -pip install decode==2024.8.3 +pip install decode==2024.8.4 ``` ## Quick look diff --git a/decode/__init__.py b/decode/__init__.py index 9c18d27..3378233 100644 --- a/decode/__init__.py +++ b/decode/__init__.py @@ -10,7 +10,7 @@ "select", "utils", ] -__version__ = "2024.8.3" +__version__ = "2024.8.4" # submodules diff --git a/pyproject.toml b/pyproject.toml index e9aae3a..c164263 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -1,6 +1,6 @@ [tool.poetry] name = "decode" -version = "2024.8.3" +version = "2024.8.4" description = "DESHIMA code for data analysis" authors = [ "Akio Taniguchi ", From b27b4015f83a78ed3d548faf43efa157c700d8c9 Mon Sep 17 00:00:00 2001 From: Akio Taniguchi Date: Sat, 17 Aug 2024 04:53:29 +0000 Subject: [PATCH 3/3] #204 Update release date --- CITATION.cff | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/CITATION.cff b/CITATION.cff index 4fc9d8f..86816f7 100644 --- a/CITATION.cff +++ b/CITATION.cff @@ -4,7 +4,7 @@ message: "If you use this software, please cite it as below." title: "de:code" abstract: "DESHIMA code for data analysis" version: 2024.8.4 -date-released: 2024-08-10 +date-released: 2024-08-17 license: "MIT" doi: "10.5281/zenodo.3384216" url: "https://github.com/deshima-dev/decode"