Exemplo n.º 1
0
    def __init__(self, atlas_name=None):
        """
            Brainrender's Atlas class subclasses BrainGlobeAtlas
            to add methods to get regions meshes as Actors
            and to get a plane at a given point and normal.

            :param atlas_name: str, atlas name from brainglobe's atlas API atlases
        """
        atlas_name = atlas_name or settings.DEFAULT_ATLAS
        BrainGlobeAtlas.__init__(self,
                                 atlas_name=atlas_name,
                                 print_authors=False)
Exemplo n.º 2
0
    def __init__(self, atlas_name, *args, base_dir=None, **kwargs):
        # Create brainglobe atlas
        BrainGlobeAtlas.__init__(self, *args, atlas_name=atlas_name, **kwargs)

        # Add brainrender paths
        Paths.__init__(self, base_dir=base_dir, **kwargs)
        self.meshes_folder = (
            None  # where the .obj mesh for each region is saved
        )

        # If it's a mouse atlas, add extra functionality
        if "Mus musculus" == self.metadata["species"]:
            ABA.__init__(self)
Exemplo n.º 3
0
    def __init__(self, atlas_name, *args, base_dir=None, **kwargs):
        # Create brainglobe atlas
        BrainGlobeAtlas.__init__(
            self, *args, atlas_name=atlas_name, print_authors=False, **kwargs
        )

        # Add brainrender paths
        Paths.__init__(self, base_dir=base_dir, **kwargs)

        Enhanced.__init__(self)

        # If it's a mouse atlas, add extra functionality
        if "Mus musculus" == self.metadata["species"]:
            ABA.__init__(self)
Exemplo n.º 4
0
def update_atlas(atlas_name, force=False):
    """Updates a bg_atlasapi atlas from the latest
    available version online.

    Arguments:
    ----------
    atlas_name: str
        Name of the atlas to update.
    force: bool
        If False it checks if the atlas is already at the latest version
        and doesn't update if that's the case.
    """

    atlas = BrainGlobeAtlas(atlas_name=atlas_name)

    # Check if we need to update
    if not force:
        if atlas.check_latest_version():
            rprint(
                f"[b][magenta2]bg_atlasapi: {atlas.atlas_name} is already updated "
                + f"(version: {_version_str_from_tuple(atlas.local_version)})[/b]"
            )
            return

    # Delete atlas folder
    rprint(
        f"[b][magenta2]bg_atlasapi: updating {atlas.atlas_name}[/magenta2][/b]"
    )
    fld = atlas.brainglobe_dir / atlas.local_full_name
    shutil.rmtree(fld)
    if fld.exists():
        raise ValueError(
            "Something went wrong while tryint to delete the old version of the atlas, aborting."
        )

    # Download again
    atlas.download_extract_file()

    # Check that everything went well
    rprint(
        f"[b][magenta2]bg_atlasapi: {atlas.atlas_name} updated to version: "
        + f"{_version_str_from_tuple(atlas.remote_version)}[/magenta2][/b]"
    )
Exemplo n.º 5
0
def test_local_search():
    brainglobe_dir = tempfile.mkdtemp()
    interm_download_dir = tempfile.mkdtemp()

    atlas = BrainGlobeAtlas(
        "example_mouse_100um",
        brainglobe_dir=brainglobe_dir,
        interm_download_dir=interm_download_dir,
    )

    assert atlas.atlas_name in atlas.local_full_name

    # Make a copy:
    copy_filename = atlas.root_dir.parent / (atlas.root_dir.name + "_2")
    shutil.copytree(atlas.root_dir, copy_filename)

    with pytest.raises(FileExistsError) as error:
        _ = BrainGlobeAtlas("example_mouse_100um",
                            brainglobe_dir=brainglobe_dir)
    assert "Multiple versions of atlas" in str(error)

    shutil.rmtree(brainglobe_dir)
    shutil.rmtree(interm_download_dir)
def reader_function(path):
    """Take a path or list of paths and return a list of LayerData tuples.

    Readers are expected to return data as a list of tuples, where each tuple
    is (data, [add_kwargs, [layer_type]]), "add_kwargs" and "layer_type" are
    both optional.

    Parameters
    ----------
    path : str or list of str
        Path to file, or list of paths.

    Returns
    -------
    layer_data : list of tuples
        A list of LayerData tuples where each tuple in the list contains
        (data, metadata, layer_type), where data is a numpy array, metadata is
        a dict of keyword arguments for the corresponding viewer.add_* method
        in napari, and layer_type is a lower-case string naming the type of layer.
        Both "meta", and "layer_type" are optional. napari will default to
        layer_type=="image" if not provided
    """

    print("Loading brainreg directory")
    path = Path(os.path.abspath(path))
    with open(path / "brainreg.json") as json_file:
        metadata = json.load(json_file)

    atlas = BrainGlobeAtlas(metadata["atlas"])
    metadata["atlas_class"] = atlas
    layers = []
    layers = load_additional_downsampled_channels(
        path,
        layers,
        search_string="downsampled_standard",
        exlusion_string="downsampled_standard.tiff",
    )
    layers.append((
        tifffile.imread(path / "downsampled_standard.tiff"),
        {
            "name": "Registered image",
            "metadata": metadata
        },
        "image",
    ))
    layers = load_atlas(atlas, layers)

    return layers
Exemplo n.º 7
0
def main():
    start_time = datetime.now()
    args = register_cli_parser().parse_args()
    arg_groups = get_arg_groups(args, register_cli_parser())
    args = define_pixel_sizes(args)

    args, additional_images_downsample = prep_registration(args)

    paths = Paths(args.brainreg_directory)

    log_metadata(paths.metadata_path, args)

    fancylog.start_logging(
        paths.registration_output_folder,
        program_for_log,
        variables=[args],
        verbose=args.debug,
        log_header="BRAINREG LOG",
        multiprocessing_aware=False,
    )

    logging.info("Starting registration")

    atlas = BrainGlobeAtlas(args.atlas)

    register(
        atlas,
        args.orientation,
        args.image_paths,
        paths,
        arg_groups["NiftyReg registration backend options"],
        x_pixel_um=args.x_pixel_um,
        y_pixel_um=args.y_pixel_um,
        z_pixel_um=args.z_pixel_um,
        sort_input_file=args.sort_input_file,
        n_free_cpus=args.n_free_cpus,
        additional_images_downsample=additional_images_downsample,
        backend=args.backend,
        debug=args.debug,
    )

    logging.info("Finished. Total time taken: %s", datetime.now() - start_time)
Exemplo n.º 8
0
def install_atlas(atlas_name):
    """Installs a BrainGlobe atlas from the latest
    available version online.

    Arguments
    ---------
    atlas_name : str
        Name of the atlas to update.
    """

    # Check input:
    if not isinstance(atlas_name, str):
        raise ValueError(f"atlas name should be a string, not {atlas_name}")

    # Check if already downloaded:
    available_atlases = get_downloaded_atlases()
    if atlas_name in available_atlases:
        rprint(
            f"[b][magenta2]Bg_atlasapi: installing {atlas_name}: atlas already installed![/magenta2][/b]"
        )
        return

    # Istantiate to download:
    BrainGlobeAtlas(atlas_name)
Exemplo n.º 9
0
def atlas():
    return BrainGlobeAtlas("example_mouse_100um")
Exemplo n.º 10
0
def atlas_path():
    return BrainGlobeAtlas("example_mouse_100um").root_dir