pcntoolkit.dataio.fileio#

Attributes#

Functions#

create_incremental_backup(filepath)

Create an incremental backup of a file using the .bak{n} naming scheme.

create_mask(data_array, mask[, verbose])

Create a mask from a data array or a nifti file

file_extension(filename)

Determine the file extension of a file (e.g. .nii.gz)

file_stem(filename)

Determine the file stem of a file (e.g. /path/to/file.nii.gz -> file)

file_type(filename)

Determine the file type of a file

load(filename[, mask, text, vol])

Load array from a file.

load_ascii(filename)

Load an ascii file into a numpy array

load_cifti(filename[, vol, mask, rmtmp])

Load a cifti file into a numpy array

load_nifti(datafile[, mask, vol])

Load a nifti file into a numpy array

load_pd(filename)

Load a csv file into a pandas dataframe

save(data, filename[, example, mask, text, dtype])

Save a numpy array to a file

save_ascii(data, filename)

Save a numpy array to an ascii file

save_cifti(data, filename, example[, mask, vol, volatlas])

Save a cifti file from a numpy array

save_nifti(data, filename, examplenii, mask[, dtype])

Write output to nifti

save_pd(data, filename)

Save a pandas dataframe to a csv file

vol2vec(dat, mask[, verbose])

Vectorise a 3d image

Module Contents#

create_incremental_backup(filepath)#

Create an incremental backup of a file using the .bak{n} naming scheme.

If the file exists, a backup is created in the same directory with the following format:

original_name.bak{n}.original_extension

where {n} is incremented based on existing backups. If the file does not exist, None is returned.

create_mask(data_array, mask, verbose=False)#

Create a mask from a data array or a nifti file

Basic usage:

create_mask(data_array, mask, verbose)
Parameters:
  • data_array – numpy array containing the data to write out

  • mask – nifti image containing a mask for the image

  • verbose – verbose output

file_extension(filename)#

Determine the file extension of a file (e.g. .nii.gz)

Basic usage:

file_extension(filename)
Parameters:

filename – name of the file to check

file_stem(filename)#

Determine the file stem of a file (e.g. /path/to/file.nii.gz -> file)

Basic usage:

file_stem(filename)
Parameters:

filename – name of the file to check

file_type(filename)#

Determine the file type of a file

Basic usage:

file_type(filename)
Parameters:

filename – name of the file to check

Returns:

str: The file type (‘cifti’, ‘nifti’, ‘text’, or ‘binary’)

Raises:

ValueError if file type is unknown

load(filename, mask=None, text=False, vol=True)#

Load array from a file.

A beautiful waterfall of errors.

Basic usage:

load(filename, mask, text, vol)
Parameters:
  • filename – name of the file to load

  • mask – nifti image containing a mask for the image

  • text – whether to write out a text file

  • vol – whether to load the image as a volume

load_ascii(filename)#

Load an ascii file into a numpy array

Basic usage:

load_ascii(filename)
Parameters:

filename – name of the file to load

load_cifti(filename, vol=False, mask=None, rmtmp=True)#

Load a cifti file into a numpy array

Basic usage:

load_cifti(filename, vol, mask, rmtmp)
Parameters:
  • filename – name of the file to load

  • vol – whether to load the image as a volume

  • mask – nifti image containing a mask for the image

  • rmtmp – whether to remove temporary files

load_nifti(datafile, mask=None, vol=False)#

Load a nifti file into a numpy array

Basic usage:

load_nifti(datafile, mask, vol, verbose)
Parameters:
  • datafile – name of the file to load

  • mask – nifti image containing a mask for the image

  • vol – whether to load the image as a volume

  • verbose – verbose output

load_pd(filename)#

Load a csv file into a pandas dataframe

Basic usage:

load_pd(filename)
Parameters:

filename – name of the file to load

save(data, filename, example=None, mask=None, text=False, dtype=None)#

Save a numpy array to a file

Basic usage:

save(data, filename, example, mask, text, dtype)
Parameters:
  • data – numpy array containing the data to write out

  • filename – where to store it

  • example – example file to copy the geometry from

  • mask – nifti image containing a mask for the image

  • text – whether to write out a text file

  • dtype – data type for the output image (if different from the image)

save_ascii(data, filename)#

Save a numpy array to an ascii file

Basic usage:

save_ascii(data, filename)
Parameters:
  • data – numpy array containing the data to write out

  • filename – where to store it

save_cifti(data, filename, example, mask=None, vol=True, volatlas=None)#

Save a cifti file from a numpy array

Basic usage:

save_cifti(data, filename, example, mask, vol, volatlas)
Parameters:
  • data – numpy array containing the data to write out

  • filename – where to store it

  • example – example file to copy the geometry from

  • mask – nifti image containing a mask for the image

  • vol – whether to load the image as a volume

  • volatlas – atlas to use for the volume

save_nifti(data, filename, examplenii, mask, dtype=None)#

Write output to nifti

Basic usage:

save_nifti(data, filename mask, dtype)
Parameters:
  • data – numpy array containing the data to write out

  • filename – where to store it

  • examplenii – nifti to copy the geometry and data type from

  • dtype – data type for the output image (if different from the image)

Mask:

nifti image containing a mask for the image

save_pd(data, filename)#

Save a pandas dataframe to a csv file

Basic usage:

save_pd(data, filename)
Parameters:
  • data – pandas dataframe containing the data to write out

  • filename – where to store it

vol2vec(dat, mask, verbose=False)#

Vectorise a 3d image

Basic usage:

vol2vec(dat, mask, verbose)
Parameters:
  • dat – numpy array containing the data to write out

  • mask – nifti image containing a mask for the image

  • verbose – verbose output

CIFTI_MAPPINGS = ('dconn', 'dtseries', 'pconn', 'ptseries', 'dscalar', 'dlabel', 'pscalar', 'pdconn', 'dpconn',...#
CIFTI_VOL_ATLAS = 'Atlas_ROIs.2.nii.gz'#
path = b'.'#
path#