def test_no_soma(): rdw = swc.read(os.path.join(SWC_PATH, 'Single_apical_no_soma.swc'), has_soma=False) nt.eq_(rdw.fmt, 'SWC') nt.eq_(len(rdw.data_block), 211) nt.eq_(np.shape(rdw.data_block), (211, 7)) nt.assert_not_equal(rdw.data_block[0][4], 0)
def test_read_split_soma(): rdw = swc.read(os.path.join(SWC_PATH, 'split_soma_single_neurites.swc')) nt.eq_(rdw.neurite_root_section_ids(), [1, 3]) nt.eq_(len(rdw.soma_points()), 3) nt.eq_(len(rdw.sections), 4) ref_ids = [[-1, 0], [0, 1, 2, 3, 4], [0, 5, 6], [6, 7, 8, 9, 10], []] for s, r in zip(rdw.sections, ref_ids): nt.eq_(s.ids, r)
def test_read_contour_soma_neuron(): rdw = swc.read(os.path.join(SWC_SOMA_PATH, 'contour_soma_neuron.swc')) nt.eq_(rdw.neurite_root_section_ids(), [3, 4, 5]) nt.eq_(len(rdw.soma_points()), 8) nt.eq_(len(rdw.sections), 7) # includes one empty section ref_ids = [[-1, 0, 1, 2], [2, 3, 4, 5], [5, 6, 7], [2, 8, 9, 10, 11, 12], [5, 13, 14, 15, 16, 17], [7, 18, 19, 20, 21, 22], []] for s, r in zip(rdw.sections, ref_ids): nt.eq_(s.ids, r)
def test_read_contour_split_soma_neuron(): rdw = swc.read(os.path.join(SWC_SOMA_PATH, 'contour_split_soma_neuron.swc')) nt.eq_(rdw.neurite_root_section_ids(), [1, 4, 5]) nt.eq_(len(rdw.soma_points()), 8) nt.eq_(len(rdw.sections), 7) # includes one empty section ref_ids = [[-1, 0, 1, 2], [2, 3, 4, 5, 6, 7], [2, 8, 9, 10], [10, 11, 12], [10, 13, 14, 15, 16, 17], [12, 18, 19, 20, 21, 22], []] for s, r in zip(rdw.sections, ref_ids): nt.eq_(s.ids, r)
def main(conn, argv): parser = argparse.ArgumentParser() parser.add_argument("image", help="Image ID") args = parser.parse_args(argv) image_id = args.image data = swc.read( "20200628-ftp/RL_35_guassian_4_4_80.tif_x94_y327_z241_app2_(GSBT).swc" ).data_block # img_name = 'FADU_tumour_Lectin_substack_deconvolved_RL_35iters_guassian_psf_4_4_80.tif' image = conn.getObject("Image", image_id) # delete existing ROIs roi_service = conn.getRoiService() result = roi_service.findByImage(image.id, None) roi_ids = [roi.id.val for roi in result.rois] if roi_ids: print("Deleting ROIs...") conn.deleteObjects("Roi", roi_ids, wait=True) size_y = image.getSizeY() paths = parse_swc(data, size_y) red_int = int.from_bytes([255, 0, 0, 255], byteorder="big", signed=True) for count, path in enumerate(paths): points = [] for zyx in path: z, y, x = zyx point = PointI() point.x = rdouble(x) point.y = rdouble(y) point.theZ = rint(round(z)) point.strokeColor = rint(red_int) points.append(point) print(f"{count}/{len(paths)} Creating ROI with {len(points)} points") create_roi(image, points)
import napari from neurom.io import swc import numpy as np def parse_swc(data): break_points = [0] + list(np.nonzero(np.diff(data[:, 6]) < 0)[0]+1) + [len(data)-1] paths = [] for i in range(len(break_points)-1): if break_points[i+1] - break_points[i] > 2: paths.append(data[break_points[i]:break_points[i+1], :3]) return paths # def parse_swc_extra(data): # break_points = [0] + list(np.nonzero(np.diff(data[:, 6]) < 0)[0]+1) + [len(data)-1] # paths = [] # for i in range(1, len(break_points)-1): # if break_points[i+1] - break_points[i] > 2: # paths.append(data[[break_points[i-1], break_points[i]], :3]) # return paths data = swc.read('data/neuromorphology/204-2-6nj.CNG.swc').data_block paths = parse_swc(data) # extra_paths = parse_swc_extra(data) with napari.gui_qt(): # create an empty viewer viewer = napari.view_shapes(paths, shape_type='path', edge_color='blue', ndisplay=3) # viewer.add_shapes(extra_paths, shape_type='path', # edge_color='red')
def test_simple_reversed(): rdw = swc.read(os.path.join(SWC_PATH, 'simple_reversed.swc')) nt.eq_(rdw.neurite_root_section_ids(), [5, 6]) nt.eq_(len(rdw.soma_points()), 1) nt.eq_(len(rdw.sections), 7)
def test_read_single_neurite(): rdw = swc.read(os.path.join(SWC_PATH, 'point_soma_single_neurite.swc')) nt.eq_(rdw.neurite_root_section_ids(), [1]) nt.eq_(len(rdw.soma_points()), 1) nt.eq_(len(rdw.sections), 2)
def test_custom_type(): rdw = swc.read(os.path.join(SWC_PATH, 'custom_type.swc')) nt.eq_(rdw.fmt, 'SWC') nt.eq_(len(rdw.data_block), 53) nt.eq_(np.shape(rdw.data_block), (53, 7)) nt.ok_(rdw.data_block[:, 4].any() <= 4 and rdw.data_block[:, 4].any() >= 0)
def setup(self): self.data = swc.read( os.path.join(SWC_PATH, 'sequential_trunk_off_42_16pt.swc')) self.first_id = int(self.data.data_block[0][COLS.ID])
def test_read_swc_basic_with_offset_42(): """ID numbering starting at 42.""" rdw = swc.read(os.path.join(SWC_PATH, 'sequential_trunk_off_42_16pt.swc')) nt.eq_(rdw.fmt, 'SWC') nt.eq_(len(rdw.data_block), 16) nt.eq_(np.shape(rdw.data_block), (16, 7))
def test_read_swc_basic(): rdw = swc.read(os.path.join(SWC_PATH, 'random_trunk_off_0_16pt.swc')) check_single_section_random_swc(rdw.data_block, rdw.fmt)
def test_consistency_between_h5_swc(): h5_data = hdf5.read(os.path.join(H5V1_PATH, 'Neuron.h5'), remove_duplicates=True) swc_data = swc.read(os.path.join(SWC_PATH, 'Neuron.swc')) nt.ok_(np.allclose(h5_data.data_block.shape, swc_data.data_block.shape))
def test_read_swc_basic(): rdw = swc.read( os.path.join(SWC_PATH, 'random_trunk_off_0_16pt.swc')) check_single_section_random_swc(rdw.data_block, rdw.fmt)
def test_read_swc_basic_with_offset_1(): """More normal ID numbering, starting at 1.""" rdw = swc.read(Path(SWC_PATH, 'sequential_trunk_off_1_16pt.swc')) nt.eq_(rdw.fmt, 'SWC') nt.eq_(len(rdw.data_block), 16) nt.eq_(np.shape(rdw.data_block), (16, 7))
def test_consistency_between_h5_swc(): h5_data = hdf5.read(Path(H5V1_PATH, 'Neuron.h5'), remove_duplicates=True) swc_data = swc.read(Path(SWC_PATH, 'Neuron.swc')) nt.ok_(np.allclose(h5_data.data_block.shape, swc_data.data_block.shape))
def test_read_swc_basic_with_offset_0(): """first ID = 0, rare to find.""" rdw = swc.read(os.path.join(SWC_PATH, 'sequential_trunk_off_0_16pt.swc')) nt.eq_(rdw.fmt, 'SWC') nt.eq_(len(rdw.data_block), 16) nt.eq_(np.shape(rdw.data_block), (16, 7))
def read_swc(filename): '''Lazy loading of SWC reader''' from neurom.io import swc from .swc import SWCDataWrapper return swc.read(filename, data_wrapper=SWCDataWrapper)
def test_read_swc_basic_with_offset_0(): '''first ID = 0, rare to find''' rdw = swc.read(os.path.join(SWC_PATH, 'sequential_trunk_off_0_16pt.swc')) nt.eq_(rdw.fmt, 'SWC') nt.eq_(len(rdw.data_block), 16) nt.eq_(np.shape(rdw.data_block), (16, 7))
def test_read_swc_basic_with_offset_42(): '''ID numbering starting at 42''' rdw = swc.read(os.path.join(SWC_PATH, 'sequential_trunk_off_42_16pt.swc')) nt.eq_(rdw.fmt, 'SWC') nt.eq_(len(rdw.data_block), 16) nt.eq_(np.shape(rdw.data_block), (16, 7))