adnipy package

Submodules

adnipy.adni module

Pandas dataframe extension for ADNI.

class adnipy.adni.ADNI(pandas_dataframe)[source]

Bases: object

Methods

drop_dynamic(self) Remove images which are dynamic.
groups(self[, grouped_mci]) Create a dataframe for each group and save it to a csv file.
longitudinal(self) Keep only longitudinal data.
rid(self) Add a roster ID column.
standard_column_names(self) Rename dataframe columns to module standard.
standard_dates(self) Change type of date columns to datetime.
standard_index(self[, index]) Process dataframes into a standardized format.
timepoints(self[, second]) Extract timepoints from a dataframe.
drop_dynamic(self)[source]

Remove images which are dynamic.

Drops all rows, in which the Description contains ‘Dynamic’.

Returns:
pd.DataFrame

All images that are not dynamic.

groups(self, grouped_mci=True)[source]

Create a dataframe for each group and save it to a csv file.

Parameters:
grouped_mci : bool, default True

If true, ‘LMCI’ and ‘EMCI’ are treated like ‘MCI’. However, the original values will stills be in the output.

Returns:
dict

Dictionnairy with a dataframe for each group.

longitudinal(self)[source]

Keep only longitudinal data.

This requires an ‘RID’ or ‘Subject ID’ column in the dataframe. Do not use if multiple images are present for a single timepoint.

Parameters:
images : pd.DataFrame

This dataframe will be modified.

Returns:
pd.DataFrame

A dataframe with only longitudinal data.

See also

drop_dynamic
rid(self)[source]

Add a roster ID column.

Will not work if ‘RID’ is already present or ‘Subject ID’ is missing.

Returns:
pd.DataFrame

Dataframe with a ‘RID’ column.

Examples

>>> subjects = {"Subject ID": ["100_S_1000", "101_S_1001"]}
>>> collection = pd.DataFrame(subjects)
>>> collection
Subject ID
0  100_S_1000
1  101_S_1001
>>> collection.rid()
Subject ID   RID
0  100_S_1000  1000
1  101_S_1001  1001
standard_column_names(self)[source]

Rename dataframe columns to module standard.

This function helps when working with multiple dataframes, since the same data can have different names. It will also call rid() on the dataframe.

Returns:
pd.DataFrame

This will have standardized columns names.

See also

rid

Examples

>>> subjects = pd.DataFrame({"Subject": ["101_S_1001", "102_S_1002"]})
>>> subjects
    Subject
0  101_S_1001
1  102_S_1002
>>> subjects.standard_column_names()
Subject ID   RID
0  101_S_1001  1001
1  102_S_1002  1002
>>> images = pd.DataFrame({"Image": [100001, 100002]})
>>> images
    Image
0  100001
1  100002
>>> images.standard_column_names()
Image ID
0    100001
1    100002
standard_dates(self)[source]

Change type of date columns to datetime.

Returns:
pd.DataFrame

Dates will have the appropriate dtype.

standard_index(self, index=None)[source]

Process dataframes into a standardized format.

The output is easy to read. Applying functions the the output may not work as expected.

Parameters:
index : list of str, default None

These columns will be the new index.

Returns:
pd.DataFrame

An easy to read dataframe for humans.

timepoints(self, second='first')[source]

Extract timepoints from a dataframe.

Parameters:
second : {‘first’ or ‘last’}, default ‘first’

‘last’ to have the latest, ‘first’ to have the earliest values for timepoint 2.

adnipy.adnipy module

Process ADNI study data with adnipy.

adnipy.adnipy.drop_dynamic(images)[source]

Remove images which are dynamic.

Drops all rows, in which the Description contains ‘Dynamic’.

Parameters:
images : pd.DataFrame

This dataframe will be modified.

Returns:
pd.DataFrame

All images that are not dynamic.

adnipy.adnipy.get_matching_images(left, right)[source]

Match different scan types based on closest date.

The columns ‘Subject ID’ and ‘SCANDATE’ are required.

Parameters:
left : pd.DataFrame

Dataframe containing the tau scans.

right : pd.DataFrame

Dataframe containing the mri scans.

Returns:
pd.DataFrame

For each timepoint there is a match from both inputs.

adnipy.adnipy.groups(collection, grouped_mci=True)[source]

Create a dataframe for each group and save it to a csv file.

Parameters:
collection : pd.DataFrame

DataFrame has to have a Group column.

grouped_mci : bool, default True

If true, ‘LMCI’ and ‘EMCI’ are treated like ‘MCI’. However, the original values will stills be in the output.

Returns:
dict

Dictionnairy with a dataframe for each group.

adnipy.adnipy.longitudinal(images)[source]

Keep only longitudinal data.

This requires an ‘RID’ or ‘Subject ID’ column in the dataframe. Do not use if multiple images are present for a single timepoint.

Parameters:
images : pd.DataFrame

This dataframe will be modified.

Returns:
pd.DataFrame

A dataframe with only longitudinal data.

See also

drop_dynamic
adnipy.adnipy.read_csv(file)[source]

Return a csv file as a pandas.DataFrame.

Recognizes missing values used in the ADNI database.

Parameters:
file : str, pathlib.Path

The path to the .csv file.

Returns:
pd.DataFrame

Returns the file as a dataframe.

adnipy.adnipy.rid(collection)[source]

Add a roster ID column.

Will not work if ‘RID’ is already present or ‘Subject ID’ is missing.

Parameters:
collection : pd.DataFrame

This dataframe will be modified.

Returns:
pd.DataFrame

Dataframe with a ‘RID’ column.

Examples

>>> collection = pd.DataFrame({"Subject ID": ["100_S_1000", "101_S_1001"]})
>>> collection
   Subject ID
0  100_S_1000
1  101_S_1001
>>> rid(collection)
   Subject ID   RID
0  100_S_1000  1000
1  101_S_1001  1001
adnipy.adnipy.standard_column_names(dataframe)[source]

Rename dataframe columns to module standard.

This function helps when working with multiple dataframes, since the same data can have different names. It will also call rid() on the dataframe.

Parameters:
dataframe : pd.DataFrame

This dataframe will be modified.

Returns:
pd.DataFrame

This will have standardized columns names.

See also

rid

Examples

>>> subjects = pd.DataFrame({"Subject": ["101_S_1001", "102_S_1002"]})
>>> subjects
      Subject
0  101_S_1001
1  102_S_1002
>>> standard_column_names(subjects)
   Subject ID   RID
0  101_S_1001  1001
1  102_S_1002  1002
>>> images = pd.DataFrame({"Image": [100001, 100002]})
>>> images
    Image
0  100001
1  100002
>>> standard_column_names(images)
   Image ID
0    100001
1    100002
adnipy.adnipy.standard_dates(dataset)[source]

Change type of date columns to datetime.

Parameters:
dataset : pd.DataFrame

This dataframe will be modified.

Returns:
pd.DataFrame

Dates will have the appropriate dtype.

adnipy.adnipy.standard_index(df, index=None)[source]

Process dataframes into a standardized format.

The output is easy to read. Applying functions the the output may not work as expected.

Parameters:
df : pd.DataFrame

This dataframe will be modified.

index : list of str, default None

These columns will be the new index.

Returns:
pd.DataFrame

An easy to read dataframe for humans.

adnipy.adnipy.timedelta(old, new)[source]

Get timedelta between timepoints.

Parameters:
old : pd.DataFrame

This is the older dataframe.

new : pd.DataFrame

This is the newer dataframe.

Returns:
pd.Series

The content will be timedelta values. Look into numpy for more options.

adnipy.adnipy.timepoints(df, second='first')[source]

Extract timepoints from a dataframe.

Parameters:
df : pd.DataFrame

This dataframe will be used as a base.

second : {‘first’ or ‘last’}, default ‘first’

‘last’ to have the latest, ‘first’ to have the earliest values for timepoint 2.

adnipy.data module

Process data created in Matlab.

adnipy.data.image_id_from_filename(filename)[source]

Extract image ID of single ADNI .nii filename.

Images from the ADNI database have a specific formatting. Using regular expressions the image ID can be extracted from filenames.

Parameters:
filename : str

It must contain the Image ID at the end.

Returns:
numpy.int64

Image as a integer.

Examples

>>> image_id_from_filename("*_I123456.nii")
123456

Module contents

Process ADNI study data with adnipy.