Skip to content

Commit

Permalink
Removed duplicate code, extraneous logger line
Browse files Browse the repository at this point in the history
  • Loading branch information
georgeaidinis committed Sep 13, 2023
1 parent db1c59e commit 063a018
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 42 deletions.
29 changes: 28 additions & 1 deletion niCHARTPipelines/CalculateROIVolume.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ def calc_roi_volumes(mrid, in_img_file, label_indices = []):

## Get label indices
if label_indices.shape[0] == 0:
logger.warning('Label indices not provided, generating from data')
# logger.warning('Label indices not provided, generating from data')
label_indices = u_ind

label_names = label_indices.astype(str)
Expand Down Expand Up @@ -99,3 +99,30 @@ def create_roi_csv(scan_id, in_roi, list_single_roi, map_derived_roi, out_img, o

## Write out csv
df_dmuse.to_csv(out_csv, index = False)

###---------calculate ROI volumes-----------
def extract_roi_masks(in_roi, map_derived_roi, out_pref):
'''Create individual roi masks for single and derived rois
'''
img_ext_type = '.nii.gz'

## Read image
in_nii = nib.load(in_roi)
img_mat = in_nii.get_fdata().astype(int)

## Read derived roi map file to a dictionary
roi_dict = {}
with open(map_derived_roi) as roi_map:
reader = csv.reader(roi_map, delimiter=',')
for row in reader:
key = str(row[0])
val = [int(x) for x in row[2:]]
roi_dict[key] = val

# Create an individual roi mask for each roi
for i, key in enumerate(roi_dict):
print(i)
key_vals = roi_dict[key]
tmp_mask = np.isin(img_mat, key_vals).astype(int)
out_nii = nib.Nifti1Image(tmp_mask, in_nii.affine, in_nii.header)
nib.save(out_nii, out_pref + '_' + str(key) + img_ext_type)
42 changes: 1 addition & 41 deletions niCHARTPipelines/MaskImage.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,47 +3,7 @@
from scipy import ndimage
from scipy.ndimage.measurements import label

## Find bounding box for the foreground values in img, with a given padding percentage
def calc_bbox_with_padding(img, perc_pad = 10):

img = img.astype('uint8')

## Output is the coordinates of the bounding box
bcoors = np.zeros([3,2], dtype=int)

## Find the largest connected component
## INFO: In images with very large FOV DLICV may have small isolated regions in
## boundaries; so we calculate the bounding box based on the brain, not all
## foreground voxels
str_3D = np.array([[[0, 0, 0], [0, 1, 0], [0, 0, 0]],
[[0, 1, 0], [1, 1, 1], [0, 1, 0]],
[[0, 0, 0], [0, 1, 0], [0, 0, 0]]], dtype='uint8')
labeled, ncomp = label(img, str_3D)
sizes = ndimage.sum(img, labeled, range(ncomp + 1))
img_largest_cc = (labeled == np.argmax(sizes)).astype(int)

## Find coors in each axis
for sel_axis in [0, 1, 2]:

## Get axes other than the selected
other_axes = [0, 1, 2]
other_axes.remove(sel_axis)

## Get img dim in selected axis
dim = img_largest_cc.shape[sel_axis]

## Find bounding box (index of first and last non-zero slices)
nonzero = np.any(img_largest_cc, axis = tuple(other_axes))
bbox= np.where(nonzero)[0][[0,-1]]

## Add padding
size_pad = int(np.round((bbox[1] - bbox[0]) * perc_pad / 100))
b_min = int(np.max([0, bbox[0] - size_pad]))
b_max = int(np.min([dim, bbox[1] + size_pad]))

bcoors[sel_axis, :] = [b_min, b_max]

return bcoors
from niCHARTPipelines.CombineMasks import calc_bbox_with_padding


###---------mask image-----------
Expand Down

0 comments on commit 063a018

Please sign in to comment.