Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 12 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,17 @@

All notable changes to this project will be documented in this file.

## [0.2.2] - 2024-12-03

### 🚀 Features

- *(utils)* Add better update func for pandas

### 🐛 Bug Fixes

- Order of sub-/superlevel inference
- Don't ignore present sub-/superlvl cols

## [0.2.1] - 2024-11-29

### 🐛 Bug Fixes
Expand Down Expand Up @@ -191,6 +202,7 @@ Initial implementation of the lyDATA library.
<!-- generated by git-cliff -->
<!-- markdownlint-disable-file MD024 -->

[0.2.2]: https://github.com/rmnldwg/lydata/compare/0.2.1..0.2.2
[0.2.1]: https://github.com/rmnldwg/lydata/compare/0.2.0..0.2.1
[0.2.0]: https://github.com/rmnldwg/lydata/compare/0.1.2..0.2.0
[0.1.2]: https://github.com/rmnldwg/lydata/compare/0.1.1..0.1.2
Expand Down
6 changes: 0 additions & 6 deletions lydata/accessor.py
Original file line number Diff line number Diff line change
Expand Up @@ -785,9 +785,6 @@ def infer_sublevels(

for subid in subids:
sublevel = superlevel + subid
if sublevel in self._obj[modality, side]:
continue

result.loc[is_healthy, (modality, side, sublevel)] = False
result.loc[~is_healthy, (modality, side, sublevel)] = None

Expand Down Expand Up @@ -851,9 +848,6 @@ def infer_superlevels(
except KeyError:
continue

if superlevel in self._obj[modality, side]:
continue

result.loc[are_all_healthy, (modality, side, superlevel)] = False
result.loc[is_any_involved, (modality, side, superlevel)] = True
result.loc[is_unknown, (modality, side, superlevel)] = None
Expand Down
49 changes: 43 additions & 6 deletions lydata/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,37 @@
from pydantic import BaseModel, Field


def update_and_expand(
left: pd.DataFrame,
right: pd.DataFrame,
**update_kwargs: Any,
) -> pd.DataFrame:
"""Update ``left`` with values from ``right``, also adding .

The added feature of this function over pandas' :py:meth:`~pandas.DataFrame.update`
is that it also adds columns that are present in ``right`` but not in ``left``.

Any keyword arguments are also directly passed to the
:py:meth:`~pandas.DataFrame.update`.

>>> left = pd.DataFrame({"a": [1, 2, None], "b": [3, 4, 5]})
>>> right = pd.DataFrame({"a": [None, 3, 4], "c": [6, 7, 8]})
>>> update_and_expand(left, right)
a b c
0 1.0 3 6
1 3.0 4 7
2 4.0 5 8
"""
result = left.copy()
result.update(right, **update_kwargs)

for column in right.columns:
if column not in result.columns:
result[column] = right[column]

return result


@dataclass
class _ColumnSpec:
"""Class for specifying column names and aggfuncs."""
Expand Down Expand Up @@ -123,12 +154,12 @@ def get_default_modalities() -> dict[str, ModalityConfig]:

def infer_all_levels(
dataset: pd.DataFrame,
infer_sublevels_kwargs: dict[str, Any] | None = None,
infer_superlevels_kwargs: dict[str, Any] | None = None,
infer_sublevels_kwargs: dict[str, Any] | None = None,
) -> pd.DataFrame:
"""Infer all levels of involvement for each diagnostic modality.

This function first infers sublevel (e.g. 'IIa", and 'IIb') involvement for each
This function first infers sublevel (e.g. 'IIa', and 'IIb') involvement for each
modality using :py:meth:`~lydata.accessor.LyDataAccessor.infer_sublevels`. Then,
it infers superlevel (e.g. 'II') involvement for each modality using
:py:meth:`~lydata.accessor.LyDataAccessor.infer_superlevels`.
Expand All @@ -138,14 +169,20 @@ def infer_all_levels(

result = dataset.copy()

result = result.join(result.ly.infer_sublevels(**infer_sublevels_kwargs))
return result.join(result.ly.infer_superlevels(**infer_superlevels_kwargs))
result = update_and_expand(
left=result,
right=result.ly.infer_superlevels(**infer_superlevels_kwargs),
)
return update_and_expand(
left=result,
right=result.ly.infer_sublevels(**infer_sublevels_kwargs),
)


def infer_and_combine_levels(
dataset: pd.DataFrame,
infer_sublevels_kwargs: dict[str, Any] | None = None,
infer_superlevels_kwargs: dict[str, Any] | None = None,
infer_sublevels_kwargs: dict[str, Any] | None = None,
combine_kwargs: dict[str, Any] | None = None,
) -> pd.DataFrame:
"""Enhance the dataset by inferring additional columns from the data.
Expand All @@ -171,8 +208,8 @@ def infer_and_combine_levels(
"""
result = infer_all_levels(
dataset,
infer_sublevels_kwargs=infer_sublevels_kwargs,
infer_superlevels_kwargs=infer_superlevels_kwargs,
infer_sublevels_kwargs=infer_sublevels_kwargs,
)
combine_kwargs = combine_kwargs or {}
max_llh = pd.concat(
Expand Down
Loading