Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Memory chokeholds #40

Open
mgxd opened this issue Mar 17, 2021 · 1 comment
Open

Memory chokeholds #40

mgxd opened this issue Mar 17, 2021 · 1 comment

Comments

@mgxd
Copy link
Collaborator

mgxd commented Mar 17, 2021

Some troublesome nodes to monitor. I have not been able to profile the specifics here yet.

  • aCompCor

    acompcor = pe.Node(
    ACompCor(components_file='acompcor.tsv', header_prefix='a_comp_cor_', pre_filter='cosine',
    save_pre_filter=True, save_metadata=True, mask_names=['CSF', 'WM', 'combined'],
    merge_method='none', failure_mode='NaN'),
    name="acompcor", mem_gb=8, nprocs=6) # TODO: Lessen expensive restrictions

  • SignalExtraction

    signals = pe.Node(SignalExtraction(class_labels=signals_class_labels),
    name="signals", mem_gb=8, n_procs=6) # TODO: Lessen expensive restrictions

  • ROIsPlot

    rois_plot = pe.Node(ROIsPlot(colors=['b', 'magenta'], generate_report=True),
    name='rois_plot', mem_gb=4) # TODO: Lessen expensive restrictions

When all run in parallel, the workflow crashes, likely due to memory. For now, the bandaid fix is to set n_procs to something fairly high and avoid running concurrently.

@mgxd
Copy link
Collaborator Author

mgxd commented Mar 31, 2021

another troublesome interface: sdcflows.interfaces.bspline.Coefficients2Warp

memory_profiler
Line #    Mem usage    Increment  Occurences   Line Contents
============================================================
    11     57.5 MiB     57.5 MiB           1   @profile
    12                                         def coeff():
    13     57.5 MiB      0.0 MiB           1       import os
    14     59.3 MiB      1.8 MiB           1       from scipy.sparse import vstack as sparse_vstack
    15     59.3 MiB      0.0 MiB           1       import numpy as np
    16     59.3 MiB      0.0 MiB           1       import nibabel as nb
    17     59.3 MiB      0.0 MiB           1       from nipype.utils.filemanip import fname_presuffix
    18     59.3 MiB      0.0 MiB           1       from sdcflows.interfaces.bspline import grid_bspline_weights
    19                                         
    20                                             # Calculate the physical coordinates of target grid
    21     59.4 MiB      0.2 MiB           1       targetnii = nb.load(in_target)
    22     62.8 MiB      3.4 MiB           1       allmask = np.ones_like(targetnii.dataobj, dtype="uint8")
    23                                         
    24     62.8 MiB      0.0 MiB           1       weights = []
    25     62.8 MiB      0.0 MiB           1       coeffs = []
    26                                         
    27    339.0 MiB      0.0 MiB           2       for cname in in_coeff:
    28     62.8 MiB      0.0 MiB           1           coeff_nii = nb.load(cname)
    29    337.8 MiB    274.9 MiB           1           wmat = grid_bspline_weights(targetnii, coeff_nii)
    30    337.8 MiB      0.0 MiB           1           weights.append(wmat)
    31    339.0 MiB      1.2 MiB           1           coeffs.append(coeff_nii.get_fdata(dtype="float32").reshape(-1))
    32                                         
    33    342.0 MiB      3.0 MiB           1       data = np.zeros(targetnii.shape, dtype="float32")
    34    389.5 MiB     47.4 MiB           1       data[allmask == 1] = np.squeeze(np.vstack(coeffs).T) @ sparse_vstack(weights)
    35                                         
    36    389.7 MiB      0.3 MiB           1       hdr = targetnii.header.copy()
    37    389.8 MiB      0.1 MiB           1       hdr.set_data_dtype("float32")
    38    390.0 MiB      0.1 MiB           2       out_field = fname_presuffix(
    39    389.8 MiB      0.0 MiB           1           in_target, suffix="_field", newpath=os.getcwd(),
    40                                             )
    41    391.7 MiB      1.7 MiB           2       targetnii.__class__(data, targetnii.affine, hdr).to_filename(
    42    390.8 MiB      0.0 MiB           1           out_field
    43                                             )
    44                                         
    45                                             # Generate warp field
    46    391.7 MiB      0.0 MiB           1       phaseEncDim = "ijk".index(pe_dir[0])
    47    391.7 MiB      0.0 MiB           1       phaseEncSign = [1.0, -1.0][len(pe_dir) != 2]
    48                                         
    49    391.7 MiB      0.0 MiB           1       data *= phaseEncSign * ro_time
    50                                         
    51    391.7 MiB      0.0 MiB           1       fieldshape = tuple(list(data.shape[:3]) + [3])
    52    391.7 MiB      0.0 MiB           2       out_warp = fname_presuffix(
    53    391.7 MiB      0.0 MiB           1           in_target, suffix="_xfm", newpath=os.getcwd(),
    54                                             )
    55                                             # Compose a vector field
    56    400.6 MiB      8.9 MiB           1       field = np.zeros((data.size, 3), dtype="float32")
    57    400.6 MiB      0.0 MiB           1       field[..., phaseEncDim] = data.reshape(-1)
    58    400.6 MiB      0.0 MiB           1       aff = targetnii.affine.copy()
    59    400.6 MiB      0.0 MiB           1       aff[:3, 3] = 0.0
    60                                             # Multiplying by the affine implicitly applies the voxel size to the shift map
    61    438.1 MiB     37.5 MiB           1       field = nb.affines.apply_affine(aff, field).reshape(fieldshape)
    62    438.8 MiB      0.7 MiB           2       warpnii = targetnii.__class__(
    63    438.1 MiB      0.0 MiB           1           field[:, :, :, np.newaxis, :].astype("float32"), targetnii.affine, None
    64                                             )
    65    438.8 MiB      0.0 MiB           1       warpnii.header.set_intent("vector", (), "")
    66    438.8 MiB      0.0 MiB           1       warpnii.header.set_xyzt_units("mm")
    67    445.9 MiB      7.0 MiB           1       warpnii.to_filename(out_warp)
    68                                         
    69    445.9 MiB      0.0 MiB           1       return out_field, out_warp

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

1 participant