Exemplo n.º 1
0
def get_numpy_dataset(original_dataset, input_slice, output_slice, transform):
    dataset_numpy = dict()
    original_data_slice = get_zero_padded_array_slice(original_dataset['data'], input_slice)
    data_slice = np.array(original_data_slice, dtype=np.float32) / (2. ** 8)
    if transform:
        if 'transform' in original_dataset:
            lo, hi = original_dataset['transform']['scale']
            data_slice = 0.5 + (data_slice-0.5)*np.random.uniform(low=lo, high=hi)
            lo, hi = original_dataset['transform']['shift']
            data_slice = data_slice + np.random.uniform(low=lo, high=hi)
        else:
            print("WARNING: source data doesn't have 'transform' attribute.")
    dataset_numpy['data'] = data_slice
    # load outputs if desired
    if output_slice is not None:
        dataset_numpy['components'] = get_zero_padded_array_slice(original_dataset['components'], output_slice)
        if 'label' in original_dataset:
            label_shape = original_dataset['label'].shape
            label_slice = (slice(0, label_shape[0]),) + output_slice
            dataset_numpy['label'] = get_zero_padded_array_slice(original_dataset['label'], label_slice)
        else:
            # compute affinities from components
            dataset_numpy['label'] = pygt.malis.seg_to_affgraph(dataset_numpy['components'], original_dataset['nhood'])
            warnings.warn("Computing affinity labels because 'label' wasn't provided in data source.", UserWarning)
        if 'mask' in original_dataset:
            dataset_numpy['mask'] = get_zero_padded_array_slice(original_dataset['mask'], output_slice)
            dataset_numpy['mask'] = dataset_numpy['mask'].astype(np.uint8)
        else:
            # assume no masking
            assumed_output_mask = np.ones_like(original_dataset['components'], dtype=np.uint8)
            dataset_numpy['mask'] = get_zero_padded_array_slice(assumed_output_mask, output_slice)
            warnings.warn("No mask provided. Setting to 1 where outputs exist.", UserWarning)
    return dataset_numpy
Exemplo n.º 2
0
 def test_works_with_too_positive_origin_1d(self):
     X = np.ones(shape=(5,))
     slice_shape = (3,)
     origin = (3,)
     result = get_zero_padded_slice_from_array_by_offset(X, origin, shape=slice_shape)
     expected_result = np.array([1, 1, 0], dtype=X.dtype)
     assert np.allclose(result, expected_result)
     assert result.dtype == expected_result.dtype
     slices = [slice(o, o + s, 1) for o, s in zip(origin, slice_shape)]
     result = get_zero_padded_array_slice(X, slices)
     assert np.allclose(result, expected_result)
     assert result.dtype == expected_result.dtype
     X = np.ones(shape=(5, 5))
     slice_shape = (3, 3)
     origin = (3, 3)
     result = get_zero_padded_slice_from_array_by_offset(X, origin, shape=slice_shape)
     expected_result = np.array([[1, 1, 0],
                                 [1, 1, 0],
                                 [0, 0, 0]], dtype=X.dtype)
     assert np.allclose(result, expected_result)
     assert result.dtype == expected_result.dtype
     slices = [slice(o, o + s, 1) for o, s in zip(origin, slice_shape)]
     result = get_zero_padded_array_slice(X, slices)
     assert np.allclose(result, expected_result)
     assert result.dtype == expected_result.dtype
Exemplo n.º 3
0
 def test_works_with_slice_bigger_than_source(self):
     X = np.ones(shape=(1,))
     origin = (-1,)
     slice_shape = (3,)
     result = get_zero_padded_slice_from_array_by_offset(X, origin, shape=slice_shape)
     expected_result = np.array([0, 1, 0], dtype=X.dtype)
     assert np.allclose(result, expected_result)
     assert result.dtype == expected_result.dtype
     slices = [slice(o, o + s, 1) for o, s in zip(origin, slice_shape)]
     result = get_zero_padded_array_slice(X, slices)
     assert np.allclose(result, expected_result)
     assert result.dtype == expected_result.dtype
Exemplo n.º 4
0
 def test_works_with_interior_slice(self):
     X = np.ones(shape=(5,))
     origin = (1,)
     slice_shape = (3,)
     result = get_zero_padded_slice_from_array_by_offset(X, origin, shape=slice_shape)
     expected_result = np.array([1, 1, 1], dtype=X.dtype)
     assert np.allclose(result, expected_result)
     assert result.dtype == expected_result.dtype, "{}, {}".format(result.dtype, expected_result.dtype)
     slices = [slice(o, o + s, 1) for o, s in zip(origin, slice_shape)]
     result = get_zero_padded_array_slice(X, slices)
     assert np.allclose(result, expected_result)
     assert result.dtype == expected_result.dtype
Exemplo n.º 5
0
 def test_works_with_hdf5_file(self):
     with h5py.File(self.test_h5_file_name, 'r') as h5_file:
         X = h5_file['test dataset']
         origin = (-1, -1)
         slice_shape = (3, 3)
         result = get_zero_padded_slice_from_array_by_offset(X, origin, slice_shape)
         expected_result = np.array([[0, 0, 0],
                                     [0, 1, 0],
                                     [0, 0, 0]], dtype=X.dtype)
         assert np.allclose(result, expected_result)
         assert result.dtype == expected_result.dtype
         slices = [slice(o, o + s, 1) for o, s in zip(origin, slice_shape)]
         result = get_zero_padded_array_slice(X, slices)
         assert np.allclose(result, expected_result)
         assert result.dtype == expected_result.dtype
Exemplo n.º 6
0
 def __getitem__(self, item):
     return get_zero_padded_array_slice(self.array_like, item)