nipoppy.tabular.base

Generic class for tabular data.

Module Contents

class nipoppy.tabular.base.BaseTabular(*args, **kwargs)

Bases: pandas.DataFrame, abc.ABC

Generic class with utilities for tabular data.

See https://pandas.pydata.org/docs/development/extending.html.

abstract property model: type[BaseTabularModel]

Model class associated with the tabular data.

Return type:

type[BaseTabularModel]

index_cols
add_or_update_records(records, validate=True)

Add or update records.

Parameters:

records (list[dict] | dict)

Return type:

typing_extensions.Self

concatenate(other, validate=True)

Concatenate two dataframes.

Parameters:

other (typing_extensions.Self)

Return type:

typing_extensions.Self

equals(other)

Check if two dataframes are equal.

Parameters:

other (object)

Return type:

typing_extensions.Self

find_duplicates(cols=None)

Find duplicate records.

Return type:

typing_extensions.Self

get_diff(other, cols=None)

Get the difference between two dataframes (self - other).

Returns a slice of self. If cols is None, the index_cols of the first object is used.

Parameters:

other (typing_extensions.Self)

Return type:

typing_extensions.Self

get_series_class()

Get the series class associated with a dataframe.

Return type:

type[pandas.Series]

classmethod load(fpath, validate=True, **kwargs)

Load (and optionally validate) a tabular data file.

Parameters:

fpath (nipoppy.env.StrOrPathLike)

Return type:

typing_extensions.Self

save_with_backup(fpath_symlink, dname_backups=None, use_relative_path=True, sort=True, dry_run=False)

Save the dataframe to a file with a backup.

Parameters:
  • fpath_symlink (nipoppy.env.StrOrPathLike)

  • dname_backups (Optional[str])

Return type:

pathlib.Path | None

sort_values(**kwargs)

Sort the dataframe, by default on specific columns and ignoring the index.

validate()

Validate the dataframe based on the model.

Return type:

typing_extensions.Self

class nipoppy.tabular.base.BaseTabularModel(/, **data)

Bases: pydantic.BaseModel

Helper class for validating tabular data.

Subclasses should define fields and their types, and optionally override the _validate_before_fields() method.

Note: This class is called “model” to be consistent with Pydantic nomenclature, but it can be thought of as a schema for each row in the tabular file.

Parameters:

data (Any)

classmethod validate_before(data)

Validate the raw input.

Parameters:

data (Any)