Ejemplo n.º 1
0
def test_getitem_identifier():
    size = 20
    pha = np.repeat(np.linspace(0, 10, size), size)
    pha = pha.reshape(size, size)

    qpi1 = qpimage.QPImage(data=1.1 * pha,
                           which_data="phase",
                           meta_data={"identifier": "peter"})
    qpi2 = qpimage.QPImage(data=1.2 * pha,
                           which_data="phase",
                           meta_data={"identifier": "hans"})
    qpi3 = qpimage.QPImage(data=1.3 * pha,
                           which_data="phase",
                           meta_data={"identifier": "doe"})

    series = qpimage.QPSeries(qpimage_list=[qpi1, qpi2, qpi3])
    assert "peter" in series
    assert "peter_bad" not in series
    assert series["peter"] == qpi1
    assert series["peter"] != qpi2
    assert series["hans"] == qpi2
    assert series["doe"] == qpi3
    try:
        series["john"]
    except KeyError:
        pass
    else:
        assert False, "'john' is not in series"
    try:
        series.add_qpimage(qpi1)
    except ValueError:
        pass
    else:
        assert False, "Adding QPImage with same identifier should not work"
Ejemplo n.º 2
0
def test_getitem():
    size = 20
    pha = np.repeat(np.linspace(0, 10, size), size)
    pha = pha.reshape(size, size)

    qpi1 = qpimage.QPImage(data=1.1 * pha, which_data="phase")
    qpi2 = qpimage.QPImage(data=1.2 * pha, which_data="phase")
    qpi3 = qpimage.QPImage(data=1.3 * pha, which_data="phase")

    series = qpimage.QPSeries(qpimage_list=[qpi1, qpi2, qpi3])

    assert qpi1 != qpi2
    assert qpi1 != qpi3
    assert series[0] == qpi1
    assert series[1] == qpi2
    assert series[2] == qpi3
    assert series[-3] == qpi1
    assert series[-2] == qpi2
    assert series[-1] == qpi3

    try:
        series[-4]
    except ValueError:
        pass
    else:
        assert False, "Negative index exceeds size."
Ejemplo n.º 3
0
def test_series_meta():
    h5file = pathlib.Path(__file__).parent / "data" / "bg_tilt.h5"
    try:
        qpimage.QPImage(h5file=h5file,
                        meta_data={"wavelength": 333e-9},
                        h5mode="r")
    except OSError:
        # no write intent on file
        pass
    else:
        assert False, "should not be able to write"

    qpi0 = qpimage.QPImage(h5file=h5file, h5mode="r")

    tf = tempfile.mktemp(suffix=".h5", prefix="qpimage_test_")
    tf = pathlib.Path(tf)
    with qpi0.copy(h5file=tf):
        pass

    qpi1 = qpimage.QPImage(h5file=tf,
                           meta_data={"wavelength": 335e-9})

    assert qpi1.meta["wavelength"] == 335e-9
    qps = qpimage.QPSeries(qpimage_list=[qpi1], meta_data={
                           "wavelength": 400e-9})
    assert qps.get_qpimage(0).meta["wavelength"] == 400e-9

    # cleanup
    try:
        tf.unlink()
    except OSError:
        pass
Ejemplo n.º 4
0
def test_qpimage_copy_to_file():
    h5file = pathlib.Path(__file__).parent / "data" / "bg_tilt.h5"
    qpi = qpimage.QPImage(h5file=h5file, h5mode="r")

    tf = tempfile.mktemp(suffix=".h5", prefix="qpimage_test_")
    with qpi.copy(h5file=tf) as qpi2:
        assert np.allclose(qpi.pha, qpi2.pha)
        assert qpi.meta == qpi2.meta
        assert not np.allclose(qpi2.bg_pha, 0)
        qpi2.clear_bg(which_data="phase", keys="fit")
        assert np.allclose(qpi2.bg_pha, 0)

    with qpimage.QPImage(h5file=tf, h5mode="r") as qpi3:
        assert np.allclose(qpi3.bg_pha, 0)

    # override h5 file
    with h5py.File(tf, mode="a") as h54:
        with qpimage.QPImage(h5file=h54) as qpi4:
            assert np.allclose(qpi4.bg_pha, 0)
        qpi.copy(h5file=h54)
        with qpimage.QPImage(h5file=h54) as qpi5:
            assert not np.allclose(qpi5.bg_pha, 0)

    try:
        os.remove(tf)
    except OSError:
        pass
Ejemplo n.º 5
0
def test_attributes():
    size = 200
    phase = np.repeat(np.linspace(0, np.pi, size), size)
    phase = phase.reshape(size, size)
    bgphase = np.sqrt(np.abs(phase))

    qpi = qpimage.QPImage(phase, bg_data=bgphase, which_data="phase")

    try:
        qpimage.integrity_check.check(qpi, checks="attributes")
    except qpimage.integrity_check.IntegrityCheckError:
        pass
    else:
        assert False, "should raise error, b/c no attributes present"

    qpi2 = qpimage.QPImage(phase,
                           bg_data=bgphase,
                           which_data="phase",
                           meta_data={
                               "medium index": 1.335,
                               "pixel size": 0.1,
                               "time": 0.0,
                               "wavelength": 10e-9,
                           })
    qpimage.integrity_check.check(qpi2)
Ejemplo n.º 6
0
def test_identifier_qpimage():
    size = 20
    pha = np.repeat(np.linspace(0, 10, size), size)
    pha = pha.reshape(size, size)

    qpi1 = qpimage.QPImage(data=1.1 * pha, which_data="phase")
    qpi2 = qpimage.QPImage(data=1.2 * pha, which_data="phase")
    qpi3 = qpimage.QPImage(data=1.3 * pha, which_data="phase")

    series = qpimage.QPSeries(qpimage_list=[qpi1, qpi2])
    series.add_qpimage(qpi=qpi3, identifier="hastalavista")
    assert series[2]["identifier"] == "hastalavista"
Ejemplo n.º 7
0
def test_qpimage_copy_method():
    h5file = pathlib.Path(__file__).parent / "data" / "bg_tilt.h5"
    tf = tempfile.mktemp(suffix=".h5", prefix="qpimage_test_")
    qpimage.core.copyh5(inh5=h5file, outh5=tf)
    qpi1 = qpimage.QPImage(h5file=h5file, h5mode="r")
    qpi2 = qpimage.QPImage(h5file=tf, h5mode="r")
    assert np.allclose(qpi1.pha, qpi2.pha)
    assert qpi1.meta == qpi2.meta

    try:
        os.remove(tf)
    except OSError:
        pass
Ejemplo n.º 8
0
def test_border_m():
    size = 200
    rsobj = np.random.RandomState(47)
    data = rsobj.normal(loc=.4, scale=.1, size=(size, size))
    pixel_size = .1e-6  # 1px = .1µm
    border_px = 5
    border_m = border_px * pixel_size

    qpi = qpimage.QPImage(data=data, which_data="phase")
    qpi.compute_bg(which_data="phase",
                   fit_offset="mean",
                   fit_profile="offset",
                   border_px=5)
    data_px = qpi.pha
    qpi.compute_bg(which_data="phase",
                   fit_offset="mean",
                   fit_profile="offset",
                   border_px=6)
    data_px_false = qpi.pha
    # Make sure different borders yield different results
    assert not np.all(data_px == data_px_false)
    # Make sure QPImage needs the "pixel size" meta data
    try:
        qpi.compute_bg(which_data="phase",
                       fit_offset="mean",
                       fit_profile="offset",
                       border_m=border_m)
    except meta.MetaDataMissingError:
        pass
    else:
        assert False, "meta data required"
    # Create an instance with the correct pixel size
    qpi2 = qpimage.QPImage(data=data,
                           which_data="phase",
                           meta_data={"pixel size": pixel_size})
    qpi2.compute_bg(which_data="phase",
                    fit_offset="mean",
                    fit_profile="offset",
                    border_m=border_m)
    assert np.all(data_px == qpi2.pha)
    # Also test with rounding
    qpi2.compute_bg(which_data="phase",
                    fit_offset="mean",
                    fit_profile="offset",
                    border_m=border_m * 1.05)
    assert np.all(data_px == qpi2.pha)
    qpi2.compute_bg(which_data="phase",
                    fit_offset="mean",
                    fit_profile="offset",
                    border_m=border_m * .95)
    assert np.all(data_px == qpi2.pha)
Ejemplo n.º 9
0
def test_repr():
    # make sure no errors when printing repr
    size = 200
    phase = np.repeat(np.linspace(0, np.pi, size), size)
    phase = phase.reshape(size, size)
    qpi = qpimage.QPImage(phase, which_data="phase",
                          meta_data={"wavelength": 550e-9})
    print(qpi)
    qpi2 = qpimage.QPImage(phase, which_data="phase",
                           meta_data={"wavelength": .1})
    print(qpi2)

    print(qpi._pha)
    print(qpi._amp)
Ejemplo n.º 10
0
def test_background_fit():
    size = 200
    phase = np.repeat(np.linspace(0, np.pi, size), size)
    phase = phase.reshape(size, size)
    tf = tempfile.mktemp(suffix=".h5", prefix="qpimage_test_")

    with qpimage.QPImage(phase, which_data="phase", h5file=tf) as qpi:
        qpi.compute_bg(which_data="phase",
                       fit_offset="fit",
                       fit_profile="tilt",
                       border_px=5)

    with h5py.File(tf, "a") as h5:
        h5["phase"]["bg_data"]["fit"][:10] = 9

    try:
        qpimage.integrity_check.check(tf, checks="background")
    except qpimage.integrity_check.IntegrityCheckError:
        pass
    else:
        assert False, "wrong bg saved should not work"
    # cleanup
    try:
        os.remove(tf)
    except OSError:
        pass
Ejemplo n.º 11
0
def test_bg_correction_index():
    qpi, path, dout = setup_test_data(num=2)

    path_out = drymass.convert(path_in=path,
                               dir_out=dout,
                               bg_data_amp=1,
                               bg_data_pha=1)
    with qpimage.QPSeries(h5file=path_out, h5mode="r") as qps:
        # background correction with same input image will result
        # in a flat QPImage.
        assert np.all(qps[0].pha == 0)
        assert np.all(qps[0].amp == 1)

    # To be absolutely sure this works, append a blank
    # QPImage and do it again.
    with qpimage.QPSeries(h5file=path, h5mode="a") as qps:
        pha = .5 * np.ones(qps[0].shape)
        amp = .9 * np.ones(qps[0].shape)
        qps.add_qpimage(
            qpimage.QPImage(data=(pha, amp), which_data="phase,amplitude"))

    path_out = drymass.convert(path_in=path,
                               dir_out=dout,
                               bg_data_amp=3,
                               bg_data_pha=3)
    with qpimage.QPSeries(h5file=path_out, h5mode="r") as qps:
        # background correction with same input image will result
        # in a flat QPImage.
        assert np.allclose(qps[0].pha, qpi.pha - .5)
        assert np.allclose(qps[0].amp, qpi.amp / .9)
Ejemplo n.º 12
0
def test_iter():
    h5file = pathlib.Path(__file__).parent / "data" / "bg_tilt.h5"
    qpi = qpimage.QPImage(h5file=h5file, h5mode="r")
    series = qpimage.QPSeries(qpimage_list=[qpi, qpi, qpi])

    for qpj in series:
        assert qpj == qpi
Ejemplo n.º 13
0
def test_qpimage_copy_to_mem():
    h5file = pathlib.Path(__file__).parent / "data" / "bg_tilt.h5"
    qpi = qpimage.QPImage(h5file=h5file, h5mode="r")
    # create an in-memory copy
    qpi2 = qpi.copy()
    assert np.allclose(qpi.pha, qpi2.pha)
    assert qpi.meta == qpi2.meta
Ejemplo n.º 14
0
def test_clear_bg():
    size = 50
    pha = np.repeat(np.linspace(0, 10, size), size)
    pha = pha.reshape(size, size)
    amp = np.linspace(.95, 1.05, size**2).reshape(size, size)

    bg_amp = np.linspace(.93, 1.02, size**2).reshape(size, size)
    bg_pha = pha * .5

    qpi = qpimage.QPImage(data=(pha, amp),
                          bg_data=(bg_pha, bg_amp),
                          which_data="phase,amplitude",
                          h5dtype="float64")
    # amplitude
    assert np.all(qpi.bg_amp == bg_amp)
    qpi.clear_bg(which_data="amplitude", keys="data")
    assert np.allclose(qpi.bg_amp, 1)
    qpi.compute_bg(which_data="amplitude",
                   fit_offset="fit",
                   fit_profile="tilt",
                   border_px=5)
    assert not np.allclose(qpi.bg_amp, 1)
    qpi.clear_bg(which_data="amplitude", keys="fit")
    assert np.allclose(qpi.bg_amp, 1)
    # phase
    assert np.all(qpi.bg_pha == bg_pha)
    qpi.clear_bg(which_data="phase", keys="data")
Ejemplo n.º 15
0
def test_border_perc():
    size = 200
    rsobj = np.random.RandomState(47)
    data = rsobj.normal(loc=.4, scale=.1, size=(size, size))

    qpi = qpimage.QPImage(data=data, which_data="phase")
    qpi.compute_bg(which_data="phase",
                   fit_offset="mean",
                   fit_profile="offset",
                   border_px=5)
    data_px = qpi.pha
    qpi.compute_bg(which_data="phase",
                   fit_offset="mean",
                   fit_profile="offset",
                   border_perc=2.5)
    assert np.all(qpi.pha == data_px)
    # rounding
    qpi.compute_bg(which_data="phase",
                   fit_offset="mean",
                   fit_profile="offset",
                   border_perc=2.4)
    assert np.all(qpi.pha == data_px)
    qpi.compute_bg(which_data="phase",
                   fit_offset="mean",
                   fit_profile="offset",
                   border_perc=2.1)  # 4px
    assert not np.all(qpi.pha == data_px)
Ejemplo n.º 16
0
def setup_test_data(radius_px=30, size=200, pxsize=1e-6, medium_index=1.335,
                    wavelength=550e-9, num=1):
    x = np.arange(size).reshape(-1, 1)
    y = np.arange(size).reshape(1, -1)
    cx = 80
    cy = 120
    r = np.sqrt((x - cx)**2 + (y - cy)**2)
    pha = (r < radius_px) * 1.3
    amp = .5 + np.roll(pha, 10) / pha.max()
    qpi = qpimage.QPImage(data=(pha, amp),
                          which_data="phase,amplitude",
                          meta_data={"pixel size": pxsize,
                                     "medium index": medium_index,
                                     "wavelength": wavelength})
    path_in = tempfile.mktemp(suffix=".h5", prefix="drymass_test_cli_sphere")
    path_in = pathlib.Path(path_in)
    with qpimage.QPSeries(h5file=path_in, h5mode="w", identifier="tt") as qps:
        for ii in range(num):
            qps.add_qpimage(qpi, identifier="image_{}".format(ii))
    # add drymass configuration file
    path_out = path_in.with_name(path_in.name + dialog.OUTPUT_SUFFIX)
    path_out.mkdir()
    cfg = config.ConfigFile(path_out)
    cfg.set_value(section="meta", key="pixel size um", value=pxsize*1e6)
    cfg.set_value(section="meta", key="wavelength nm", value=wavelength*1e9)
    cfg.set_value(section="meta", key="medium index", value=medium_index)
    cfg.set_value(section="specimen", key="size um",
                  value=radius_px*2*pxsize*1e6)
    cfg.set_value(section="sphere", key="method", value="edge")
    cfg.set_value(section="sphere", key="model", value="projection")
    return qpi, path_in, path_out
Ejemplo n.º 17
0
def setup_test_data(radius_px=30,
                    size=200,
                    pxsize=1e-6,
                    medium_index=1.335,
                    wavelength=550e-9,
                    num=1):
    x = np.arange(size).reshape(-1, 1)
    y = np.arange(size).reshape(1, -1)
    cx = 80
    cy = 120
    r = np.sqrt((x - cx)**2 + (y - cy)**2)
    pha = (r < radius_px) * 1.3
    amp = .5 + np.roll(pha, 10) / pha.max()
    qpi = qpimage.QPImage(data=(pha, amp),
                          which_data="phase,amplitude",
                          meta_data={
                              "pixel size": pxsize,
                              "medium index": medium_index,
                              "wavelength": wavelength
                          })
    path = tempfile.mktemp(suffix=".h5", prefix="drymass_test_convert")
    dout = tempfile.mkdtemp(prefix="drymass_test_sphere_")
    with qpimage.QPSeries(h5file=path, h5mode="w") as qps:
        for ii in range(num):
            qps.add_qpimage(qpi, identifier="test_{}".format(ii))
    return qpi, path, dout
Ejemplo n.º 18
0
def test_bg_overlap():
    size = 200
    x = np.arange(size).reshape(-1, 1)
    y = np.arange(size).reshape(1, -1)
    # 5 px between regions
    cx1 = 100
    cy1 = 100
    cx2 = 100
    cy2 = 145
    radius = 20
    r1 = np.sqrt((x - cx1)**2 + (y - cy1)**2)
    r2 = np.sqrt((x - cx2)**2 + (y - cy2)**2)
    raw_pha = (r1 < radius) * 1.3
    bg_pha = (r2 < radius) * 1.2
    # create data set
    qpi = qpimage.QPImage(data=raw_pha,
                          bg_data=bg_pha,
                          which_data="phase",
                          meta_data={"pixel size": 1e-6})
    slices1 = search.search_phase_objects(qpi=qpi,
                                          size_m=2 * radius *
                                          qpi["pixel size"],
                                          exclude_overlap=0)
    slices2 = search.search_phase_objects(qpi=qpi,
                                          size_m=2 * radius * 1e-6,
                                          exclude_overlap=10)
    assert len(slices1) == 1
    assert len(slices2) == 0
Ejemplo n.º 19
0
def setup_test_data(radius=30,
                    pxsize=1e-6,
                    size=200,
                    cx=80,
                    cy=120,
                    num=1,
                    identifier=None,
                    medium_index=1.335,
                    wavelength=550e-9,
                    bg=None):
    x = np.arange(size).reshape(-1, 1)
    y = np.arange(size).reshape(1, -1)
    r = np.sqrt((x - cx)**2 + (y - cy)**2)
    image = (r < radius) * 1.3
    if bg is not None:
        image += bg
    qpi = qpimage.QPImage(data=image,
                          which_data="phase",
                          meta_data={
                              "pixel size": pxsize,
                              "medium index": medium_index,
                              "wavelength": wavelength
                          })
    path = tempfile.mktemp(suffix=".h5", prefix="drymass_test_roi")
    dout = tempfile.mkdtemp(prefix="drymass_test_roi_")
    with qpimage.QPSeries(h5file=path, identifier=identifier) as qps:
        for ii in range(num):
            qpid = "{}_test_{}".format(identifier, ii)
            qps.add_qpimage(qpi, identifier=qpid)
    return qpi, path, dout
Ejemplo n.º 20
0
def test_properties():
    size = 50
    pha = np.repeat(np.linspace(0, 10, size), size)
    pha = pha.reshape(size, size)
    amp = np.linspace(.95, 1.05, size**2).reshape(size, size)

    bg_amp = np.linspace(.93, 1.02, size**2).reshape(size, size)
    bg_pha = pha * .5

    qpi = qpimage.QPImage((pha, amp),
                          bg_data=(bg_pha, bg_amp),
                          which_data="phase,amplitude",
                          h5dtype="float64")

    assert np.all(qpi.bg_amp == bg_amp)
    assert np.all(qpi.bg_pha == bg_pha)

    assert np.iscomplexobj(qpi.field)
    fval = -0.46418608511978926 + 0.91376822116221712j
    assert np.allclose(qpi.field[20, 20], fval)

    assert qpi.shape == (size, size)

    qpi.compute_bg(which_data=["phase", "amplitude"],
                   fit_offset="fit",
                   fit_profile="tilt",
                   border_px=5)

    assert not np.all(qpi.bg_amp == bg_amp)
    assert not np.all(qpi.bg_pha == bg_pha)
Ejemplo n.º 21
0
    def handle_event(self, event_data):
        """Handle a new event"""

        # retrieve the hologram image
        # here is just an example, as our rtdc file does
        # not have a hologram, so we use a fake dataset (see above)
        event_data.images[0]  # should be the hologram

        # create some fake noise
        noise = np.random.rand(img_crop.shape[-1], img_crop.shape[-2])
        # use qpimage to process the hologram
        qpi = qpimage.QPImage(data=img_crop + noise, which_data="hologram")
        field = qpi.field

        # example nrefocus parameters
        # would be faster if this interval was smaller, see the tests
        focus_range = (20e-6, 30e-6)
        pixel_size = 0.34e-6
        light_wavelength = 532e-9
        # use nrefocus to calculate autofocus
        afocus = nrefocus.RefocusPyFFTW(field,
                                        light_wavelength,
                                        pixel_size,
                                        padding=False)
        focus_distance = afocus.autofocus(focus_range)

        # send to objective the absolute focus value
        # this value must first be calibrated with the z-stage
        # via a log file? Write the focus values to log?
        # emulate here with print
        print(focus_distance)

        return False
Ejemplo n.º 22
0
def setup_test_data(radius_px=30,
                    size=200,
                    pxsize=1e-6,
                    medium_index=1.335,
                    wavelength=550e-9,
                    num=1):
    x = np.arange(size).reshape(-1, 1)
    y = np.arange(size).reshape(1, -1)
    cx = 80
    cy = 120
    r = np.sqrt((x - cx)**2 + (y - cy)**2)
    image = (r < radius_px) * 1.3
    qpi = qpimage.QPImage(data=image,
                          which_data="phase",
                          meta_data={
                              "pixel size": pxsize,
                              "medium index": medium_index,
                              "wavelength": wavelength
                          })
    path = tempfile.mktemp(suffix=".h5", prefix="drymass_test_sphere")
    dout = tempfile.mkdtemp(prefix="drymass_test_sphere_")
    identifier = "abcdef:123456:a1b2c3"
    with qpimage.QPSeries(h5file=path, identifier=identifier) as qps:
        for ii in range(num):
            qps.add_qpimage(qpi, identifier="test_{}".format(ii))
    return qpi, path, dout
Ejemplo n.º 23
0
def test_bad_keys_error():
    size = 200
    phase = np.repeat(np.linspace(0, np.pi, size), size)
    phase = phase.reshape(size, size)
    bgphase = np.sqrt(np.abs(phase))

    qpi = qpimage.QPImage(phase, bg_data=bgphase, which_data="phase")
    clspha = qpi._pha
    try:
        clspha.del_bg("peter")
    except ValueError:
        pass
    else:
        assert False

    try:
        clspha.get_bg("peter")
    except ValueError:
        pass
    else:
        assert False

    try:
        clspha.set_bg(bg=None, key="peter")
    except ValueError:
        pass
    else:
        assert False
Ejemplo n.º 24
0
def test_use_meta_data_from_experiment():
    tmp_path = pathlib.Path(tempfile.mkdtemp())
    # create test dataset
    radius_px = 30
    size = 200
    pxsize = 1e-6
    wavelength = 512e-9
    medium_index = 1.33123
    x = np.arange(size).reshape(-1, 1)
    y = np.arange(size).reshape(1, -1)
    cx = 80
    cy = 120
    r = np.sqrt((x - cx)**2 + (y - cy)**2)
    pha = (r < radius_px) * 1.3
    amp = .5 + np.roll(pha, 10) / pha.max()
    qpi_path = tmp_path / "test.h5"
    with qpimage.QPImage(data=(pha, amp),
                         which_data="phase,amplitude",
                         h5file=qpi_path,
                         meta_data={
                             "pixel size": pxsize,
                             "wavelength": wavelength,
                             "medium index": medium_index
                         }):
        pass
    # this must run without any user input required
    pin, pout = dialog.main(
        path=qpi_path,
        req_meta=["pixel size um", "wavelength nm", "medium index"],
    )
    assert str(pin) == str(qpi_path.resolve())
    cfg = config.ConfigFile(path=pout)
    assert cfg["meta"]["medium index"] == medium_index
    assert cfg["meta"]["pixel size um"] == pxsize * 1e6
    assert cfg["meta"]["wavelength nm"] == wavelength * 1e9
    def handle_event(self, event_data: EventData) -> bool:
        """Check that the chosen features were transferred"""

        # retrieve the hologram image
        # here is just an example, as our rtdc file does
        # not have a hologram, so we use a fake dataset (see above)
        event_data.images[0]  # should be the hologram

        # use qpimage to process the hologram
        qpi = qpimage.QPImage(data=self.hologram, which_data="hologram")
        field = qpi.field

        # example nrefocus parameters
        if self.coarse_search:
            focus_range = (20.0e-6, 30.0e-6)
        else:
            focus_range = (26.0e-6, 26.3e-6)
        pixel_size = 0.34e-6
        light_wavelength = 532e-9
        default_focus = 0
        # use nrefocus to calculate autofocus
        afocus = nrefocus.RefocusPyFFTW(
            field, light_wavelength, pixel_size,
            distance=default_focus, padding=False)
        focus_distance = afocus.autofocus(focus_range)

        assert np.isclose(focus_distance, 2.61756e-05)

        return False
Ejemplo n.º 26
0
def test_info():
    size = 50
    data = np.zeros((size, size), dtype=float)
    mask = np.zeros_like(data, dtype=bool)
    mask[::2, ::2] = True
    qpi = qpimage.QPImage(data=data,
                          which_data="phase",
                          meta_data={
                              "wavelength": 300e-9,
                              "pixel size": .12e-6,
                          })
    # mask with border
    qpi.compute_bg(which_data="phase",
                   fit_offset="mean",
                   fit_profile="offset",
                   from_mask=mask,
                   border_px=5)

    info_dict = dict(qpi.info)
    assert 'phase background from mask' in info_dict
    assert 'amplitude background from mask' not in info_dict
    assert info_dict["phase background fit_offset"] == "mean"
    assert info_dict["phase background border_px"] == 5

    qpi.compute_bg(which_data="amplitude",
                   fit_offset="mean",
                   fit_profile="offset",
                   border_px=5)
    info_dict2 = dict(qpi.info)
    assert not info_dict2['amplitude background from mask']
Ejemplo n.º 27
0
def test_equals():
    h5file = pathlib.Path(__file__).parent / "data" / "bg_tilt.h5"
    qpi = qpimage.QPImage(h5file=h5file, h5mode="r")

    qpi1 = qpi.copy()
    assert qpi1 == qpi

    qpi1["wavelength"] = 123
    assert qpi1 != qpi

    # change phase data
    qpi2 = qpi.copy()
    qpi2.compute_bg(which_data="phase",
                    fit_offset="mean",
                    fit_profile="offset",
                    border_perc=10)

    # change amplitude data
    qpi3 = qpi.copy()
    qpi3.set_bg_data(bg_data=.01 + qpi3.amp, which_data="field")
    assert qpi3 != qpi

    # change identifier (still same data)
    qpi4 = qpi.copy()
    qpi4["identifier"] = "test"
    assert qpi4 == qpi
Ejemplo n.º 28
0
def test_poly2o():
    """simple 2nd order polynomial test
    """
    size = 40
    mx = .5
    my = -.3
    mxy = .1
    ax = -.05
    ay = .04
    x = np.linspace(-10, 10, size).reshape(-1, 1)
    y = np.linspace(-10, 10, size).reshape(1, -1)
    off = 1

    phase = off \
        + ax * x ** 2 \
        + ay * y ** 2 \
        + mx * x \
        + my * y \
        + mxy * x * y

    qpi = qpimage.QPImage(data=phase, which_data="phase", h5dtype="float64")
    qpi.compute_bg(which_data="phase",
                   fit_profile="poly2o",
                   from_mask=np.ones_like(phase, dtype=bool))

    assert not np.allclose(phase, 0, atol=1e-14, rtol=0)
    assert np.allclose(qpi.pha, 0, atol=1e-14, rtol=0)
Ejemplo n.º 29
0
def test_series_hdf5_hardlink_bg():
    # save compression
    compr = qpimage.image_data.COMPRESSION.copy()
    # disable compression
    qpimage.image_data.COMPRESSION = {}
    # start test
    size = 200
    phase = np.repeat(np.linspace(0, np.pi, size), size)
    phase = phase.reshape(size, size)
    bgphase = np.sqrt(np.abs(phase)) + .4

    qpi1 = qpimage.QPImage(data=phase, which_data="phase",
                           bg_data=bgphase, h5dtype="float64")
    qpi2 = qpimage.QPImage(data=phase, which_data="phase", h5dtype="float64")

    tf = tempfile.mktemp(suffix=".h5", prefix="qpimage_test_hardlink_")
    tf = pathlib.Path(tf)
    with qpimage.QPSeries(h5file=tf, h5mode="w") as qps:
        qps.h5.flush()
        s_init = tf.stat().st_size

        qps.add_qpimage(qpi1)
        qps.h5.flush()
        s_bg1 = tf.stat().st_size

        qps.add_qpimage(qpi2, bg_from_idx=0)
        qps.h5.flush()
        s_bg2 = tf.stat().st_size

        qps.add_qpimage(qpi1)
        qps.h5.flush()
        s_bg3 = tf.stat().st_size

        qpih = qps[1].copy()

    assert s_bg2 - s_bg1 < .51 * (s_bg1 - s_init)
    assert s_bg2 - s_bg1 < .51 * (s_bg3 - s_bg2)
    assert np.allclose(qpih.pha, qpi1.pha)
    assert not np.allclose(qpih.pha, qpi2.pha)

    # restore compression
    qpimage.image_data.COMPRESSION = compr
    # cleanup
    try:
        tf.unlink()
    except OSError:
        pass
Ejemplo n.º 30
0
def test_get_amp_pha_nan():
    size = 50
    pha = np.repeat(np.linspace(0, 10, size), size)
    pha = pha.reshape(size, size)
    phanan = pha.copy()
    phanan[:4] = np.nan
    qpi = qpimage.QPImage(phanan, which_data="phase")
    assert np.allclose(qpi.pha, phanan, equal_nan=True)