def test_compute_csa_ellipse(dummy_segmentation): """Test computation of cross-sectional area from input segmentation""" metrics = process_seg.compute_csa(dummy_segmentation(shape='ellipse', angle=0, a=50.0, b=30.0), algo_fitting=PARAM.algo_fitting, angle_correction=True, use_phys_coord=False, verbose=VERBOSE) assert np.mean(metrics['csa'].data[30:70]) == pytest.approx(47.01, rel=0.01) assert np.mean(metrics['angle'].data[30:70]) == pytest.approx(0.0, rel=0.01)
def test_compute_csa_noangle(dummy_segmentation): """Test computation of cross-sectional area from input segmentation""" metrics = process_seg.compute_csa(dummy_segmentation(shape='rectangle', angle=0, a=50.0, b=30.0), algo_fitting='hanning', window_length=5, angle_correction=False, use_phys_coord=True, verbose=0) assert np.isnan(metrics['csa'].data[95]) assert np.mean(metrics['csa'].data[20:80]) == pytest.approx(61.61, rel=0.01) assert np.mean(metrics['angle'].data[20:80]) == pytest.approx(0.0, rel=0.01)
def test_compute_csa(dummy_segmentation): """Test computation of cross-sectional area from input segmentation Note: here, compared to the previous tests with no angle, we use smaller hanning window and smaller range for computing the mean, because the smoothing creates spurious errors at edges.""" metrics = process_seg.compute_csa(dummy_segmentation(shape='rectangle', angle=15, a=50.0, b=30.0), algo_fitting=PARAM.algo_fitting, angle_correction=True, use_phys_coord=False, verbose=VERBOSE) assert np.mean(metrics['csa'].data[30:70]) == pytest.approx( 61.61, rel=0.01) # theoretical: 61.61 assert np.mean(metrics['angle'].data[30:70]) == pytest.approx(15.00, rel=0.02)
def main(args): parser = get_parser() arguments = parser.parse(args) param = Param() # Initialization slices = param.slices angle_correction = True use_phys_coord = True group_funcs = (('MEAN', func_wa), ('STD', func_std)) # functions to perform when aggregating metrics along S-I fname_segmentation = sct.get_absolute_path(arguments['-i']) name_process = arguments['-p'] fname_vert_levels = '' if '-o' in arguments: file_out = os.path.abspath(arguments['-o']) else: file_out = '' if '-append' in arguments: append = int(arguments['-append']) else: append = 0 if '-vert' in arguments: vert_levels = arguments['-vert'] else: vert_levels = '' if '-r' in arguments: remove_temp_files = arguments['-r'] if '-vertfile' in arguments: fname_vert_levels = arguments['-vertfile'] if '-perlevel' in arguments: perlevel = arguments['-perlevel'] else: perlevel = Param().perlevel if '-v' in arguments: verbose = int(arguments['-v']) if '-z' in arguments: slices = arguments['-z'] if '-perslice' in arguments: perslice = arguments['-perslice'] else: perslice = Param().perslice if '-a' in arguments: param.algo_fitting = arguments['-a'] if '-no-angle' in arguments: if arguments['-no-angle'] == '1': angle_correction = False elif arguments['-no-angle'] == '0': angle_correction = True if '-use-image-coord' in arguments: if arguments['-use-image-coord'] == '1': use_phys_coord = False if arguments['-use-image-coord'] == '0': use_phys_coord = True # update fields param.verbose = verbose metrics_agg = {} if not file_out: file_out = name_process + '.csv' if name_process == 'centerline': process_seg.extract_centerline(fname_segmentation, verbose=param.verbose, algo_fitting=param.algo_fitting, use_phys_coord=use_phys_coord, file_out=file_out) if name_process == 'csa': metrics = process_seg.compute_csa(fname_segmentation, algo_fitting=param.algo_fitting, type_window=param.type_window, window_length=param.window_length, angle_correction=angle_correction, use_phys_coord=use_phys_coord, remove_temp_files=remove_temp_files, verbose=verbose) for key in metrics: metrics_agg[key] = aggregate_per_slice_or_level(metrics[key], slices=parse_num_list(slices), levels=parse_num_list(vert_levels), perslice=perslice, perlevel=perlevel, vert_level=fname_vert_levels, group_funcs=group_funcs) metrics_agg_merged = merge_dict(metrics_agg) save_as_csv(metrics_agg_merged, file_out, fname_in=fname_segmentation, append=append) sct.printv('\nFile created: '+file_out, verbose=1, type='info') if name_process == 'label-vert': if '-discfile' in arguments: fname_discs = arguments['-discfile'] else: sct.printv('\nERROR: Disc label file is mandatory (flag: -discfile).\n', 1, 'error') process_seg.label_vert(fname_segmentation, fname_discs, verbose=verbose) if name_process == 'shape': fname_discs = None if '-discfile' in arguments: fname_discs = arguments['-discfile'] metrics = process_seg.compute_shape(fname_segmentation, remove_temp_files=remove_temp_files, verbose=verbose) for key in metrics: metrics_agg[key] = aggregate_per_slice_or_level(metrics[key], slices=parse_num_list(slices), levels=parse_num_list(vert_levels), perslice=perslice, perlevel=perlevel, vert_level=fname_vert_levels, group_funcs=group_funcs) metrics_agg_merged = merge_dict(metrics_agg) save_as_csv(metrics_agg_merged, file_out, fname_in=fname_segmentation, append=append) sct.printv('\nFile created: ' + file_out, verbose=1, type='info')