deep_folding.brainvisa.utils package

Submodules

deep_folding.brainvisa.utils.bbox module

Getting bounding box from a list of sulci and specific hemisphere

The aim of this script is to output the bounding box of one or several sulci of a specified hemisphere.

deep_folding.brainvisa.utils.bbox.compute_max(list_bbmin: list, list_bbmax: list) → tuple

Returns the coordinates of the box encompassing all input boxes

Parameters: list_bbmin: list containing the upper right vertex of the box list_bbmax: list containing the lower left vertex of the box

Returns: tuple (bbmin, bbmax) with

bbmin: numpy array with the x,y,z coordinates
of the upper right corner of the box;
bbMax: numpy array with the x,y,z coordinates
of the lower left corner of the box
deep_folding.brainvisa.utils.bbox.compute_max_box(sulci_list, side, talairach_box=False, src_dir='test/bbox/1mm')

Returns maximal bounding box of a given list of sulci

It reads json files contained in the source directory. They are listed in subdirectory ‘L’ or ‘R’ according t hemisphere Each json file is the name of the sulcus + ‘json’

Parameters:
  • sulci_list – a list of sulci
  • side – a string corresponding to the hemisphere, whether ‘L’ or ‘R’
  • talairach_box – a boolean whether using Talairach coordinates or voxels
  • src_dir – path to source directory containing bbox dimensions
Returns:

tuple (bbminx, bbmax) with

bbmin: an array of minimum bounding box coordinates of given sulci; bbmax: an array of maximum bouding box coordinates of given sulci

deep_folding.brainvisa.utils.constants module

deep_folding.brainvisa.utils.convert_volume_to_bucket module

This program converts volumes contained in a folder into buckets. It writes bucket files in the output folder

deep_folding.brainvisa.utils.convert_volume_to_bucket.build_bucket_filename(subject, tgt_dir, mesh)

Returns bucket filename

deep_folding.brainvisa.utils.convert_volume_to_bucket.get_basename_without_extension(filename)

Returns file basename without extension

deep_folding.brainvisa.utils.convert_volume_to_bucket.loop_over_directory(src_dir, tgt_dir, mask_dir, mesh, distmap)

Loops conversion over input directory

deep_folding.brainvisa.utils.convert_volume_to_bucket.main(argv)

Reads argument line and creates cropped files and pickle file

Parameters:argv – a list containing command line arguments
deep_folding.brainvisa.utils.convert_volume_to_bucket.parse_args(argv)

Parses command-line arguments

Parameters:argv – a list containing command line arguments
Returns:args
deep_folding.brainvisa.utils.convert_volume_to_bucket.read_convert_write(vol_filename, bucket_filename, mask_dir=None, distmap=False, mesh=False)

Reads volume, converts and writes back bucket.

Parameters:
  • [str] (bucket_filename) – path to input volume file
  • [str] – path to output bucket file

deep_folding.brainvisa.utils.dilate_mask module

deep_folding.brainvisa.utils.dilate_mask.dilate(mask, radius=10.0)
deep_folding.brainvisa.utils.dilate_mask.main()

deep_folding.brainvisa.utils.distmap module

Utilities to generate distance maps from skeleton files

deep_folding.brainvisa.utils.distmap.generate_distmap_from_resampled_skeleton(skeleton_file: str, distmap_file: str)

Generates distmap from resampled skeleton file.

deep_folding.brainvisa.utils.distmap.generate_distmap_from_skeleton_file(skeleton_file: str, distmap_file: str)

Generates distmap from skeleton file. Distmaps files are padded to avoid 0-background close to skeleton voxels when going to ICBMc referential. /!skeleton files have various dimensions

deep_folding.brainvisa.utils.folder module

deep_folding.brainvisa.utils.folder.create_folder(folder: str) → None

Creates folder if it doesn’t exist

deep_folding.brainvisa.utils.foldlabel module

Utilities to generate foldlabels from graph

deep_folding.brainvisa.utils.foldlabel.generate_foldlabel_from_graph(graph: soma.aims.Graph, junction: str = 'thin') → <function Volume at 0x7f32bb7240d0>

Generates foldlabel from graph

deep_folding.brainvisa.utils.foldlabel.generate_foldlabel_from_graph_file(graph_file: str, foldlabel_file: str, junction: str = 'thin')

Generates skeleton from graph file

deep_folding.brainvisa.utils.foldlabel.generate_foldlabel_thin_junction(graph: soma.aims.Graph) → <function Volume at 0x7f32bb7240d0>

Converts an aims graph into skeleton and foldlabel volumes

It should produce thin junctions as vertices (aims_ss, aims_bottom) are written after edges (junction, plidepassage). Thus, when voxels are present in both, simple and bottom surfaces override junctions

deep_folding.brainvisa.utils.foldlabel.generate_foldlabel_wide_junction(graph: soma.aims.Graph) → <function Volume at 0x7f32bb7240d0>

Converts an aims graph into skeleton and foldlabel volumes

It should produce wide junctions as edges (junction, plidepassage) are written after vertices (aims_ss, aims_bottom). Thus, when voxels are present in both, junction voxels override simple surface and bottom voxels

deep_folding.brainvisa.utils.graph module

Utilities on graphs

deep_folding.brainvisa.utils.graph.create_empty_volume_from_graph(graph: soma.aims.Graph) → <function Volume at 0x7f32bb7240d0>

Creates empty volume with graph header

deep_folding.brainvisa.utils.logs module

The aim of this script is to put together useful classes and functions used by brainvisa-dependent preprocessing for logging

class deep_folding.brainvisa.utils.logs.LogJson(json_file)

Bases: object

Handles json file lifecycle

Json file is created by overwriting old file. Upon loading a ne dictionary, it updates the json file by reading back and writing the updated content

create_file()

Creates json file and overwrites old content

update(dict_to_add)

Updates json file with new dictionary entry

Parameters:dict_to_add – dictionary appended to json file
write_general_info()

Writes general information on json

It contains information about generation date, git hash/version number.

deep_folding.brainvisa.utils.logs.log_command_line(args: argparse.Namespace, prog_name: str, tgt_dir: str, suffix: str = None) → None

Logs command on file command_line.sh in target directory

The command file gives thus the exact command line the should be given to reproduce the results

deep_folding.brainvisa.utils.logs.setup_log(args: argparse.Namespace, log_dir: str, prog_name: str, suffix: str = None) → None

Setups log for command line programs

deep_folding.brainvisa.utils.mask module

Getting bounding box from a list of sulci and specific hemisphere

The aim of this script is to output the bounding box of one or several sulci of a specified hemisphere.

deep_folding.brainvisa.utils.mask.compute_bbox_mask(arr)
deep_folding.brainvisa.utils.mask.compute_centered_mask(sulci_list, side, mask_dir='test/mask/1mm')

Function returning mask combining mask over several sulci

It reads mask files in the source mask directory and combines them. They are listed in subdirectory ‘L’ or ‘R’ according the hemisphere

Parameters:
  • sulci_list – a list of sulci
  • side – a string corresponding to the hemisphere, whether ‘L’ or ‘R’
  • mask_dir – path to source directory containing masks
Returns:

AIMS volume containing combined mask bbmin: an array of minimum coordinates of min box around the mask bbmax: an array of maximum coordinates of max_box around the mask

Return type:

mask_result

deep_folding.brainvisa.utils.mask.compute_intersection_mask(sulci_list, side, mask_dir='test/mask/1mm', dilation=5.0, threshold=0.0)

Function returning mask making intersection of masks over several sulci

It reads mask files in the source mask directory and combines them. They are listed in subdirectory ‘L’ or ‘R’ according the hemisphere

Parameters:
  • sulci_list – a list of sulci
  • side – a string corresponding to the hemisphere, whether ‘L’ or ‘R’
  • mask_dir – path to source directory containing masks
Returns:

AIMS volume containing combined mask bbmin: an array of minimum coordinates of min box around the mask bbmax: an array of maximum coordinates of max_box around the mask

Return type:

mask_result

deep_folding.brainvisa.utils.mask.compute_simple_mask(sulci_list, side, mask_dir='test/mask/1mm', dilation=5.0, threshold=0.0)

Function returning mask combining mask over several sulci

It reads mask files in the source mask directory and combines them. They are listed in subdirectory ‘L’ or ‘R’ according the hemisphere

Parameters:
  • sulci_list – a list of sulci
  • side – a string corresponding to the hemisphere, whether ‘L’ or ‘R’
  • mask_dir – path to source directory containing masks
Returns:

AIMS volume containing combined mask bbmin: an array of minimum coordinates of min box around the mask bbmax: an array of maximum coordinates of max_box around the mask

Return type:

mask_result

deep_folding.brainvisa.utils.mask.intersect_binary(a, b)

returns intersection of two binary arrays

deep_folding.brainvisa.utils.padding module

Padds a numpy array

Taken from: https://github.com/neurospin/pynet/blob/master/pynet/preprocessing/spatial.py

deep_folding.brainvisa.utils.padding.padd(arr, shape, fill_value=0)

Apply a padding. :param arr: the input data. :type arr: array :param shape: the desired shape. :type shape: list of int :param fill_value: the value used to fill the array. :type fill_value: int, default 0

Returns:transformed – the transformed input data.
Return type:array

deep_folding.brainvisa.utils.parallel module

The aim of this script is to put together useful classes and functions for parallel computing

deep_folding.brainvisa.utils.parallel.define_njobs()

Returns number of cpus used by main loop

deep_folding.brainvisa.utils.quality_checks module

Utilities to perform quality checks

deep_folding.brainvisa.utils.quality_checks.compare_number_aims_files_with_expected(output_dir: str, list_subjects: list)

Compares number of generated files and expected number

deep_folding.brainvisa.utils.quality_checks.compare_number_aims_files_with_number_in_source(output_dir: str, src_dir: str)

Compares number of generated files and source files

deep_folding.brainvisa.utils.quality_checks.get_not_processed_cropped_files(src_dir, tgt_dir)

Returns list of source files not yet processed.

this one is specific toc rop directory. This is done by comparing subjects in src and tgt directories

deep_folding.brainvisa.utils.quality_checks.get_not_processed_files(src_dir, tgt_dir, src_filename)

Returns list of source files not yet processed.

This is done by comparing subjects in src and tgt directories

deep_folding.brainvisa.utils.quality_checks.get_not_processed_subjects(src_subjects, tgt_dir, prefix='generated_')

Returns list of source files not yet processed.

This is done by comparing subjects in src and tgt directories

deep_folding.brainvisa.utils.quality_checks.get_not_processed_subjects_dict(subjects, tgt_dir)

Returns list of subjects not yet processed.

This is done by comparing subjects in subject dict and tgt directories

deep_folding.brainvisa.utils.quality_checks.get_not_processed_subjects_distmap(src_subjects, tgt_dir, prefix='generated_')

Returns list of source files not yet processed.

This is done by comparing subjects in src and tgt directories

deep_folding.brainvisa.utils.quality_checks.get_not_processed_subjects_transform(src_subjects, tgt_dir, prefix='ICBM2009c_')

Returns list of source files not yet processed.

This is done by comparing subjects in src and tgt directories

deep_folding.brainvisa.utils.quality_checks.save_list_to_csv(not_processed_files, csv_file_name)

Saves list of not_processed files to csv

deep_folding.brainvisa.utils.referentials module

deep_folding.brainvisa.utils.referentials.ICBM2009c_to_aims_talairach(point_ICBM2009c: <built-in function array>) → <built-in function array>

Transforms coordinates from ICBM2009c to AIMS talairach referential

deep_folding.brainvisa.utils.referentials.generate_ref_volume_ICBM2009c(out_voxel_size: tuple) → <function Volume at 0x7f32bb7240d0>

Defines MNI 2009 reference aims volume with output voxel size

Parameters:output_voxel_size – tuple Output voxel size (default: None, no resampling)
Returns:
volume (aims.Volume_S16) filled with 0 in MNI2009 referential
and with requested voxel_size
Return type:vol

deep_folding.brainvisa.utils.remove_hull module

Removing hull voxels from skeletons’ crops

The aim of this script is to remove skeleton’s voxels that correspond to the hull and to use this new volume for visualization if needed.

class deep_folding.brainvisa.utils.remove_hull.DatasetHullRemoved(src_dir='/nfs/neurospin/dico/data/deep_folding/data/crops/SC/sulcus_based/2mm/', tgt_dir='/neurospin/dico/data/deep_folding/data/crops/SC/sulcus_based/2mm/meshes/', side='R', number_subjects=-1, list_subjects=None, file_subjects=None)

Bases: object

Generates meshes of crops without hull

create_meshes()

Creates meshes from skeleton crops (.nii files)

The programm loops over all subjects from the input (source) directory. :param number_subjects: integer giving the number of subjects to analyze,

by default it is set to _ALL_SUBJECTS (-1).
create_one_mesh(subject_id)

Creates one mesh from a skeleton crop (.nii file)

Parameters:subject_id – string giving the subject ID
deep_folding.brainvisa.utils.remove_hull.convert_volume_to_bucket(vol)

Converts volume to bucket

Parameters:arr – numpy array
deep_folding.brainvisa.utils.remove_hull.create_one_mesh(vol, padding=1, ext=1, threshold=12)

Creates

Parameters:vol – aims volume
deep_folding.brainvisa.utils.remove_hull.define_njobs()

Returns number of cpus used by main loop

deep_folding.brainvisa.utils.remove_hull.main(argv)
deep_folding.brainvisa.utils.remove_hull.parse_args(argv)

Function parsing command-line arguments

Parameters:argv – a list containing command line arguments
Returns:dictionary with keys: src_dir, tgt_dir, nb_subjects, list_sulci
Return type:params
deep_folding.brainvisa.utils.remove_hull.remove_hull(arr, padding=1, ext=1)

Removes the pixels on the hull.

Pixels on the hull are defined as being in contact with both the internal and external part. This function removes the hull in place

Parameters:
  • arr – numpy array modified in the array
  • padding – padding of the image, equal to the extension ext
  • ext – local array extension in which to look for extern/internal pixels
deep_folding.brainvisa.utils.remove_hull.threshold_and_binarize(arr, threshold=12)

Threshold images

Parameters:arr – numpy array

deep_folding.brainvisa.utils.resample module

Resample a volume that contains discret values

deep_folding.brainvisa.utils.resample.resample(input_image: typing.Union[str, Volume], transformation: typing.Union[str, soma.aims.aims.AffineTransformation3d], output_vs: tuple = None, background: int = 0, values: <built-in function array> = None, verbose: bool = False) → <function Volume at 0x7f32bb7240d0>

Transforms and resamples a volume that has discret values

Parameters:
  • input_image (path to nifti file or aims volume) – Path to the input volume (.nii or .nii.gz file)
  • transformation (path to file) – Linear transformation file (.trm file)
  • output_vs (tuple) – Output voxel size (default: None, no resampling)
  • background (int) – Background value (default: 0)
  • values ([]) – Array of unique values ordered by ascendent priority without background. If not given, priority is set by ascendent values
Returns:

Transformed and resampled volume

Return type:

resampled

deep_folding.brainvisa.utils.save_data module

Scripts that enables to create a dataframe of numpy arrays from .nii.gz or .nii images.

deep_folding.brainvisa.utils.save_data.compare_array_aims_files(subjects, arr, cropped_dir, parallel=True)

Compares numpy arrays to subject nifti files

deep_folding.brainvisa.utils.save_data.compare_one_array(cropped_dir, list_basename, row)
deep_folding.brainvisa.utils.save_data.get_one_numpy_array(filename, cropped_dir)
deep_folding.brainvisa.utils.save_data.is_file_nii(filename)

Tests if file is nii file

Parameters:filename – string giving file name with full path
Returns:boolean stating if file is nii file
Return type:is_file_nii
deep_folding.brainvisa.utils.save_data.quality_checks(csv_file_path, npy_array_file_path, cropped_dir, parallel=False)

Checks that the numpy arrays are equal to subject nifti files.

This is to check that the subjects list in csv file match the order set in numpy arrays

deep_folding.brainvisa.utils.save_data.save_to_dataframe_format_from_list(cropped_dir, tgt_dir=None, file_basename=None, list_sample_id=None, list_sample_file=None)

Creates a dataframe of data with a column for each subject and associated np.array. Saved these this dataframe to pkl format on the target directory

Parameters:
  • cropped_dir – directory containing cropped images
  • tgt_dir – directory where to save the pickle file
  • file_basename – final file name = file_basename.pkl
deep_folding.brainvisa.utils.save_data.save_to_numpy(cropped_dir, tgt_dir=None, file_basename=None, parallel=False)

Creates a numpy array for each subject.

Saved these this dataframe to npy format on the target directory

Parameters:
  • cropped_dir – directory containing cropped images
  • tgt_dir – directory where to save the numpy array file
  • file_basename – final file name = file_basename.npy
deep_folding.brainvisa.utils.save_data.save_to_pickle(cropped_dir, tgt_dir=None, file_basename=None, parallel=False)

Creates a dataframe of data with a column for each subject and associated np.array. Saved these this dataframe to pkl format on the target directory

Parameters:
  • cropped_dir – directory containing cropped images
  • tgt_dir – directory where to save the pickle file
  • file_basename – final file name = file_basename.pkl

deep_folding.brainvisa.utils.skeleton module

Utilities to generate skeletons from graph

deep_folding.brainvisa.utils.skeleton.generate_skeleton_from_graph(graph: soma.aims.Graph, junction: str = 'thin') → <function Volume at 0x7f32bb7240d0>

Generates skeleton from graph

deep_folding.brainvisa.utils.skeleton.generate_skeleton_from_graph_file(graph_file: str, skeleton_file: str, junction: str = 'thin')

Generates skeleton from graph file

deep_folding.brainvisa.utils.skeleton.generate_skeleton_thin_junction(graph: soma.aims.Graph) → <function Volume at 0x7f32bb7240d0>

Converts an aims graph into skeleton volumes

It should produce thin junctions as vertices (aims_ss, aims_bottom) are written after edges (junction, plidepassage). Thus, when voxels are present in both, simple and bottom surfaces override junctions

deep_folding.brainvisa.utils.skeleton.generate_skeleton_wide_junction(graph: soma.aims.Graph) → <function Volume at 0x7f32bb7240d0>

Converts an aims graph into skeleton volumes

It should produce wide junctions as edges (junction, plidepassage) are written after vertices (aims_ss, aims_bottom). Thus, when voxels are present in both, junction voxels override simple surface and bottom voxels

deep_folding.brainvisa.utils.skeleton.is_skeleton(arr)

checks if arr is a skeleton

deep_folding.brainvisa.utils.subjects module

deep_folding.brainvisa.utils.subjects.get_all_subjects_as_dictionary(src_dir_list, graph_file_list, side)

Lists all subjects from the database (directory src_dir).

Subjects are the names of the subdirectories of the root directory.

Returns:a list of dictionaries containing all subjects as dict
Return type:subjects
deep_folding.brainvisa.utils.subjects.get_number_subjects(nb_subjects: str) → int

Returns nb_subjects as int

If it is “all”, it returns -1

Parameters:nb_subjects – string giving nb of subjects (“all” if all subjects)
Returns:int giving nb of subjects (-1 if all subjects)
Return type:nb_subjects_int
deep_folding.brainvisa.utils.subjects.is_it_a_subject(filename)
deep_folding.brainvisa.utils.subjects.select_good_qc(orig_list: list, qc_path: str)

Return the sublist of orig_list where all data with bad qc are removed. /!Also removes subjects that are not mentioned in the qc file.

Parameters:
  • orig_list – list of strings, the origin list of subjects
  • qc_path – string giving the file path with the relevant information. It is a .csv (or .tsv) with a ‘participant_id’ and ‘qc’ columns. qc values are supposed to be either 0 or 1. If qc_path is set to None, then no QC are applied.
Returns:

list of strings, being the subjects with acceptable qc.

Return type:

subjects

deep_folding.brainvisa.utils.subjects.select_subjects(orig_list: list, nb_subjects: str) → list

Returns a sublist of nb_subjects elements

if nb_subjects == “all”, it returns the original list Otherwise it returns the nb_subjects first elements of orig_list

Parameters:
  • orig_list – list of strings, the origin list of subjects
  • nb_subjects – string giving nb of subjects (“all” if all subjects)
Returns:

list of strings, being the select number of subjects

Return type:

sublist

deep_folding.brainvisa.utils.subjects.select_subjects_int(orig_list: list, nb_subjects: int) → list

Returns a sublist of nb_subjects elements

if nb_subjects == -1, it returns the original list

Parameters:
  • orig_list – list of strings, the origin list of subjects
  • nb_subjects – intgiving nb of subjects (-1 if all subjects)
Returns:

list of strings, being the select number of subjects

Return type:

sublist

deep_folding.brainvisa.utils.sulcus module

Getting complete sulci names based on generic name and side

The aim of this script is to output the complete name of sulci. For instance, giving “S.T.s.ter.asc.ant” and right side, script outputs “S.T.s.ter.asc.ant._right”

deep_folding.brainvisa.utils.sulcus.complete_sulci_name(sulci_list, side)

Function gathering sulci and side to obtain full name of sulci

It reads suli prefixes from a list and adds a suffix depending on a given side.

Parameters:
  • sulci_list – a list of sulci
  • side – a string corresponding to the hemisphere, whether ‘L’ or ‘R’
Returns:

a list with full sulci names, ie with side included

Return type:

full_sulci_list

deep_folding.brainvisa.utils.suppress_files_from_csv module

This program converts volumes contained in a folder into buckets. It writes bucket files in the output folder

deep_folding.brainvisa.utils.suppress_files_from_csv.main(argv)

Reads argument line and creates cropped files and pickle file

Parameters:argv – a list containing command line arguments
deep_folding.brainvisa.utils.suppress_files_from_csv.parse_args(argv)

Parses command-line arguments

Parameters:argv – a list containing command line arguments
Returns:args
deep_folding.brainvisa.utils.suppress_files_from_csv.suppress(csv_file_name)

Suppress files listed in csv

deep_folding.brainvisa.utils.write_distance_map module

Write distance maps from skeleton files

Typical usage

You can use this program by first entering in the brainvisa environment (here brainvisa 5.0.0 installed with singurity) and launching the script from the terminal: >>> bv bash >>> python write_distance_map.py

deep_folding.brainvisa.utils.write_distance_map.build_distMap_filename(subject, tgt_dir)

Returns bucket filename

deep_folding.brainvisa.utils.write_distance_map.get_subject_name(filename)

Returns file basename without extension

deep_folding.brainvisa.utils.write_distance_map.loop_over_directory(src_dir, tgt_dir)

Loops conversion over input directory

deep_folding.brainvisa.utils.write_distance_map.parse_args(argv)

Parses command-line arguments

Parameters:argv – a list containing command line arguments
Returns:args
deep_folding.brainvisa.utils.write_distance_map.skel_2_distMap(subject)

Reads volume, converts and writes back bucket.

Parameters:
  • [str] (bucket_filename) – path to input volume file
  • [str] – path to output bucket file

Module contents