Beispiel #1
0
 def write_to_n5(self, folder, dataset):
     """
     Note: because diluvian works with z,y,x and pyn5 assumes x,y,z:
     when writing coordinates must be flipped
     """
     pyn5.create_dataset(
         folder, dataset, self.bounds[1], self.leaf_shape, str(self.dtype).upper()
     )
     dataset = pyn5.open(folder, dataset)
     for leaf in self.iter_leaves():
         pyn5.write(dataset, leaf.bounds, leaf.data, self.dtype)
Beispiel #2
0
    def test_read_write(self):
        # make sure n5 is initialized to all zeros
        self.assertTrue(
            np.array_equal(
                pyn5.read(self.n5,
                          (np.array([0, 0, 0]), np.array(self.dataset_size))),
                np.zeros([10, 10, 10]),
            ))

        # write ones to whole dataset, and then
        # write on partial blocks to make sure data isn't overwritten
        pyn5.write(
            self.n5,
            (np.array([0, 0, 0]), np.array(self.dataset_size)),
            np.ones(self.dataset_size),
        )
        self.assertTrue(
            np.array_equal(
                pyn5.read(self.n5,
                          (np.array([0, 0, 0]), np.array(self.dataset_size))),
                np.ones([10, 10, 10]),
            ))

        pyn5.write(self.n5, (np.array([1, 1, 1]), np.array([3, 3, 3])),
                   np.ones([2, 2, 2]) * 2)
        self.assertTrue(
            np.array_equal(
                pyn5.read(self.n5, (np.array([1, 1, 1]), np.array([3, 3, 3]))),
                np.ones([2, 2, 2]) * 2,
            ))

        # test writting non-uniform block to make sure axis orderings are correct
        pyn5.write(
            self.n5,
            (np.array([1, 1, 1]), np.array([5, 5, 5])),
            np.array(range(64), dtype=int).reshape([4, 4, 4]),
        )
        self.assertTrue(
            np.array_equal(
                pyn5.read(self.n5, (np.array([1, 1, 1]), np.array([5, 5, 5]))),
                np.array(range(64), dtype=int).reshape([4, 4, 4]),
            ))

        # test writing data in non block shapes
        bounds = (np.array([0, 0, 0]), np.array([1, 2, 3]))
        data = np.array(range(6)).reshape([1, 2, 3])
        pyn5.write(self.n5, bounds, data)

        self.assertTrue(np.array_equal(pyn5.read(self.n5, bounds), data))
Beispiel #3
0
 def save_data_for_CATMAID(self, folder_path: Path):
     """
     Save the segmentation confidence score
     """
     pyn5.create_dataset(
         folder_path + "/segmentations.n5",
         "confidence",
         [int(x) for x in self.end_voxel],
         [int(x) for x in self.leaf_shape_voxels],
         "UINT8",
     )
     dataset = pyn5.open(folder_path + "/segmentations.n5", "confidence")
     for leaf in self.distances.iter_leaves():
         pyn5.write(
             dataset,
             leaf.bounds,
             (
                 255
                 * self._view_weighted_mask(
                     tuple(map(slice, leaf.bounds[0], leaf.bounds[1]))
                 )
             ).astype(np.uint8),
             np.uint8,
         )
for file_path in (zarr_root / "volumes").iterdir():
    if not file_path.name.startswith("affs") and not file_path.name.startswith(
            "."):
        dataset_name = "volumes/{}".format(file_path.name)
        zarr_dataset = zarr_data[dataset_name]
        dtype = "{}".format(zarr_dataset.dtype).upper()
        if (n5_root / "volumes" / file_path.name).exists():
            shutil.rmtree(n5_root / "volumes" / file_path.name)
        all_data = zarr_dataset[:, :, :].transpose([2, 1, 0])
        # if dtype == "UINT64":
        #     vals = list(set(all_data.flatten()))
        #     mapping = {v: i for i, v in enumerate(vals)}
        #     fv = np.vectorize(lambda x: mapping[x])
        #     all_data = fv(all_data)
        #     dtype = "UINT64"
        pyn5.create_dataset(
            str(n5_root.absolute()),
            dataset_name,
            zarr_dataset.shape[::-1],
            [64, 64, 64],
            dtype,
        )
        n5_dataset = pyn5.open(str(n5_root.absolute()), dataset_name)
        pyn5.write(
            n5_dataset,
            (np.array([0, 0, 0]), np.array(zarr_dataset.shape[::-1])),
            all_data,
            dtype,
        )
Beispiel #5
0
    def test_read_write(self):
        # make sure n5 is initialized to all zeros
        self.assertTrue(
            np.array_equal(
                pyn5.read(self.n5,
                          (np.array([0, 0, 0]), np.array(self.dataset_size))),
                np.zeros([10, 10, 10]),
            ))

        # write ones to whole dataset, and then
        # write on partial blocks to make sure data isn't overwritten
        pyn5.write(
            self.n5,
            (np.array([0, 0, 0]), np.array(self.dataset_size)),
            np.ones(self.dataset_size),
        )
        self.assertTrue(
            np.array_equal(
                pyn5.read(self.n5,
                          (np.array([0, 0, 0]), np.array(self.dataset_size))),
                np.ones([10, 10, 10]),
            ))

        pyn5.write(self.n5, (np.array([1, 1, 1]), np.array([3, 3, 3])),
                   np.ones([2, 2, 2]) * 2)
        self.assertTrue(
            np.array_equal(
                pyn5.read(self.n5, (np.array([1, 1, 1]), np.array([3, 3, 3]))),
                np.ones([2, 2, 2]) * 2,
            ))

        # test writting non-uniform block to make sure axis orderings are correct
        self.n5.write_ndarray(
            np.array([1, 1, 1]),
            np.array(range(64), dtype=np.uint8).reshape([4, 4, 4]),
            0,
        )
        self.assertTrue(
            np.array_equal(
                self.n5.read_ndarray(np.array([1, 1, 1]), np.array([4, 4, 4])),
                np.array(range(64), dtype=int).reshape([4, 4, 4]),
            ))

        # sanity check axis orderings
        data = np.zeros([5, 7, 1], dtype=np.uint8)
        data[0, :, 0] = 1
        data[1, :, 0] = 2
        data[2, :, 0] = 3
        self.n5.write_ndarray(np.array([0, 0, 0]), data, 0)
        self.assertTrue(
            np.all(
                self.n5.read_ndarray(np.array([1, 0, 0]), np.array([1, 7, 1]))
                == 2))
        np.testing.assert_equal(
            self.n5.read_ndarray(np.array([0, 0, 0]), np.array([3, 1,
                                                                1])).flatten(),
            np.array([1, 2, 3]),
        )

        # test writing data in non block shapes
        data = np.array(range(6), dtype=np.uint8).reshape([1, 2, 3])
        self.n5.write_ndarray(np.array([0, 0, 0]), data, 0)

        self.assertTrue(
            np.array_equal(
                self.n5.read_ndarray(np.array([0, 0, 0]), np.array([1, 2, 3])),
                data))