def test_dataset_with_data_transform(): seq = random_ubyte_image_sequence(lazy=True, N=3) t = lambda x: x + 1.0 ds = Dataset(seq, data_transform=t) for i in range(3): assert_equal(ds[i].data, t(seq[i].data))
def test_calibrate_compensate_distortion_realdata(): ds = datasets.calibration_ipv40CCD_distortion() res = calibrate_distortion(ds, (7, 7)) ds_comp = compensate_distortion(ds, *res) for img, comp in zip(ds, ds_comp): assert_equal(comp.data.mean(), img.data.mean(), 0.01 * img.data.max())
def test_compensate_flatfield(): img = np.random.random((10, 10)) coeff = np.random.random((4, 10, 10)) / 10 target = coeff[0] + coeff[1] * img + coeff[2] * img**2 + coeff[3] * img**3 res = _compensate_flatfield(_make_image_seq([img]), coeff)[0].data assert_equal(res, target, 0.1)
def test_calibrate_and_compensate_int_preserves_range(): img0 = np.array([[-10]], dtype=DTYPE_INT) img1 = np.array([[10]], dtype=DTYPE_INT) img_test = np.array([[0]], dtype=DTYPE_INT) coeff = calibrate_flatfield(_make_image_seq([img0, img1]), [0, 1.0]) compensated = compensate_flatfield(_make_image_seq([img_test]), coeff) assert_equal(compensated[0].data, 0)
def test_calibrate_flatfield_linear(): img0 = np.random.random((10, 10)) / 10 img1 = (-np.random.random((10, 10)) / 10) + 1.0 coeff = 1 / (img1 - img0) coeff *= np.mean(img1) coeff_result = _calibrate_flatfield([img0, img1], [0, 1.0], order=1) assert_equal(coeff_result[1], coeff) assert_equal(coeff_result[0], -coeff * img0)
def test_segment_module_part(): mod = data.datasets.poly10x6(1)[0] mod = detection.locate_module_and_cells(mod) part = detection.segment_module_part(mod, 1, 2, 2, 3) assert_equal(part.shape[1], 2 / 3 * part.shape[0], 0.1) assert part.first_col == 1 assert part.first_row == 2 assert part.cols == 2 assert part.rows == 3
def test_calibrate_flatfield_least_squares_linear(): img0 = np.random.random((10, 10)) / 10 img2 = (-np.random.random((10, 10)) / 10) + 1.0 coeff = 1 / (img2 - img0) img1 = (np.full_like(img0, 0.5) + img0) / coeff coeff *= np.mean(img2) coeff_result = _calibrate_flatfield([img0, img1, img2], [0, 0.5, 1.0], order=1) assert_equal(coeff_result[1], coeff, 0.1) assert_equal(coeff_result[0], -coeff * img0, 0.1)
def test_stitching(): ( image_0, image_1, ) = _prepare_stitching_test_img() images = (image_0, image_1) # test stitching and check size of result stitched = locate_and_stitch_modules(ImageSequence(images, True), 6, 6, 2, overlap_horizontal=2) assert_equal(stitched.shape[1] / stitched.shape[0], 10 / 6, precision=0.05)
def test_segment_padding_transform(): img = detection.locate_module_and_cells(data.datasets.poly10x6(1)[0]) part = detection.segment_module_part(img, 0, 0, 3, 2, padding=0.5, size=20) x1 = part.get_meta("transform")(np.array([[0.0, 1.0]])).flatten() x2 = part.get_meta("transform")(np.array([[2.0, 3.0]])).flatten() assert_equal(x1[0], 10) assert_equal(x1[1], 30) assert_equal(x2[0], 50) assert_equal(x2[1], 70)
def test_calibration_object(tmp_path: Path): ff_imgs, ex_list = _prepare_ff_data() test_img = _prepare_ff_test_img() dist_imgs = datasets.calibration_ipv40CCD_distortion(N=8) # perform calibration calib = Calibration() calib.calibrate_flatfield(images=ff_imgs, targets=ex_list) calib.calibrate_distortion(images=dist_imgs, checkerboard_size=(7, 7)) # save to disk calib.save(tmp_path / "calib.pck") # load from disk calib_loaded = load_calibration(tmp_path / "calib.pck") # perform compensation with both comp = calib.process(test_img) comp_loaded = calib_loaded.process(test_img) # assert both result in the same image assert_equal(comp.data, comp_loaded.data)
def test_locate_partial_module(): img = detection.locate_module_and_cells(data.datasets.poly10x6(1)[0]) part = detection.segment_module_part(img, 0, 0, 3, 2, padding=0.5) part_det = detection.locate_module_and_cells(part, False) x1_true = [part_det.shape[1] * 1 / 8, part_det.shape[0] * 1 / 6] x2_true = [part_det.shape[1] * 7 / 8, part_det.shape[0] * 5 / 6] x1 = part_det.get_meta("transform")(np.array([[0.0, 0.0]])).flatten() x2 = part_det.get_meta("transform")(np.array([[3.0, 2.0]])).flatten() eps = 0.05 * part_det.shape[1] assert_equal(x1[0], x1_true[0], eps) assert_equal(x1[1], x1_true[1], eps) assert_equal(x2[0], x2_true[0], eps) assert_equal(x2[1], x2_true[1], eps)
def test_calibrate_and_compensate_flatfield(): img0 = np.random.random((10, 10)) / 10 img1 = np.random.random((10, 10)) / 10 + 0.5 - 0.05 img2 = (-np.random.random((10, 10)) / 10) + 1.0 mean = np.mean(img2) coeff = _calibrate_flatfield([img0, img1, img2], [0, 0.5, 1.0], order=2) compensated = _compensate_flatfield(_make_image_seq([img0, img1, img2]), coeff) assert_equal(np.zeros_like(img0), compensated[0].data) assert_equal(np.full_like(img1, 0.5 * mean), compensated[1].data) assert_equal(np.full_like(img1, 1.0 * mean), compensated[2].data)
def test_calibrate_flatfield_quadratic(): img0 = np.random.random((10, 10)) / 10 img1 = np.random.random((10, 10)) / 10 + 0.5 - 0.05 img2 = (-np.random.random((10, 10)) / 10) + 1.0 mean = np.mean(img2) def compensate(img, coeff): return coeff[2] * img**2 + coeff[1] * img + coeff[0] coeff = _calibrate_flatfield([img0, img1, img2], [0, 0.5, 1.0], order=2) assert_equal(np.zeros_like(img0), compensate(img0, coeff)) assert_equal(np.full_like(img1, 0.5 * mean), compensate(img1, coeff)) assert_equal(np.full_like(img1, 1.0 * mean), compensate(img2, coeff))
def test_segment_modules(): seq = data.datasets.poly10x6(2) seq = detection.locate_module_and_cells(seq, True) modules = detection.segment_modules(seq) assert isinstance(modules, ModuleImageSequence) assert isinstance(modules[0], ModuleImage) assert len(modules) == 2 assert modules[0].path == seq[0].path assert modules.same_camera is False x = modules[0].get_meta("transform")(np.array([[0.0, 0.0]])).flatten() assert_equal(x[0], 0.0) assert_equal(x[1], 0.0) x = modules[0].get_meta("transform")(np.array([[10.0, 0.0]])).flatten() assert_equal(x[0], modules[0].shape[1]) assert_equal(x[1], 0.0) x = modules[0].get_meta("transform")(np.array([[10.0, 6.0]])).flatten() assert_equal(x[0], modules[0].shape[1]) assert_equal(x[1], modules[0].shape[0])
def test_segment_padding(): img = detection.locate_module_and_cells(data.datasets.poly10x6(1)[0]) part = detection.segment_module_part(img, 0, 0, 3, 2, padding=0.5) assert_equal(part.shape[1] / part.shape[0], 8 / 6)
def test_segment_size(): img = detection.locate_module_and_cells(data.datasets.poly10x6(1)[0]) part = detection.segment_module_part(img, 1, 3, 2, 1, size=20) assert_equal(part.shape[1], 2 * 20) assert_equal(part.shape[0], 1 * 20)