def test_tilt_correction_end_to_end(self): input = { 'swc_path': self.swc_path, 'marker_path': self.marker_path, 'ccf_soma_location': self.ccf_soma_location, 'slice_transform_list': self.slice_transform_list, 'slice_image_flip': self.slice_image_flip, 'ccf_path': self.ccf_path, 'output_json': self.output_json_path } cmd = [ 'python', '-m', 'neuron_morphology.transforms.tilt_correction.compute_tilt_correction' ] for key, value in input.items(): cmd.append(f'--{key}') cmd.append(f'{value}') sp.check_call(cmd) outputs = ju.read(self.output_json_path) self.assertAlmostEqual(outputs['tilt_correction'], -np.pi / 2) aff_t = AffineTransform.from_dict(outputs['tilt_transform_dict']) morph_t = aff_t.transform_morphology(self.morphology) axon = morph_t.node_by_id(1) self.assertAlmostEqual(axon['x'], 0) self.assertAlmostEqual(axon['y'], 3) self.assertAlmostEqual(axon['z'], -2)
def test_scale_correction_end_to_end(self): input = { 'swc_path': self.swc_path, 'marker_path': self.marker_path, 'soma_depth': self.soma_depth, 'cut_thickness': self.cut_thickness, 'output_json': self.output_json_path } cmd = [ 'python', '-m', 'neuron_morphology.transforms.scale_correction.compute_scale_correction' ] for key, value in input.items(): cmd.append(f'--{key}') cmd.append(f'{value}') sp.check_call(cmd) outputs = ju.read(self.output_json_path) self.assertAlmostEqual(outputs['scale_correction'], 2.0) aff_t = AffineTransform.from_dict(outputs['scale_transform']) morph_t = aff_t.transform_morphology(self.morphology, scale_radius=False) axon = morph_t.node_by_id(1) self.assertAlmostEqual(axon['x'], 0) self.assertAlmostEqual(axon['y'], 2) self.assertAlmostEqual(axon['z'], 200) self.assertAlmostEqual(axon['radius'], 0.4)
def test_upright_end_to_end(self): input = { 'gradient_path': self.gradient_path, 'node': '[0, 0, 0]', 'swc_path': self.swc_path, 'output_json': self.output_json_path } cmd = [ 'python', '-m', 'neuron_morphology.transforms.upright_angle.compute_angle' ] for key, value in input.items(): cmd.append(f'--{key}') cmd.append(f'{value}') sp.check_call(cmd) outputs = ju.read(self.output_json_path) self.assertAlmostEqual(outputs['upright_angle'], -np.pi / 2, 3) aff_t = AffineTransform.from_dict(outputs['upright_transform_dict']) morph_t = aff_t.transform_morphology(self.morphology) axon = morph_t.node_by_id(1) self.assertAlmostEqual(axon['x'], 1) self.assertAlmostEqual(axon['y'], 3) self.assertAlmostEqual(axon['z'], 3)
def test_transform_morphology(self): transformed_nodes = (AffineTransform(self.array) .transform_morphology(self.morphology) .nodes()) for node in transformed_nodes: assert np.allclose([node['x'], node['y'], node['z']], self.transformed_vector[node['id']])
def main(): mod = ArgSchemaParser(schema_type=ApplyAffineSchema, output_schema_type=OutputParameters) args = mod.args if 'affine_dict' in args: affine_transform = AffineTransform.from_dict(args['affine_dict']) elif 'affine_list' in args: affine_transform = AffineTransform.from_list(args['affine_list']) else: raise ValueError('must provide either an affine_dict or affine_list') morph_in = morphology_from_swc(args['input_swc']) morph_out = affine_transform.transform_morphology(morph_in) morphology_to_swc(morph_out, args['output_swc']) output = { 'inputs': args, 'transformed_swc': args['output_swc'], } mod.output(output)
def setUp(self): self.morphology = (MorphologyBuilder().root(1, 2, 3).axon(0, 2, 3).build()) self.test_dir = tempfile.mkdtemp() self.swc_path = os.path.join(self.test_dir, '123.swc') morphology_to_swc(self.morphology, self.swc_path) self.marker_path = os.path.join(self.test_dir, '123.marker') with open(self.marker_path, 'w') as f: f.write( "##x,y,z,radius,shape,name,comment, color_r,color_g,color_b\n" "1.0,2.0,2.5,0.0,0,30,0,255, 255, 255\n" "0.0,2.0,3.0,0.0,0,20,0,255, 255, 255") self.slice_transform_list = [1, 0, 0, 0, 1, 0, 0, 0, 1, 0, 0, 0] self.slice_transform = AffineTransform.from_list( self.slice_transform_list) self.slice_image_flip = True self.soma_marker = {'x': 1, 'y': 2, 'z': 0} self.soma_voxel = (400, 400, 1120) self.ccf_soma_location = [ self.soma_voxel[0] * CCF_RESOLUTION, self.soma_voxel[1] * CCF_RESOLUTION, self.soma_voxel[2] * CCF_RESOLUTION ] self.csl_dict = dict(zip(['x', 'y', 'z'], self.ccf_soma_location)) # Streamline is on z+, so should result in tilt of np.pi / 2 self.closest_path = np.asarray([ 400 * np.ones((20, ), dtype='i'), 400 * np.ones((20, ), dtype='i'), np.arange(1120, 1140, dtype='i') ]) self.ccf_path = os.path.join(self.test_dir, 'paths.h5') path = np.ravel_multi_index(self.closest_path, CCF_SHAPE) with h5py.File(self.ccf_path, 'w') as f: f.create_dataset("view lookup", (1, ), dtype='i', data=0) f.create_dataset("paths", (1, 20), dtype='i', data=path) self.output_json_path = os.path.join(self.test_dir, 'output.json')
def collect_inputs(working_bucket: str, run_prefix: str, reconstruction_id: int, upright_swc_key: str): md_json_key = f"{run_prefix}/{reconstruction_id}.json" md_json_response = s3.get_object(Bucket=working_bucket, Key=md_json_key) metadata = json.load(md_json_response["Body"]) marker_key = f"{run_prefix}/{metadata['marker_file']}" ccf_key = 'top_view_paths_10.h5' swc_response = s3.get_object( Bucket=working_bucket, Key=upright_swc_key, ) marker_response = s3.get_object( Bucket=working_bucket, Key=marker_key, ) ccf_response = s3.get_object( Bucket=working_bucket, Key=ccf_key, ) morphology = morphology_from_swc(swc_response["Body"]) soma_marker = read_soma_marker(marker_response["Body"]) ccf_data = BytesIO(ccf_response["Body"].read()) ccf_soma_location = dict(zip(['x', 'y', 'z'], metadata["ccf_soma_xyz"])) slice_transform = AffineTransform.from_list(metadata['slice_transform']) return { 'morphology': morphology, 'soma_marker': soma_marker, 'ccf_soma_location': ccf_soma_location, 'slice_transform': slice_transform, 'slice_image_flip': metadata['slice_image_flip'], 'ccf_path': ccf_data, }
def test_affine_from_list(self): assert np.allclose(AffineTransform.from_list(self.list).affine, self.array)
def test_affine_to_dict(self): affine_dict = AffineTransform(self.array).to_dict() for key, value in self.dict.items(): self.assertAlmostEqual(value, affine_dict[key])
def test_init_affine_from_dict(self): assert np.allclose(AffineTransform.from_dict(self.dict).affine, self.array)
def test_init_affine_matrix_from_array(self): assert np.allclose(AffineTransform(self.array).affine, self.array)
def test_init_affine_empty(self): assert np.allclose(AffineTransform().affine, np.eye(4))
def affine_from_transform_translation_2nd(self): affine = affine_from_transform_translation(transform=self.transform, translation=self.translation) assert np.allclose(AffineTransform(affine).transform(self.point), [1 / 2 + 10, np.sqrt(3) / 2, 0])
def test_transform_vector(self): affine_transform = AffineTransform(self.array) assert np.allclose(affine_transform.transform(self.vector), np.asarray(self.transformed_vector))
def test_affine_to_list(self): affine_list = AffineTransform(self.array).to_list() assert np.allclose(affine_list, self.list)
def test_transform_point(self): affine_transform = AffineTransform(self.array) assert np.allclose(affine_transform.transform(self.point), np.asarray([-np.sqrt(3)/2, 10.5, 0]))
def affine_from_transform_translation_1st(self): affine = affine_from_transform_translation(transform=self.transform, translation=self.translation, translate_first=True) assert np.allclose(AffineTransform(affine).transform(self.point), [11 / 2, 11 * np.sqrt(3) / 2, 0])