cmtklib.util module

Module that defines CMTK Utility functions.

class cmtklib.util.BColors[source]

Bases: object

Utility class for color unicode.

BOLD = '\x1b[1m'
ENDC = '\x1b[0m'
FAIL = '\x1b[91m'
HEADER = '\x1b[95m'
OKBLUE = '\x1b[94m'
OKGREEN = '\x1b[92m'
UNDERLINE = '\x1b[4m'
WARNING = '\x1b[93m'
cmtklib.util.bidsapp_2_local_bids_dir(local_dir, path, debug=True)[source]

Replace all path prefixes /bids_dir by the real local directory path.

Parameters
  • local_dir (string) – Local path to BIDS root directory

  • path (string) – Path where the prefix should be replaced

  • debug (Boolean) – If True, print the new path after replacement

Returns

new_path – Output path with prefix replaced

Return type

string

cmtklib.util.bidsapp_2_local_output_dir2(local_dir, path, debug=True)[source]

Replace all path prefixes /output_dir by the real local directory path.

Parameters
  • local_dir (string) – Local path to output / derivatives directory

  • path (string) – Path where the prefix should be replaced

  • debug (Boolean) – If True, print the new path after replacement

Returns

new_path – Output path with prefix replaced

Return type

string

cmtklib.util.create_results_plkz_local(plkz_file, local_output_dir, debug=True)[source]

Update path in pickle files generated by Nipype nodes.

Parameters
  • plkz_file (zipped pickle) – Pickle file generated by Nipype node

  • local_output_dir (string) – Local output / derivatives directory

  • debug (Boolean) – If True, print extra information

cmtklib.util.extract_freesurfer_subject_dir(reconall_report, local_output_dir=None)[source]

Extract Freesurfer subject directory from the report created by Nipype Freesurfer Recon-all node.

Parameters
  • reconall_report (string) – Path to the recon-all report

  • local_output_dir (string) – Local output / derivatives directory

Returns

fs_subject_dir – Freesurfer subject directory

Return type

string

cmtklib.util.force_decode(string, codecs=None)[source]

Force decoding byte string with a specific codec.

Parameters
  • string (bytes) – String in bytearray format to be decoded

  • codecs (list) – List of codecs to try to decode the encoded string

cmtklib.util.get_node_dictionary_outputs(node_report, local_output_dir=None)[source]

Read the Nipype node report and return a dictionary of node outputs.

Parameters
  • node_report (string) – Path to node report

  • local_output_dir (string) – Local output / derivatives directory

Returns

dict_outputs – dictionary of outputs extracted from node execution report

Return type

dict

cmtklib.util.get_pipeline_dictionary_outputs(datasink_report, local_output_dir=None)[source]

Read the Nipype datasink report and return a dictionary of pipeline outputs.

Parameters
  • datasink_report (string) – Path to the datasink report

  • local_output_dir (string) – Local output / derivatives directory

Returns

dict_outputs – Dictionary of pipeline outputs

Return type

dict

cmtklib.util.length(xyz, along=False)[source]

Euclidean length of track line.

Parameters
  • xyz (array-like shape (N,3)) – array representing x,y,z of N points in a track

  • along (bool, optional) – If True, return array giving cumulative length along track, otherwise (default) return scalar giving total length.

Returns

L – scalar in case of along == False, giving total length, array if along == True, giving cumulative lengths.

Return type

scalar or array shape (N-1,)

Examples

>>> xyz = np.array([[1,1,1],[2,3,4],[0,0,0]])
>>> expected_lens = np.sqrt([1+2**2+3**2, 2**2+3**2+4**2])
>>> length(xyz) == expected_lens.sum()
True
>>> len_along = length(xyz, along=True)
>>> np.allclose(len_along, expected_lens.cumsum())
True
>>> length([])
0
>>> length([[1, 2, 3]])
0
>>> length([], along=True)
array([0])
cmtklib.util.load_graphs(output_dir, subjects, parcellation_scheme, weight)[source]

Return a dictionary of connectivity matrices (graph adjacency matrices).

Still in development

Parameters
  • output_dir (string) – Output/derivatives directory

  • subjects (list) – List of subject

  • parcellation_scheme (['NativeFreesurfer','Lausanne2008','Lausanne2018']) – Parcellation scheme

  • weight (['number_of_fibers','fiber_density',..]) – Edge metric to extract from the graph

Returns

connmats – Dictionary of connectivity matrices

Return type

dict

cmtklib.util.magn(xyz, n=1)[source]

Returns the vector magnitude

Parameters
  • xyz (vector) – Input vector

  • n (int) – Tile by n if n>1 before return

cmtklib.util.mean_curvature(xyz)[source]

Calculates the mean curvature of a curve.

Parameters

xyz (array-like shape (N,3)) – array representing x,y,z of N points in a curve

Returns

m – float representing the mean curvature

Return type

float

Examples

Create a straight line and a semi-circle and print their mean curvatures

>>> from dipy.tracking import metrics as tm
>>> import numpy as np
>>> x=np.linspace(0,1,100)
>>> y=0*x
>>> z=0*x
>>> xyz=np.vstack((x,y,z)).T
>>> m=tm.mean_curvature(xyz) #mean curvature straight line
>>> theta=np.pi*np.linspace(0,1,100)
>>> x=np.cos(theta)
>>> y=np.sin(theta)
>>> z=0*x
>>> xyz=np.vstack((x,y,z)).T
>>> m=tm.mean_curvature(xyz) #mean curvature for semi-circle
cmtklib.util.return_button_style_sheet(image, icon_size, verbose=False)[source]

Return Qt style sheet for QPushButton with image

Parameters
  • image (string) – Path to image to use as icon

  • icon_size (int) – Image size

  • verbose (Bool) – Print the style sheet if True Default: False

Returns

button_style_sheet – Qt style sheet for QPushButton with image

Return type

string