Beispiel #1
0
def test_cells_to_xml(tmpdir):
    cells = cell_io.get_cells(xml_path)
    tmp_cells_out_path = os.path.join(str(tmpdir), "cells.xml")
    cell_io.cells_to_xml(cells, tmp_cells_out_path)
    assert cells == cell_io.get_cells(tmp_cells_out_path)
Beispiel #2
0
def xml_crop(args, df_query="name"):
    args = prep_atlas_conf(args)

    if args.structures_file_path is None:
        args.structures_file_path = get_structures_path()

    reference_struct_df = pd.read_csv(get_structures_path())

    curate_struct_df = pd.read_csv(args.structures_file_path)

    curate_struct_df = reference_struct_df[
        reference_struct_df[df_query].isin(curate_struct_df[df_query])
    ]

    curated_ids = list(curate_struct_df["structure_id_path"])

    atlas = brainio.load_any(args.registered_atlas_path)
    hemisphere = brainio.load_any(args.hemispheres_atlas_path)

    structures_reference_df = load_structures_as_df(get_structures_path())

    atlas_pixel_sizes = get_atlas_pixel_sizes(args.atlas_config)
    sample_pixel_sizes = args.x_pixel_um, args.y_pixel_um, args.z_pixel_um

    scales = cells_regions.get_scales(sample_pixel_sizes, atlas_pixel_sizes)

    destination_folder = os.path.join(args.xml_dir, "xml_crop")
    if not os.path.exists(destination_folder):
        os.makedirs(destination_folder)

    xml_names = [f for f in os.listdir(args.xml_dir) if f.endswith(".xml")]
    xml_paths = [os.path.join(args.xml_dir, f) for f in xml_names]

    for idx, xml_path in enumerate(xml_paths):
        print("Curating file: {}".format(xml_names[idx]))
        cells = cells_regions.get_cells_data(
            xml_path, cells_only=args.cells_only,
        )
        max_coords = cells_regions.get_max_coords(cells)

        curated_cells = []
        for i, cell in enumerate(cells):
            cells_regions.transform_cell_coords(atlas, cell, scales)

            structure_id = cells_regions.get_structure_from_coordinates(
                atlas,
                cell,
                max_coords,
                order=args.coordinates_order,
                structures_reference_df=structures_reference_df,
            )
            if structure_id in curated_ids:
                if args.hemisphere_query in [1, 2]:
                    hemisphere = cells_regions.get_structure_from_coordinates(
                        hemisphere,
                        cell,
                        max_coords,
                        order=args.coordinates_order,
                    )
                    if hemisphere is args.hemisphere_query:
                        curated_cells.append(cell)
                else:
                    curated_cells.append(cell)
        cells_to_xml(
            curated_cells,
            os.path.join(destination_folder, xml_names[idx]),
            artifact_keep=True,
        )
    print("Done!")
Beispiel #3
0
def napari_cells_to_xml(cells, non_cells, xml_file_path):
    cell_list = napari_array_to_cell_list(cells, type=Cell.CELL)
    non_cell_list = napari_array_to_cell_list(non_cells, type=Cell.UNKNOWN)

    all_cells = cell_list + non_cell_list
    cells_to_xml(all_cells, xml_file_path)