Commit afe125ea authored by Michiel Cottaar's avatar Michiel Cottaar
Browse files

ENH: optionally only produce non-plot QC outputs

parent c15907c1
Pipeline #5132 failed with stage
in 0 seconds
...@@ -25,7 +25,7 @@ def faces_to_vertices(arr, surface: CorticalMesh): ...@@ -25,7 +25,7 @@ def faces_to_vertices(arr, surface: CorticalMesh):
def plot_all(surface: CorticalMesh, mask, nstream, basis: BasisFunc, def plot_all(surface: CorticalMesh, mask, nstream, basis: BasisFunc,
params=(), directory='.', skip_nsplit=False): params=(), directory='.', skip_nsplit=False, skip_plots=False):
""" """
Plots all the QC plots on the surface Plots all the QC plots on the surface
...@@ -36,6 +36,7 @@ def plot_all(surface: CorticalMesh, mask, nstream, basis: BasisFunc, ...@@ -36,6 +36,7 @@ def plot_all(surface: CorticalMesh, mask, nstream, basis: BasisFunc,
:param params: parameters :param params: parameters
:param directory: directory to store the plots :param directory: directory to store the plots
:param skip_nsplit: skip the (slow) nsplit plot if True :param skip_nsplit: skip the (slow) nsplit plot if True
:param skip_plots: skip all plots if True
""" """
masked_surface = surface[mask] masked_surface = surface[mask]
request = read_surface(masked_surface, nsplit=1) request = read_surface(masked_surface, nsplit=1)
...@@ -47,7 +48,7 @@ def plot_all(surface: CorticalMesh, mask, nstream, basis: BasisFunc, ...@@ -47,7 +48,7 @@ def plot_all(surface: CorticalMesh, mask, nstream, basis: BasisFunc,
expected = nstream / masked_surface.size_faces() expected = nstream / masked_surface.size_faces()
if not skip_nsplit: if not (skip_nsplit or skip_plots):
import matplotlib.pyplot as plt import matplotlib.pyplot as plt
fig, axes = plt.subplots(1, 1) fig, axes = plt.subplots(1, 1)
nsplit.plot_offset(request, basis, params, axes=axes) nsplit.plot_offset(request, basis, params, axes=axes)
...@@ -64,12 +65,13 @@ def plot_all(surface: CorticalMesh, mask, nstream, basis: BasisFunc, ...@@ -64,12 +65,13 @@ def plot_all(surface: CorticalMesh, mask, nstream, basis: BasisFunc,
header=cifti2.Cifti2Header.from_axes((scl, bm)) header=cifti2.Cifti2Header.from_axes((scl, bm))
).to_filename(op.join(directory, 'field.dscalar.nii')) ).to_filename(op.join(directory, 'field.dscalar.nii'))
for toplot, name, label in [ if not skip_plots:
(ncrossing / expected, 'density_crossing', r'$\rho$(streamlines crossing)'), for toplot, name, label in [
(density / expected, 'density_total', r'$\rho$(streamlines)'), (ncrossing / expected, 'density_crossing', r'$\rho$(streamlines crossing)'),
(radiality, 'radiality', 'radial alignment'), (density / expected, 'density_total', r'$\rho$(streamlines)'),
]: (radiality, 'radiality', 'radial alignment'),
plot_array(surface, mask, toplot, op.join(directory, name), label) ]:
plot_array(surface, mask, toplot, op.join(directory, name), label)
def plot_array(surface: CorticalMesh, mask, arr_faces, name, label): def plot_array(surface: CorticalMesh, mask, arr_faces, name, label):
......
...@@ -12,7 +12,7 @@ import nibabel as nib ...@@ -12,7 +12,7 @@ import nibabel as nib
def plot_all(mask_img: nib.Nifti1Image, dyads: nib.Nifti1Image, basis: BasisFunc, def plot_all(mask_img: nib.Nifti1Image, dyads: nib.Nifti1Image, basis: BasisFunc,
params=(), directory='.', skip_nsplit=False): params=(), directory='.', skip_nsplit=False, skip_plots=False):
""" """
Plots all the QC plots on the surface Plots all the QC plots on the surface
...@@ -22,6 +22,7 @@ def plot_all(mask_img: nib.Nifti1Image, dyads: nib.Nifti1Image, basis: BasisFunc ...@@ -22,6 +22,7 @@ def plot_all(mask_img: nib.Nifti1Image, dyads: nib.Nifti1Image, basis: BasisFunc
:param params: parameters :param params: parameters
:param directory: directory to store the plots :param directory: directory to store the plots
:param skip_nsplit: skip the (slow) nsplit plot if True :param skip_nsplit: skip the (slow) nsplit plot if True
:param skip_plots: skip all plots if True
""" """
mask = mask_img.get_data() > 0 mask = mask_img.get_data() > 0
request = read_volume(mask_img, nsplit=1) request = read_volume(mask_img, nsplit=1)
...@@ -30,7 +31,7 @@ def plot_all(mask_img: nib.Nifti1Image, dyads: nib.Nifti1Image, basis: BasisFunc ...@@ -30,7 +31,7 @@ def plot_all(mask_img: nib.Nifti1Image, dyads: nib.Nifti1Image, basis: BasisFunc
density = np.sqrt((field_voxels ** 2).sum(-1)) density = np.sqrt((field_voxels ** 2).sum(-1))
alignment = abs(np.sum(dyad_to_mm(dyads.get_fdata()[mask], mask_img.affine) * field_voxels, -1)) / density alignment = abs(np.sum(dyad_to_mm(dyads.get_fdata()[mask], mask_img.affine) * field_voxels, -1)) / density
if not skip_nsplit: if not (skip_nsplit or skip_plots):
import matplotlib.pyplot as plt import matplotlib.pyplot as plt
fig, axes = plt.subplots(1, 1) fig, axes = plt.subplots(1, 1)
nsplit.plot_offset(request, basis, params, axes=axes, nsplit_ref=3) nsplit.plot_offset(request, basis, params, axes=axes, nsplit_ref=3)
...@@ -40,10 +41,10 @@ def plot_all(mask_img: nib.Nifti1Image, dyads: nib.Nifti1Image, basis: BasisFunc ...@@ -40,10 +41,10 @@ def plot_all(mask_img: nib.Nifti1Image, dyads: nib.Nifti1Image, basis: BasisFunc
(density, 'density', 'N(streamlines)'), (density, 'density', 'N(streamlines)'),
(alignment, 'alignment', 'V1 alignment'), (alignment, 'alignment', 'V1 alignment'),
]: ]:
plot_array(mask_img, toplot, op.join(directory, name), label) plot_array(mask_img, toplot, op.join(directory, name), label, skip_plots=skip_plots)
def plot_array(img, arr, name, label): def plot_array(img, arr, name, label, skip_plots=False):
""" """
Produces QC plots for array on surface Produces QC plots for array on surface
...@@ -51,14 +52,16 @@ def plot_array(img, arr, name, label): ...@@ -51,14 +52,16 @@ def plot_array(img, arr, name, label):
:param arr: array evaluated on the faces of the surface :param arr: array evaluated on the faces of the surface
:param name: basename of the output file :param name: basename of the output file
:param label: text describing the array :param label: text describing the array
:param skip_plots: skip all plots if True
""" """
full_arr = np.zeros(img.shape, dtype=arr.dtype) full_arr = np.zeros(img.shape, dtype=arr.dtype)
full_arr[img.get_fdata() > 0] = arr full_arr[img.get_fdata() > 0] = arr
nib.Nifti1Image(full_arr, None, header=img.header).to_filename(f'{name}.nii.gz') nib.Nifti1Image(full_arr, None, header=img.header).to_filename(f'{name}.nii.gz')
import matplotlib.pyplot as plt if not skip_plots:
fig, axes = plt.subplots(1, 1) import matplotlib.pyplot as plt
axes.hist(arr, bins=51) fig, axes = plt.subplots(1, 1)
axes.set_xlabel(label) axes.hist(arr, bins=51)
axes.set_ylabel('N(voxels)') axes.set_xlabel(label)
fig.savefig(f'{name}_hist.png') axes.set_ylabel('N(voxels)')
fig.savefig(f'{name}_hist.png')
...@@ -19,6 +19,8 @@ parser.add_argument("-v", '--volume', nargs=2, action='append', help=( ...@@ -19,6 +19,8 @@ parser.add_argument("-v", '--volume', nargs=2, action='append', help=(
parser.add_argument("-d", '--dyads', help='Dyads to compare with in the volumetric masks') parser.add_argument("-d", '--dyads', help='Dyads to compare with in the volumetric masks')
parser.add_argument("-sn", "--skip_nsplit", action='store_true', parser.add_argument("-sn", "--skip_nsplit", action='store_true',
help='Skip producing the relatively slow `npslit` plot if set') help='Skip producing the relatively slow `npslit` plot if set')
parser.add_argument("-sp", "--skip_plots", action='store_true',
help='Skip producing any plots')
args = parser.parse_args() args = parser.parse_args()
from gyral_structure.basis import read from gyral_structure.basis import read
...@@ -53,6 +55,7 @@ for surf_name in ('white', 'midthickness', 'pial'): ...@@ -53,6 +55,7 @@ for surf_name in ('white', 'midthickness', 'pial'):
bf, params, bf, params,
out_directory, out_directory,
skip_nsplit=args.skip_nsplit, skip_nsplit=args.skip_nsplit,
skip_plots=args.skip_plots,
) )
if args.volume is not None: if args.volume is not None:
...@@ -65,4 +68,5 @@ if args.volume is not None: ...@@ -65,4 +68,5 @@ if args.volume is not None:
bf, params, bf, params,
out_directory, out_directory,
skip_nsplit=args.skip_nsplit, skip_nsplit=args.skip_nsplit,
skip_plots=args.skip_plots,
) )
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment