def neurons_df(self): """Table with all neurons positions and soma regions. """ if self._neurons_df is None: # Generate table with soma position to query by region: atlas = BrainGlobeAtlas("mpin_zfish_1um", print_authors=False) neurons_dict = dict() for f in self.data_path.glob("*.swc"): coords = soma_coords_from_file(f) # compute coordinates # Calculate anatomical structure the neuron belongs to: try: region = atlas.structure_from_coords(coords) except IndexError: region = 0 neurons_dict[f.stem] = dict( filename=f.name, pos_ap=coords[0], pos_si=coords[1], pos_lr=coords[2], region=region, ) self._neurons_df = pd.DataFrame(neurons_dict).T return self._neurons_df
def prep_cellfinder_general(): args = parser.cellfinder_parser().parse_args() arg_groups = get_arg_groups(args, cellfinder_parser()) # args = define_pixel_sizes(args) check_input_arg_existance(args) if not os.path.exists(args.output_dir): os.makedirs(args.output_dir) args.paths = Paths(args.output_dir) fancylog.start_logging( args.output_dir, program_for_log, variables=[args, args.paths], verbose=args.debug, log_header="CELLFINDER LOG", ) log_metadata(args.paths.metadata_path, args) what_to_run = CalcWhatToRun(args) args.signal_ch_ids, args.background_ch_id = check_and_return_ch_ids( args.signal_ch_ids, args.background_ch_id, args.signal_planes_paths) args.brainreg_paths = BrainRegPaths(args.paths.registration_output_folder) atlas = BrainGlobeAtlas(args.atlas) return args, arg_groups, what_to_run, atlas
def analyse_cell_positions(points, brainreg_directory, signal_path, output_directory): brainreg_paths = BrainregPaths(brainreg_directory) with open(brainreg_paths.metadata_path) as json_file: metadata = json.load(json_file) atlas = BrainGlobeAtlas(metadata["atlas"]) downsampled_space = get_downsampled_space( atlas, brainreg_paths.boundaries_file_path) deformation_field_paths = [ brainreg_paths.deformation_field_0, brainreg_paths.deformation_field_1, brainreg_paths.deformation_field_2, ] run_analysis( points, signal_path, metadata["orientation"], metadata["voxel_sizes"], atlas, deformation_field_paths, downsampled_space, output_directory / "downsampled.points", output_directory / "atlas.points", output_directory / "points.npy", brainreg_paths.volume_csv_path, output_directory / "all_points.csv", output_directory / "summary.csv", ) print("Done")
def load_atlas(self, resolution: int) -> Atlas: if resolution not in [10, 25, 100]: raise ValueError( "Only 10um, 25um and 100um atlas resolutions available.") with redirect_stdout( StringIO()): # blocks the BrainGlobeAtlas print to console bgatlas = BrainGlobeAtlas(f"allen_mouse_{resolution}um") return Atlas( volume=bgatlas.reference, resolution_um=bgatlas.resolution[0], )
def load_atlas(self): atlas = BrainGlobeAtlas(self.current_atlas_name) self.atlas = atlas self.base_layer = self.viewer.add_image( self.atlas.reference, name="Reference", ) self.atlas_layer = self.viewer.add_labels( self.atlas.annotation, name=self.atlas.atlas_name, blending="additive", opacity=0.3, visible=False, ) self.standard_space = True
def test_lateralise_atlas_image(): atlas = BrainGlobeAtlas(atlas_name) mask = np.random.random(atlas.annotation.shape) > 0.7 masked_annotations = mask * atlas.annotation annotations_left, annotations_right = atlas_utils.lateralise_atlas_image( masked_annotations, atlas.hemispheres, left_hemisphere_value=atlas.left_hemisphere_value, right_hemisphere_value=atlas.right_hemisphere_value, ) # not the best way to test this is functioning properly total_vals_in = np.prod(mask.shape) total_vals_out = len(annotations_left) + len(annotations_right) assert total_vals_in == total_vals_out
def read(self, name: str) -> RemoteAtlasReaderData: with redirect_stdout( StringIO()): # blocks the BrainGlobeAtlas print to console bgatlas = BrainGlobeAtlas(atlas_name=name) # Brainglobe atlases have a "reference volume" and an "annotation volume" assert bgatlas.annotation.shape == bgatlas.reference.shape new_reference = bgatlas.space.map_stack_to("ipl", bgatlas.reference) new_annotation = bgatlas.space.map_stack_to("ipl", bgatlas.annotation) return RemoteAtlasReaderData( source="Brainglobe", name=name, registration_volume=new_reference, annotation_volume=new_annotation, resolution_um=bgatlas.resolution[0], )
import numpy as np import pandas as pd from pathlib import Path from bg_atlasapi import BrainGlobeAtlas from brainreg_segment.tracks.analysis import run_track_analysis atlas_name = "allen_mouse_50um" atlas = BrainGlobeAtlas(atlas_name) tracks_dir = Path.cwd() / "tests" / "data" / "tracks" spline_validate = np.array( ( [100.0, 49.81249744, 50.08749819], [100.0, 58.06928727, 58.53487443], [100.0, 66.38765117, 66.9206766], [100.0, 74.74999655, 75.26249729], [100.0, 83.13873081, 83.57792909], [100.0, 91.53626138, 91.8845646], [100.0, 99.92499565, 100.1999964], [100.0, 108.28734103, 108.54181709], [100.0, 116.60570493, 116.92761926], [100.0, 124.86249475, 125.3749955], ) ) def test_run_track_analysis(tmpdir, rtol=1e-10): points = pd.read_hdf(tracks_dir / "track.points")
def get_neurons_by_structure(self, *region): atlas = BrainGlobeAtlas("mpin_zfish_1um", print_authors=False) IDs = atlas._get_from_structure(region, "id") return list( self.neurons_df.loc[self.neurons_df.region.isin(IDs)].index)
def fetch_neurons_metadata(self, filterby=None, filter_regions=None, **kwargs): """ Download neurons metadata and data from the API. The downloaded metadata can be filtered to keep only the neurons whose soma is in a list of user selected brain regions. :param filterby: Accepted values: "soma". If it's "soma", neurons are kept only when their soma is in the list of brain regions defined by filter_regions (Default value = None) :param filter_regions: List of brain regions acronyms. If filtering neurons, these specify the filter criteria. (Default value = None) :param **kwargs: """ # Download all metadata print("Querying MouseLight API...") url = mouselight_base_url + "graphql" query = make_query(filterby=filterby, filter_regions=filter_regions, **kwargs) res = post_mouselight(url, query=query)["searchNeurons"] print(" ... fetched metadata for {} neurons in {}s".format( res["totalCount"], round(res["queryTime"] / 1000, 2))) # Process neurons to clean up the results and make them easier to handle neurons = res["neurons"] node = namedtuple("node", "x y z r area_acronym sample_n parent_n") tracing_structure = namedtuple("tracing_structure", "id name value named_id") cleaned_nurons = [] # <- output is stored here for neuron in neurons: if neuron["brainArea"] is not None: brainArea_acronym = neuron["brainArea"]["acronym"] brainArea_id = neuron["brainArea"]["id"] brainArea_name = neuron["brainArea"]["name"] brainArea_safename = neuron["brainArea"]["safeName"] brainArea_atlasId = neuron["brainArea"]["atlasId"] brainArea_structureIdPath = neuron["brainArea"][ "structureIdPath"] else: brainArea_acronym = None brainArea_id = None brainArea_name = None brainArea_safename = None brainArea_atlasId = None brainArea_structureIdPath = None if len(neuron["tracings"]) > 1: dendrite = tracing_structure( neuron["tracings"][1]["id"], neuron["tracings"][1]["tracingStructure"]["name"], neuron["tracings"][1]["tracingStructure"]["value"], neuron["tracings"][1]["tracingStructure"]["id"], ) else: dendrite = None clean_neuron = dict( brainArea_acronym=brainArea_acronym, brainArea_id=brainArea_id, brainArea_name=brainArea_name, brainArea_safename=brainArea_safename, brainArea_atlasId=brainArea_atlasId, brainArea_structureIdPath=brainArea_structureIdPath, id=neuron["id"], idNumber=neuron["idNumber"], idString=neuron["idString"], tag=neuron["tag"], soma=node( neuron["tracings"][0]["soma"]["x"], neuron["tracings"][0]["soma"]["y"], neuron["tracings"][0]["soma"]["z"], neuron["tracings"][0]["soma"]["radius"], brainArea_name, neuron["tracings"][0]["soma"]["sampleNumber"], neuron["tracings"][0]["soma"]["parentNumber"], ), axon=tracing_structure( neuron["tracings"][0]["id"], neuron["tracings"][0]["tracingStructure"]["name"], neuron["tracings"][0]["tracingStructure"]["value"], neuron["tracings"][0]["tracingStructure"]["id"], ), dendrite=dendrite, ) cleaned_nurons.append(clean_neuron) # Filter neurons to keep only those matching the search criteria if filterby is not None: if filter_regions is None: raise ValueError( "If filtering neuron by region, you need to pass a list of filter regions to use" ) # get brain globe atlas atlas = BrainGlobeAtlas("allen_mouse_25um") # Filter by soma if filterby == "soma": filtered_neurons = [] for neuron in cleaned_nurons: if neuron["brainArea_acronym"] is None: continue # get ancestors of neuron's regions try: neuron_region_ancestors = atlas.get_structure_ancestors( neuron["brainArea_acronym"]) except KeyError: # ignore if region is not found continue # If any of the ancestors are in the allowed regions, keep neuron. if is_any_item_in_list(filter_regions, neuron_region_ancestors): filtered_neurons.append(neuron) print(" ... selected {} neurons out of {}".format( len(filtered_neurons), res["totalCount"])) neurons = filtered_neurons else: print(" ... selected {} neurons out of {}".format( len(cleaned_nurons), res["totalCount"])) neurons = cleaned_nurons else: neurons = cleaned_nurons return neurons
def main( atlas, data_orientation, target_brain_path, paths, voxel_sizes, niftyreg_args, n_free_cpus=2, sort_input_file=False, additional_images_downsample=None, backend="niftyreg", scaling_rounding_decimals=5, debug=False, ): atlas = BrainGlobeAtlas(atlas) source_space = bg.AnatomicalSpace(data_orientation) scaling = [] for idx, axis in enumerate(atlas.space.axes_order): scaling.append( round( float(voxel_sizes[idx]) / atlas.resolution[atlas.space.axes_order.index( source_space.axes_order[idx])], scaling_rounding_decimals, )) n_processes = get_num_processes(min_free_cpu_cores=n_free_cpus) load_parallel = n_processes > 1 logging.info("Loading raw image data") target_brain = imio.load_any( target_brain_path, scaling[1], scaling[2], scaling[0], load_parallel=load_parallel, sort_input_file=sort_input_file, n_free_cpus=n_free_cpus, ) target_brain = bg.map_stack_to(data_orientation, atlas.metadata["orientation"], target_brain) if backend == "niftyreg": run_niftyreg( paths.registration_output_folder, paths, atlas, target_brain, n_processes, additional_images_downsample, data_orientation, atlas.metadata["orientation"], niftyreg_args, scaling, load_parallel, sort_input_file, n_free_cpus, debug=debug, ) logging.info("Calculating volumes of each brain area") calculate_volumes( atlas, paths.registered_atlas, paths.registered_hemispheres, paths.volume_csv_path, # for all brainglobe atlases left_hemisphere_value=1, right_hemisphere_value=2, ) logging.info("Generating boundary image") boundaries( paths.registered_atlas, paths.boundaries_file_path, ) logging.info(f"brainreg completed. Results can be found here: " f"{paths.registration_output_folder}")