def test_write_excel_tables(): num_sheets = np.random.randint(1, 4) num_cols = np.random.randint(1, 5, size=num_sheets) num_rows = np.random.randint(20, 40, size=num_sheets) tables = [] for m, n in zip(num_rows, num_cols): columns = [f'column{i}' for i in range(n)] data = np.random.random((m, n)) tables.append(pd.DataFrame(data=data, columns=columns)) sheet_names = [f'sheet {i}' for i in range(num_sheets)] kwargs = dict(zip(sheet_names, tables)) kwargs['config'] = { 'image files': ['image1.tif', 'image2.tif'], 'image format': 'fei', 'threshold radius': 5e-8 } with temporary_file(suffix='.xlsx') as file: io.write_excel(file, **kwargs) tables_in = [ pd.read_excel(file, sheet_name=name, index_col=0) for name in sheet_names ] config_in_df = pd.read_excel(file, sheet_name='config') config_in = dict( zip(config_in_df['parameters'], config_in_df['values'])) for table, table_in in zip(tables, tables_in): assert list(table.columns) == list(table_in.columns) np.testing.assert_allclose(table_in.values, table.values) for key, val in kwargs['config'].items(): str(val) == str(config_in[key])
def test_server_with_id_service(dummy_data): frag, gt, fman = dummy_data id_service_port = 5600 config = {'client_url': 'tcp://*:5590', 'id_service_url': 'tcp://localhost:%i' % id_service_port, 'solver_url': 'tcp://localhost:5590'} with temporary_file('.json') as config_filename: with open(config_filename, 'w') as fout: json.dump(config, fout) solver = serve.Solver(frag, feature_manager=fman, config_file=config_filename) starting_id = 23461 id_thread = threading.Thread(target=id_serve, name='id-service', daemon=True, kwargs=dict(port=id_service_port, curr_id=starting_id)) id_thread.start() thread = threading.Thread(target=solver.listen, name='solver') thread.start() host, port = config['solver_url'].rsplit(':', maxsplit=1) _, dst = serve.proofread(frag, gt, host=host, port=int(port), num_operations=2, stop_when_finished=True, random_state=0) result = np.array(dst)[frag] # test: resulting segmentation should be improvement over fragments alone assert (ev.vi(result, gt, ignore_x=[], ignore_y=[]) < ev.vi(frag, gt, ignore_x=[], ignore_y=[])) # test 2: make sure ID service worked: starting ID should be as above # should be equal but boundary ID messes things up assert np.min(result) > starting_id thread.join()
def test_imsave_incorrect_dimension(): with temporary_file(suffix='.png') as fname: with testing.raises(ValueError): with expected_warnings([fname + ' is a low contrast image']): imsave(fname, np.zeros((2, 3, 3, 1))) with testing.raises(ValueError): with expected_warnings([fname + ' is a low contrast image']): imsave(fname, np.zeros((2, 3, 2)))
def test_jpg_quality_arg(): chessboard = np.load(os.path.join(data_dir, 'chessboard_GRAY_U8.npy')) with temporary_file(suffix='.jpg') as jpg: imsave(jpg, chessboard, quality=95) im = imread(jpg) sim = ssim(chessboard, im, data_range=chessboard.max() - chessboard.min()) assert sim > 0.99
def test_jpg_quality_arg(): chessboard = np.load(fetch('data/chessboard_GRAY_U8.npy')) with temporary_file(suffix='.jpg') as jpg: imsave(jpg, chessboard, quality=95) im = imread(jpg) sim = structural_similarity( chessboard, im, data_range=chessboard.max() - chessboard.min()) assert sim > 0.99
def test_vtk_roundtrip(): for i in range(4): ar_shape = np.random.randint(1, 50, size=3) dtypes = list(imio.numpy_type_to_vtk_string.keys()) cur_dtype = np.random.choice(dtypes) if np.issubdtype(cur_dtype, np.integer): ar = np.random.randint(0, 127, size=ar_shape).astype(cur_dtype) else: ar = np.random.rand(*ar_shape).astype(cur_dtype) with temporary_file('.vtk') as fout: imio.write_vtk(ar, fout) ar_in = imio.read_vtk(fout) np.testing.assert_equal(ar, ar_in)
def test_cremi_roundtrip(): raw_image = np.random.randint(256, size=(5, 100, 100), dtype=np.uint8) labels = np.random.randint(4096, size=raw_image.shape, dtype=np.uint64) for ax in range(labels.ndim): labels.sort(axis=ax) # try to get something vaguely contiguous. =P with temporary_file('.hdf') as fout: imio.write_cremi({'/volumes/raw': raw_image, '/volumes/labels/neuron_ids': labels}, fout) raw_in, lab_in = imio.read_cremi(fout) f = h5py.File(fout) stored_resolution = f['/volumes/raw'].attrs['resolution'] f.close() np.testing.assert_equal(stored_resolution, (40, 4, 4)) np.testing.assert_equal(raw_in, raw_image) np.testing.assert_equal(lab_in, labels)
def test_cremi_roundtrip(): raw_image = np.random.randint(256, size=(5, 100, 100), dtype=np.uint8) labels = np.random.randint(4096, size=raw_image.shape, dtype=np.uint64) for ax in range(labels.ndim): labels.sort(axis=ax) # try to get something vaguely contiguous. =P with temporary_file('.hdf') as fout: imio.write_cremi( { '/volumes/raw': raw_image, '/volumes/labels/neuron_ids': labels }, fout) raw_in, lab_in = imio.read_cremi(fout) f = h5py.File(fout) stored_resolution = f['/volumes/raw'].attrs['resolution'] f.close() np.testing.assert_equal(stored_resolution, (40, 4, 4)) np.testing.assert_equal(raw_in, raw_image) np.testing.assert_equal(lab_in, labels)
def test_server_with_periodic_send(dummy_data): frag, gt, fman = dummy_data id_service_port = 5601 config = { 'client_url': 'tcp://*:5591', 'id_service_url': 'tcp://localhost:%i' % id_service_port, 'solver_url': 'tcp://localhost:5591' } with temporary_file('.json') as config_filename: with open(config_filename, 'w') as fout: json.dump(config, fout) solver = serve.Solver(frag, feature_manager=fman, config_file=config_filename) starting_id = 23461 id_thread = threading.Thread(target=id_serve, name='id-service', daemon=True, kwargs=dict(port=id_service_port, curr_id=starting_id)) id_thread.start() thread = threading.Thread(target=solver.listen, name='solver', daemon=True, kwargs=dict(send_every=10)) thread.start() host, port = config['solver_url'].rsplit(':', maxsplit=1) _, dst = serve.proofread(frag, gt, host=host, port=int(port), num_operations=2, stop_when_finished=True, request_seg=False, random_state=0) result = np.array(dst)[frag] # test: resulting segmentation should be improvement over fragments alone assert (ev.vi(result, gt, ignore_x=[], ignore_y=[]) < ev.vi( frag, gt, ignore_x=[], ignore_y=[])) # test 2: make sure ID service worked: starting ID should be as above # should be equal but boundary ID messes things up assert np.min(result) > starting_id
def roundtrip_file(self, x): with temporary_file(suffix='.png') as fname: imsave(fname, x) y = imread(fname) return y
def test_imsave_incorrect_dimension(): with temporary_file(suffix='.png') as fname: with testing.raises(ValueError): imsave(fname, np.zeros((2, 3, 3, 1))) with testing.raises(ValueError): imsave(fname, np.zeros((2, 3, 2)))