Skip to content

Commit

Permalink
Merge branch 'master' into for_beluga_scilpy2
Browse files Browse the repository at this point in the history
  • Loading branch information
EmmaRenauld committed May 17, 2024
2 parents 1e4860b + 6266c2a commit b84da8c
Show file tree
Hide file tree
Showing 44 changed files with 1,758 additions and 655 deletions.
3 changes: 1 addition & 2 deletions dwi_ml/data/dataset/multi_subject_containers.py
Original file line number Diff line number Diff line change
Expand Up @@ -281,7 +281,6 @@ def load(self, hdf_handle: h5py.File, subj_id=None):
hdf_handle, subj_id, ref_group_info)

# Add subject to the list
logger.debug(" Adding it to the list of subjects.")
subj_idx = self.subjs_data_list.add_subject(subj_data)

# Arrange streamlines
Expand All @@ -290,7 +289,6 @@ def load(self, hdf_handle: h5py.File, subj_id=None):
if subj_data.is_lazy:
subj_data.add_handle(hdf_handle)

logger.debug(" Counting streamlines")
for group in range(len(self.streamline_groups)):
subj_sft_data = subj_data.sft_data_list[group]
n_streamlines = len(subj_sft_data)
Expand All @@ -302,6 +300,7 @@ def load(self, hdf_handle: h5py.File, subj_id=None):
subj_data.hdf_handle = None

# Arrange final data properties: Concatenate all subjects
logging.debug("All subjects added. Final verifications.")
self.streamline_lengths_mm = \
[np.concatenate(lengths_mm[group], axis=0)
for group in range(len(self.streamline_groups))]
Expand Down
3 changes: 2 additions & 1 deletion dwi_ml/data/dataset/single_subject_containers.py
Original file line number Diff line number Diff line change
Expand Up @@ -110,7 +110,8 @@ def init_single_subject_from_hdf(
subject_mri_data_list.append(subject_mri_group_data)

for group in streamline_groups:
logger.debug(" Loading subject's streamlines")
logger.debug(" Loading streamlines group '{}'"
.format(group))
sft_data = SFTData.init_sft_data_from_hdf_info(
hdf_file[subject_id][group])
subject_sft_data_list.append(sft_data)
Expand Down
17 changes: 7 additions & 10 deletions dwi_ml/data/hdf5/hdf5_creation.py
Original file line number Diff line number Diff line change
Expand Up @@ -59,8 +59,8 @@ def format_filelist(filenames, enforce_presence, folder=None) -> List[str]:
return new_files


def _load_and_verify_file(filename: str, subj_input_path, group_name: str,
group_affine, group_res):
def _load_and_verify_file(filename: str, group_name: str, group_affine,
group_res):
"""
Loads a 3D or 4D nifti file. If it is a 3D dataset, adds a dimension to
make it 4D. Then checks that it is compatible with a given group based on
Expand All @@ -70,26 +70,22 @@ def _load_and_verify_file(filename: str, subj_input_path, group_name: str,
------
filename: str
File's name. Must be .nii or .nii.gz.
subj_input_path: Path
Path where to load the nifti file from.
group_name: str
Name of the group with which 'filename' file must be compatible.
group_affine: np.array
The loaded file's affine must be equal (or very close) to this affine.
group_res: np.array
The loaded file's resolution must be equal (or very close) to this res.
"""
data_file = subj_input_path.joinpath(filename)

if not data_file.is_file():
if not os.path.isfile(filename):
logging.debug(" Skipping file {} because it was not "
"found in this subject's folder".format(filename))
# Note: if args.enforce_files_presence was set to true, this
# case is not possible, already checked in
# create_hdf5_dataset.py.
return None

data, affine, res, _ = load_file_to4d(data_file)
data, affine, res, _ = load_file_to4d(filename)

if not np.allclose(affine, group_affine, atol=1e-5):
# Note. When keeping default options on tolerance, we have run
Expand Down Expand Up @@ -483,6 +479,7 @@ def _process_one_volume_group(self, group: str, subj_id: str,
else:
std_mask = np.logical_or(sub_mask_data, std_mask)

# Get the files and add the subject_dir as prefix.
file_list = self.groups_config[group]['files']
file_list = format_filelist(file_list, self.enforce_files_presence,
folder=subj_input_dir)
Expand All @@ -505,8 +502,8 @@ def _process_one_volume_group(self, group: str, subj_id: str,
for file_name in file_list[1:]:
logging.info(" - Processing file {}"
.format(os.path.basename(file_name)))
data = _load_and_verify_file(file_name, subj_input_dir, group,
group_affine, group_res)
data = _load_and_verify_file(file_name, group, group_affine,
group_res)

if std_option == 'per_file':
logging.info(' - Standardizing')
Expand Down
28 changes: 16 additions & 12 deletions dwi_ml/data/processing/space/neighborhood.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,15 +19,17 @@ def prepare_neighborhood_vectors(
neighborhood_type: str
Either the 'axes' option or the 'grid' option. See each method for a
description.
neighborhood_radius: Optional[int]
The radius. For the axes option: a radius of 1 = 7 neighbhors, a
radius of 2 = 13 . For the grid option: a radius of 1 = 27 neighbors,
a radius of 2 = 125.
neighborhood_resolution: Optional[float]
Resolution between each layer of neighborhood, in voxel space as compared
to the MRI data. Ex: 0.5 one neighborhood every half voxel.
Hint: Neighborhood's space will depend on this value. To convert from
mm to voxel world, you may use
neighborhood_radius: int
Required if neighborhood type is not None.
- axes: a radius of 1 = 7 neighbhors, a radius of 2 = 13, on two
concentric spheres.
- grid option: a radius of 1 = 27 neighbors, a radius of 2 = 125, on
two concentric cubes.
neighborhood_resolution: float
Required if neighborhood type is not None.
- 'axes': spacing between each concentric sphere.
- 'grid': resolution of the final grid-like neighborhood.
Hint: To convert from mm to voxel world, you may use
dwi_ml.data.processing.space.world_to_vox.convert_world_to_vox(
radius_mm, affine_mm_to_vox)
Expand Down Expand Up @@ -68,9 +70,11 @@ def prepare_neighborhood_vectors(

def get_neighborhood_vectors_axes(radius: int, resolution: float):
"""
This neighborhood definition lies on a sphere. Returns a list of 7
positions (current, up, down, left, right, behind, in front) at exactly
`resolution` (mm or voxels) from origin (i.e. current postion).
This neighborhood definition lies on a sphere.
For radius = 1, returns a list of 7 positions (current, up, down, left,
right, behind, in front) at exactly `resolution` (mm or voxels) from origin
(i.e. current postion).
If radius is > 1, returns a multi-radius neighborhood (lying on
concentring spheres).
Expand Down
10 changes: 6 additions & 4 deletions dwi_ml/experiment_utils/prints.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,17 @@
from collections.abc import Mapping


def format_dict_to_str(d, indent=1):
def format_dict_to_str(d, indent=1, keys_only=False):
indentation = indent * " "
return ("\n" + indentation) + ("\n" + indentation).join(
"{!r}: {},".format(k, _format_val_to_str(v, indent+1))
"{!r}: {},".format(k, _format_val_to_str(v, indent+1, keys_only))
for k, v in d.items())


def _format_val_to_str(v, indent):
def _format_val_to_str(v, indent, keys_only=False):
if isinstance(v, Mapping):
return format_dict_to_str(v, indent)
return format_dict_to_str(v, indent, keys_only)
elif keys_only:
return ''
else:
return v
Loading

0 comments on commit b84da8c

Please sign in to comment.