Пример #1
0
    def __init__(self, path):
        path = Path(path)

        # To temporarily support the old syntax, a conf file can still be
        # passed.
        # If a conf file is used, we assume only one atlas is logged there -
        # as currently
        # this seems to be the only supported option
        # If we are already using new system:
        if path.is_dir():
            with open(path / "atlas_metadata.json", "r") as f:
                atlas_metadata = json.load(f)

            # This field we want to generate actively from instantiation path,
            # to avoid confusion:
            self.base_folder = path
        else:
            # If a configuration file is passed:
            data_dict = dict(get_config_obj(str(path)))

            # Generate clean atlas metadata structure:
            atlas_metadata = data_dict["atlas"]

            # Atlas base folder:
            self.base_folder = Path(atlas_metadata.pop("base_folder"))

            # To actively propagate support for new syntax, we also write the
            # json file in the atlas directory; this should discontinued in
            # the long term:
            with open(self.base_folder / "atlas_metadata.json", "w") as f:
                json.dump(atlas_metadata, f)

        self._pix_sizes = None

        super().__init__(**atlas_metadata)
Пример #2
0
    def __init__(
        self,
        config_path,
        affine_n_steps=6,
        affine_use_n_steps=5,
        freeform_n_steps=6,
        freeform_use_n_steps=4,
        bending_energy_weight=0.95,
        grid_spacing=-10,
        smoothing_sigma_reference=-1.0,
        smoothing_sigma_floating=-1.0,
        histogram_n_bins_floating=128,
        histogram_n_bins_reference=128,
    ):
        self.config = get_config_obj(config_path)
        self.transform_program_path = self.__get_binary("transform")
        self.affine_reg_program_path = self.__get_binary("affine")
        self.freeform_reg_program_path = self.__get_binary("freeform")
        self.segmentation_program_path = self.__get_binary("segmentation")

        # affine (reg_aladin)
        self.affine_reg_pyramid_steps = ("-ln", affine_n_steps)
        self.affine_reg_used_pyramid_steps = ("-lp", affine_use_n_steps)

        # freeform (ref_f3d)
        self.freeform_reg_pyramid_steps = ("-ln", freeform_n_steps)
        self.freeform_reg_used_pyramid_steps = ("-lp", freeform_use_n_steps)

        self.freeform_reg_grid_spacing = ("-sx", grid_spacing)

        self.bending_energy_penalty_weight = ("-be", bending_energy_weight)

        self.reference_image_smoothing_sigma = (
            "-smooR",
            smoothing_sigma_reference,
        )
        self.floating_image_smoothing_sigma = (
            "-smooF",
            smoothing_sigma_floating,
        )

        self.reference_image_histo_n_bins = (
            "--rbn",
            histogram_n_bins_reference,
        )
        self.floating_image_histo_n_bins = ("--fbn", histogram_n_bins_floating)

        # segmentation (reg_resample)
        self.segmentation_interpolation_order = ("-inter", 0)

        # The atlas has been saved to the output folder
        atlas = RegistrationAtlas(config_path)
        # self.atlas_path = atlas.get_path()
        # self.atlas_brain_path = atlas.get_brain_path()
        # self.hemispheres_path = atlas.get_hemispheres_path()

        pixel_sizes = atlas["pixel_size"]
        self.atlas_x_pix_size = pixel_sizes["x"]
        self.atlas_y_pix_size = pixel_sizes["y"]
        self.atlas_z_pix_size = pixel_sizes["z"]
Пример #3
0
def get_structures_path(config=None):
    if config is None:
        config = source_custom_config_amap()

    config_obj = get_config_obj(config)
    atlas_conf = config_obj["atlas"]

    return get_atlas_element_path(atlas_conf, "structures_name")
Пример #4
0
def get_voxel_volume(registration_config):
    config_obj = get_config_obj(registration_config)
    atlas_conf = config_obj["atlas"]
    atlas_pixel_sizes = atlas_conf["pixel_size"]
    x_pixel_size = float(atlas_pixel_sizes["x"])
    y_pixel_size = float(atlas_pixel_sizes["y"])
    z_pixel_size = float(atlas_pixel_sizes["z"])

    voxel_volume = x_pixel_size * y_pixel_size * z_pixel_size
    return voxel_volume
Пример #5
0
def write_model_to_cfg(new_model_path, orig_config, custom_config):
    config_obj = get_config_obj(orig_config)
    model_conf = config_obj["model"]
    orig_path = model_conf["model_path"]

    with open(orig_config, "r") as in_conf:
        data = in_conf.readlines()
    for i, line in enumerate(data):
        data[i] = line.replace(f"model_path = '{orig_path}",
                               f"model_path = '{new_model_path}")
    with open(custom_config, "w") as out_conf:
        out_conf.writelines(data)
Пример #6
0
def get_image_scales(log_entries, config_file):
    """
    Returns the scaling from downsampled data to raw data
    :param log_entries: Entries parsed from the log file
    :param config_file: Path to the amap config file
    :return: Tuple of scaling factors
    """
    config_obj = get_config_obj(config_file)
    atlas_conf = config_obj["atlas"]
    pixel_sizes = atlas_conf["pixel_size"]
    x_scale = float(pixel_sizes["x"]) / float(log_entries["x_pixel_um"])
    y_scale = float(pixel_sizes["y"]) / float(log_entries["y_pixel_um"])
    z_scale = float(pixel_sizes["z"]) / float(log_entries["z_pixel_um"])
    return z_scale, y_scale, x_scale
Пример #7
0
def write_atlas_to_cfg(atlas_folder, atlas, orig_config, custom_config):
    config_obj = get_config_obj(orig_config)
    atlas_conf = config_obj["atlas"]
    orig_base_directory = atlas_conf["base_folder"]

    with open(orig_config, "r") as in_conf:
        data = in_conf.readlines()
    for i, line in enumerate(data):
        data[i] = line.replace(
            f"base_folder = '{orig_base_directory}",
            f"base_folder = '{os.path.join(atlas_folder, atlas)}",
        )
    with open(custom_config, "w") as out_conf:
        out_conf.writelines(data)
Пример #8
0
    def __init__(self, config_path, dest_folder=""):
        config_obj = get_config_obj(config_path)
        self.atlas_conf = config_obj["atlas"]

        self.dest_folder = dest_folder

        self._pix_sizes = None  # cached to avoid reloading atlas
        self._data = None
        self._brain_data = None
        self._hemispheres_data = None

        self.original_orientation = self.atlas_conf["orientation"]
        if self.original_orientation != "horizontal":
            raise NotImplementedError(
                "Unknown orientation {}. Only horizontal supported so far".
                format(self.original_orientation))
Пример #9
0
def check_atlas_install():
    """
    Checks whether the atlas directory exists, and whether it's empty or not.
    :return: Whether the directory exists, and whether the files also exist
    """
    dir_exists = False
    files_exist = False
    cfg_file_path = source_files.source_custom_config()
    if os.path.exists(cfg_file_path):
        config_obj = get_config_obj(cfg_file_path)
        atlas_conf = config_obj["atlas"]
        atlas_directory = atlas_conf["base_folder"]
        if os.path.exists(atlas_directory):
            dir_exists = True
            if not os.listdir(atlas_directory) == []:
                files_exist = True

    return dir_exists, files_exist
Пример #10
0
def generate_test_config(atlas_dir):
    config = os.path.join(os.getcwd(), "tests", "data", "config", "test.conf")
    config_obj = get_config_obj(config)
    atlas_conf = config_obj["atlas"]
    orig_base_directory = atlas_conf["base_folder"]

    with open(config, "r") as in_conf:
        data = in_conf.readlines()
    for i, line in enumerate(data):
        data[i] = line.replace(
            f"base_folder = '{orig_base_directory}",
            f"base_folder = '{os.path.join(atlas_dir, TEST_ATLAS)}",
        )
    test_config = os.path.join(atlas_dir, "config.conf")
    with open(test_config, "w") as out_conf:
        out_conf.writelines(data)

    return test_config
Пример #11
0
def check_atlas_install():
    """
    Checks whether the atlas directory exists, and whether it's empty or not.
    :return: Whether the directory exists, and whether the files also exist
    """
    # TODO: make more sophisticated, check for all files that might be needed
    dir_exists = False
    files_exist = False
    cfg_file_path = source_files.source_custom_config_cellfinder()
    if os.path.exists(cfg_file_path):
        config_obj = get_config_obj(cfg_file_path)
        atlas_conf = config_obj["atlas"]
        atlas_directory = atlas_conf["base_folder"]
        if os.path.exists(atlas_directory):
            dir_exists = True
            if not os.listdir(atlas_directory) == []:
                files_exist = True

    return dir_exists, files_exist
Пример #12
0
def write_atlas_to_cfg(atlas_folder, atlas, orig_config, custom_config):
    """ Write configuration of an atlas over a preexisting config file
    :param atlas_folder:
    :param atlas:
    :param orig_config:
    :param custom_config:
    :return:
    """
    config_obj = get_config_obj(orig_config)
    atlas_conf = config_obj["atlas"]
    orig_base_directory = atlas_conf["base_folder"]

    with open(orig_config, "r") as in_conf:
        data = in_conf.readlines()
    for i, line in enumerate(data):
        data[i] = line.replace(
            f"base_folder = '{orig_base_directory}",
            f"base_folder = '{atlas_folder / atlas}",
        )
    with open(custom_config, "w") as out_conf:
        out_conf.writelines(data)
Пример #13
0
def get_atlas_pixel_sizes(atlas_config_path):
    config_obj = get_config_obj(atlas_config_path)
    atlas_conf = config_obj["atlas"]
    atlas_pixel_sizes = atlas_conf["pixel_size"]
    return atlas_pixel_sizes
Пример #14
0
def get_model_weights(config_file):
    logging.debug(f"Reading config file: {config_file}")
    config_obj = get_config_obj(config_file)
    model_conf = config_obj["model"]
    model_weights = model_conf["model_path"]
    return model_weights