Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Move handling of deprecated parameters to deprecations module. #172

Merged
merged 2 commits into from
Jun 6, 2024
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
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -101,13 +101,13 @@ continuous = ['Age', 'SysABP', 'Height', 'Weight']
```python
groupby = 'death'
nonnormal = ['Age']
labels={'death': 'mortality'}
rename={'death': 'mortality'}
```

7. Create an instance of TableOne with the input arguments:

```python
mytable = TableOne(data, columns=columns, categorical=categorical, continuous=continuous, groupby=groupby, nonnormal=nonnormal, rename=labels, pval=False)
mytable = TableOne(data, columns=columns, categorical=categorical, continuous=continuous, groupby=groupby, nonnormal=nonnormal, rename=rename, pval=False)
```

8. Display the table using the `tabulate` method. The `tablefmt` argument allows the table to be displayed in multiple formats, including "github", "grid", "fancy_grid", "rst", "html", and "latex".
Expand Down
26 changes: 26 additions & 0 deletions tableone/deprecations.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
import warnings

# display deprecation warnings
warnings.simplefilter('always', DeprecationWarning)


def deprecated_parameter(parameter, parameter_name, message, version=None):
"""
Raise warning for a deprecated parameter.

Parameters:
- parameter: parameter to be removed.
- parameter: name of parameter to be removed.
- message: str, message to insert into deprecated warning.
- version: str, version when the parameter was deprecated (optional).

Usage:
deprecated_parameter(parameter=label, parameter_name='level, new_name='rename', version='0.7')
"""
if parameter:
if version:
version_message = f"version {version}"
else:
version_message = "a future version"
warnings.warn(f"{parameter_name} is deprecated and will be removed in {version_message}. {message}",
category=DeprecationWarning, stacklevel=2)
36 changes: 6 additions & 30 deletions tableone/tableone.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,10 @@
import pandas as pd
from tabulate import tabulate

from tableone.deprecations import deprecated_parameter
from tableone.statistics import Statistics
from tableone.validators import DataValidator, InputValidator, InputError

# display deprecation warnings
warnings.simplefilter('always', DeprecationWarning)


def load_dataset(name: str) -> pd.DataFrame:
"""
Expand Down Expand Up @@ -215,7 +213,10 @@ def __init__(self, data: pd.DataFrame,
tukey_test: bool = False,
pval_threshold: Optional[float] = None) -> None:

self._handle_deprecations(labels, rename, isnull, pval_test_name, remarks)
deprecated_parameter(labels, "labels", "Use 'rename' instead")
deprecated_parameter(isnull, "isnull", "Use 'missing' instead")
deprecated_parameter(pval_test_name, "pval_test_name", "Use 'htest_name' instead")
deprecated_parameter(remarks, "remarks", "Use test names instead (e.g. diptest = True)")

if not columns:
columns = data.columns.values # type: ignore
Expand All @@ -225,7 +226,7 @@ def __init__(self, data: pd.DataFrame,
self.data_validator.validate(data, columns) # type: ignore

self.input_validator = InputValidator()
self.input_validator.validate(groupby, nonnormal, min_max, pval_adjust, order, # type: ignore
self.input_validator.validate(groupby, nonnormal, min_max, pval_adjust, order, # type: ignore
pval, columns, categorical, continuous) # type: ignore

# nonnormal should be a list
Expand Down Expand Up @@ -373,31 +374,6 @@ def __init__(self, data: pd.DataFrame,
if display_all:
self._set_display_options()

def _handle_deprecations(self, labels, rename, isnull, pval_test_name, remarks):
"""
Raise deprecation warnings.
"""
if labels is not None:
warnings.warn("The 'labels' argument of TableOne() is deprecated and will be removed in a future version. "
"Use 'rename' instead.", DeprecationWarning, stacklevel=3)

if labels is not None and rename is not None:
raise TypeError("TableOne received both 'labels' and 'rename'. Please use only 'rename'.")

if isnull is not None:
warnings.warn("The 'isnull' argument is deprecated; use 'missing' instead.",
DeprecationWarning, stacklevel=3)

# pval_test_name is now htest_name
if pval_test_name:
warnings.warn("The pval_test_name argument is deprecated; use htest_name instead.",
DeprecationWarning, stacklevel=3)

if remarks:
warnings.warn("The 'remarks' argument is deprecated; specify tests "
"by name instead (e.g. diptest = True)",
DeprecationWarning, stacklevel=2)

def __str__(self) -> str:
return self.tableone.to_string() + self._generate_remarks('\n')

Expand Down
Loading