def test_preprocess_modify_array(): def pre(arr, maxval): return arr / maxval size = (64, 64) sdg = SpatialDataGenerator() sdg.source = 'data/small.tif' sdg.indexes = 1 sdg.width, sdg.height = size df = sdg.regular_grid(*size) df['max'] = [a.max() for a in sdg.flow_from_dataframe(df, batch_size=1)] sdg.add_preprocess_callback('normalize', pre, df['max'].max()) arr = next(sdg.flow_from_dataframe(df)) assert len(arr.shape) == 3 assert arr.shape[0] == min(sdg.batch_size, len(df)) assert arr.shape[-2] == size[0] and arr.shape[-1] == size[1] assert arr.max() <= 1.0
def test_get_batch_match_frame_len(): size = (64,64) sdg = SpatialDataGenerator() sdg.source = 'data/small.tif' df = sdg.regular_grid(*size) gen = sdg.flow_from_dataframe(df, *size, batch_size=3) count = sum([batch.shape[0] for batch in gen]) assert count == len(df)
def test_get_size_upscale(): size = (64,64) sdg = SpatialDataGenerator() sdg.source = 'data/small.tif' df = sdg.regular_grid(size[0]//4, size[1]//4) gen = sdg.flow_from_dataframe(df, *size) arr = next(gen) assert len(arr.shape) == 4 assert arr.shape[0] == min(sdg.batch_size, len(df))
def test_sample_size(): sdg = SpatialDataGenerator() sdg.source = 'data/small.tif' df = sdg.regular_grid(64,64) gen = sdg.flow_from_dataframe(df, 64, 64) arr = next(gen) assert len(arr.shape) == 4 assert arr.shape[0] == min(sdg.batch_size, len(df)) assert arr.shape[1] == 64 and arr.shape[2] == 64
def test_sample_size(): size = (64, 64) sdg = SpatialDataGenerator() sdg.source = 'data/small.tif' sdg.width, sdg.height = size df = sdg.regular_grid(*size) gen = sdg.flow_from_dataframe(df) arr = next(gen) assert len(arr.shape) == 4 assert arr.shape[0] == min(sdg.batch_size, len(df)) assert arr.shape[-2] == size[0] and arr.shape[-1] == size[1]
def test_get_size_downscale(): size = (16, 16) sdg = SpatialDataGenerator() sdg.source = 'data/small.tif' df = sdg.regular_grid(size[0] * 4, size[1] * 4) gen = sdg.flow_from_dataframe(df, *size) arr = next(gen) assert len(arr.shape) == 4 assert arr.shape[0] == min(sdg.batch_size, len(df)) assert arr.shape[2] == size[1] and arr.shape[3] == size[0]
def test_get_batch_size_override(): size = (64,64) batch_size = 2 sdg = SpatialDataGenerator() sdg.source = 'data/small.tif' df = sdg.regular_grid(*size) gen = sdg.flow_from_dataframe(df, *size, batch_size=batch_size) arr = next(gen) assert len(arr.shape) == 4 assert arr.shape[0] == min(batch_size, len(df)) assert arr.shape[1] == size[1] and arr.shape[2] == size[0]
def test_preprocess_add_array(): def pre(arr): return np.stack((arr, arr / 10)) size = (64, 64) sdg = SpatialDataGenerator() sdg.source = 'data/small.tif' sdg.indexes = 1 sdg.width, sdg.height = size df = sdg.regular_grid(*size) sdg.add_preprocess_callback('pre', pre) arr = next(sdg.flow_from_dataframe(df)) assert len(arr.shape) == 4 assert arr.shape[0] == min(sdg.batch_size, len(df)) assert arr.shape[1] == 2 assert arr.shape[-2] == size[0] and arr.shape[-1] == size[1]