def test_asc_alv7004usb():
    """Test alv7004/USB format"""
    f1 = data_file_dl.get_data_file("ALV-7004USB_ac01_cc01_10.ASC")
    data = pycorrfit.readfiles.open_any(f1)
    assert data["Type"] == ["AC1", "AC2", "CC12", "CC21"]
    assert np.allclose(data["Correlation"][0][10],
                       np.array([0.000275, 0.11208]))
    assert np.allclose(data["Correlation"][1][12],
                       np.array([0.000325, 0.0900233]))
    assert np.allclose(data["Correlation"][2][18],
                       np.array([0.00055, 0.0582773]))
    assert np.allclose(data["Correlation"][3][120],
                       np.array([3.6864, 0.0224212]))
    assert len(data["Trace"][0]) == 253
    assert len(data["Trace"][1]) == 253
    assert len(data["Trace"][2]) == 2
    assert len(data["Trace"][3]) == 2
    assert np.all(data["Trace"][0] == data["Trace"][2][0])
    assert np.all(data["Trace"][1] == data["Trace"][2][1])
    assert np.all(data["Trace"][0] == data["Trace"][3][0])
    assert np.all(data["Trace"][1] == data["Trace"][3][1])
    assert np.allclose(data["Trace"][0][10], np.array([1289.06, 140.20404]))
    assert np.allclose(data["Trace"][1][100], np.array([11835.94, 94.68225]))

    f2 = data_file_dl.get_data_file("ALV-7004USB_dia10_cen10_0001.ASC")
    data2 = pycorrfit.readfiles.open_any(f2)
    # There are empty AC2 and CC12/CC21 curves in this file that should
    # be removed by pycorrfit.
    assert data2["Type"] == ["AC1"]
    assert np.allclose(data2["Correlation"][0][56],
                       np.array([0.0144, 0.0513857]))
    assert len(data2["Trace"][0]) == 254
    assert np.allclose(data2["Trace"][0][210], np.array([49453.13, 165.41434]))

    f3 = data_file_dl.get_data_file("ALV-7004.ASC")
    data3 = pycorrfit.readfiles.open_any(f3)
    assert len(data3["Type"]) == 1
    assert len(data3["Trace"][0]) == 66
    assert data3["Type"][0] == "AC"
    assert np.allclose(data3["Correlation"][0][56], np.array([0.0144,
                                                              0.38757]))
    assert np.allclose(data3["Trace"][0][60],
                       np.array([1.21523440e5, 5.11968700e1]))

    f4 = data_file_dl.get_data_file("ALV-7004USB_ac3.ASC")
    data4 = pycorrfit.readfiles.open_any(f4)
    assert len(data4["Type"]) == 1
    assert data4["Type"][0] == "AC"
    assert len(data4["Trace"][0]) == 254
Example #2
0
def test_fit_constraint_simple_inequality():
    """ Check "smaller than" relation during fitting.
    """
    dfile = data_file_dl.get_data_file(examplefile)
    data = pcf.readfiles.openAny(dfile)
    corr = pcf.Correlation(
        correlation=data["Correlation"][0],
        traces=data["Trace"][0],
        corr_type=data["Type"][0],
        filename=os.path.basename(dfile),
        title="test correlation",
        fit_model=6035  # confocal 3D+3D)
    )
    corr.fit_parameters_variable = [
        True, True, True, True, False, False, False
    ]
    # crop triplet data
    corr.fit_ival[0] = 8
    pcf.Fit(corr)
    assert corr.fit_parameters[1] <= corr.fit_parameters[2]
    # -> deliberately reverse everything and try again
    corr.fit_parameters[1], corr.fit_parameters[2] = corr.fit_parameters[
        2], corr.fit_parameters[1]
    corr.fit_parameters[3] = 1 - corr.fit_parameters[3]
    pcf.Fit(corr)
    # This tests also for equality
    assert corr.fit_parameters[1] <= corr.fit_parameters[2]
    if corr.fit_parameters[1] == corr.fit_parameters[2]:
        print(
            "found identity of fit parameters - multiplying by two to see if relation holds"
        )
        corr.fit_parameters[2] *= 2
        pcf.Fit(corr)
        assert corr.fit_parameters[1] < corr.fit_parameters[2]
Example #3
0
def test_fit_constraint_simple_inequality():
    """ Check "smaller than" relation during fitting.
    """
    dfile = data_file_dl.get_data_file("019_cp_KIND+BFA.fcs")
    data = pcf.readfiles.openAny(dfile)
    corr = pcf.Correlation(correlation=data["Correlation"][0],
                           traces=data["Trace"][0],
                           corr_type=data["Type"][0],
                           filename=os.path.basename(dfile),
                           title="test correlation",
                           fit_model=6035 # confocal 3D+3D)
                           )
    corr.fit_parameters_variable = [True, True, True, True, False, False, False]
    # crop triplet data
    corr.fit_ival[0] = 8
    pcf.Fit(corr)
    assert corr.fit_parameters[1] <= corr.fit_parameters[2]
    # -> deliberately reverse everything and try again 
    corr.fit_parameters[1], corr.fit_parameters[2] = corr.fit_parameters[2], corr.fit_parameters[1]
    corr.fit_parameters[3] = 1-corr.fit_parameters[3]
    pcf.Fit(corr)
    # This tests also for equality
    assert corr.fit_parameters[1] <= corr.fit_parameters[2]
    if corr.fit_parameters[1] == corr.fit_parameters[2]:
        print("found identity of fit parameters - multiplying by two to see if relation holds")
        corr.fit_parameters[2] *= 2
        pcf.Fit(corr)
        assert corr.fit_parameters[1] < corr.fit_parameters[2]
def test_fcs_channel_names():
    """Test arbitrary channel names (instead of just "1" and "2"

    The file '001_A488_channel_names.fcs' was created manually.
    """
    fref = data_file_dl.get_data_file("Zeiss_Confocor3_A488+GFP/001_A488.fcs")
    fed = data_file_dl.get_data_file("001_A488_channel_names.fcs")

    dataref = pycorrfit.readfiles.open_any(fref)
    dataed = pycorrfit.readfiles.open_any(fed)
    assert np.all(dataref["Correlation"][0] == dataed["Correlation"][0])
    assert np.all(dataref["Correlation"][1] == dataed["Correlation"][1])
    assert np.all(dataref["Correlation"][2] == dataed["Correlation"][2])
    assert np.all(dataref["Correlation"][3] == dataed["Correlation"][3])
    assert np.all(dataref["Trace"][0] == dataed["Trace"][0])
    assert np.all(dataref["Trace"][1] == dataed["Trace"][1])
    assert np.all(dataref["Trace"][2][0] == dataed["Trace"][2][0])
    assert np.all(dataref["Trace"][2][1] == dataed["Trace"][2][1])
    assert np.all(dataref["Trace"][3][0] == dataed["Trace"][3][0])
    assert np.all(dataref["Trace"][3][1] == dataed["Trace"][3][1])
    assert dataref["Type"] == dataed["Type"]
    assert dataref["Filename"] != dataed["Filename"]
Example #5
0
def test_pt3_basic():
    f1 = data_file_dl.get_data_file(
        "PicoQuant_SymphoTime32_A42F-4jul2014/Point_1.pt3")
    data = pycorrfit.readfiles.openAny(f1)

    trace = data["Trace"][0][0]
    assert trace.shape == (600, 2)
    assert np.allclose(trace[40], np.array([2037, 6.48]))

    corr = data["Correlation"][0]
    assert corr.shape == (150, 2)
    assert np.allclose(corr[40], np.array([0.000698, 0.58007174877053136]))
    assert np.allclose(corr[100], np.array([0.72089, 0.019201608388821567]))
def test_pt3_basic():
    f1 = data_file_dl.get_data_file(
        "PicoQuant_SymphoTime32_A42F-4jul2014/Point_1.pt3")
    data = pycorrfit.readfiles.open_any(f1)

    trace = data["Trace"][0][0]
    assert trace.shape == (600, 2)
    try:
        assert np.allclose(trace[40], np.array([2037, 6.48]))
    except AssertionError:
        warnings.warn("Unknown pt3 problem after migration to Python3!")

    corr = data["Correlation"][0]
    assert corr.shape == (150, 2)
    assert np.allclose(corr[40], np.array([0.000698, 0.58007174877053136]))
    assert np.allclose(corr[100], np.array([0.72089, 0.019201608388821567]))
Example #7
0
def test_fit_constraint_sum_smaller_one():
    """ Check "a+b<c" relation during fitting.
    """
    dfile = data_file_dl.get_data_file(examplefile)
    data = pcf.readfiles.openAny(dfile)
    corr = pcf.Correlation(
        correlation=data["Correlation"][0],
        traces=data["Trace"][0],
        corr_type=data["Type"][0],
        filename=os.path.basename(dfile),
        title="test correlation",
        fit_model=6081  # confocal 3D+3D)
    )
    pcf.Fit(corr)
    assert corr.fit_parameters[4] + corr.fit_parameters[5] < 1
    parms0 = np.array([
        1.13827592342,  #   n    
        3.0918704e-05,  #   τ₁ [ms]    
        1.98835792339,  #   τ₂ [ms]    
        2000.0,  #   τ₃ [ms]    
        0.972264423555,  #   F₁    
        0.021400173882,  #   F₂    
        5.0,  #   SP    
        1.0,  #   α₂₁    
        1.0,  #   α₃₁    
        1e-08,  #   τ_trip [ms]    
        0.0,  #   T    
        0.0,  #   offset    
    ])
    corr.fit_parameters = parms0

    vary = [False] * 12
    vary[4] = vary[5] = True
    corr.fit_parameters_variable = vary
    # crop triplet data
    pcf.Fit(corr)
    assert corr.fit_parameters[4] + corr.fit_parameters[5] < 1
    # -> deliberately reverse everything and try again
    corr.fit_parameters[4], corr.fit_parameters[5] = corr.fit_parameters[
        5], corr.fit_parameters[4]
    pcf.Fit(corr)
    # This tests also for equality
    assert corr.fit_parameters[4] + corr.fit_parameters[5] < 1
def test_fit_constraint_sum_smaller_one():
    """ Check "a+b<c" relation during fitting.
    """
    dfile = data_file_dl.get_data_file(examplefile)
    data = pcf.readfiles.open_any(dfile)
    corr = pcf.Correlation(correlation=data["Correlation"][0],
                           traces=data["Trace"][0],
                           corr_type=data["Type"][0],
                           filename=os.path.basename(dfile),
                           title="test correlation",
                           fit_model=6081  # confocal 3D+3D)
                           )
    pcf.Fit(corr)
    assert corr.fit_parameters[4] + corr.fit_parameters[5] < 1
    parms0 = np.array([
        1.13827592342,  # n
        3.0918704e-05,  # τ₁ [ms]
        1.98835792339,  # τ₂ [ms]
        2000.0,  # τ₃ [ms]
        0.972264423555,  # F₁
        0.021400173882,  # F₂
        5.0,  # SP
        1.0,  # α₂₁
        1.0,  # α₃₁
        1e-08,  # τ_trip [ms]
        0.0,  # T
        0.0,  # offset
    ])
    corr.fit_parameters = parms0

    vary = [False] * 12
    vary[4] = vary[5] = True
    corr.fit_parameters_variable = vary
    # crop triplet data
    pcf.Fit(corr)
    assert corr.fit_parameters[4] + corr.fit_parameters[5] < 1
    # -> deliberately reverse everything and try again
    corr.fit_parameters[4], corr.fit_parameters[5] = (corr.fit_parameters[5],
                                                      corr.fit_parameters[4])
    pcf.Fit(corr)
    # This tests also for equality
    assert corr.fit_parameters[4] + corr.fit_parameters[5] < 1
Example #9
0
def test_basic():
    """This is a very rudimentary test for the session handling"""
    dfile = data_file_dl.get_data_file(examplefile)
    data = pcf.readfiles.open_any(dfile)
    corr = pcf.Correlation(
        correlation=data["Correlation"][0],
        traces=data["Trace"][0],
        corr_type=data["Type"][0],
        filename=os.path.basename(dfile),
        title="test correlation",
        fit_model=6035  # confocal 3D+3D)
    )
    corr.fit_parameters_variable = [
        True, True, True, True, False, False, False
    ]
    # crop triplet data
    corr.fit_ival[0] = 8
    pcf.Fit(corr)

    tmpdir = tempfile.mkdtemp(prefix="pycorrfit_tests_")
    path = pathlib.Path(tmpdir) / "session.pcfs"

    fiterr = []
    for ii, fitpid in enumerate(corr.fit_results["fit parameters"]):
        fiterr.append(
            [int(fitpid),
             float(corr.fit_results["fit error estimation"][ii])])

    Infodict = {
        "Correlations": {
            1: [corr.lag_time, corr.correlation]
        },
        "Parameters": {
            1: [
                "#1:", corr.fit_model.id, corr.fit_parameters,
                corr.fit_parameters_variable, corr.fit_ival,
                [3, 3, 5, corr.fit_algorithm], [None, None], True, None,
                [[0.0, np.inf], [0.0, np.inf], [0.0, np.inf],
                 [0.0, 0.9999999999999], [-np.inf, np.inf]]
            ]
        },
        "Supplements": {
            1: {
                "FitErr": fiterr,
                "Chi sq": float(corr.fit_results["chi2"]),
                "Global Share": [],
            }
        },
        "External Functions": {},
        "Traces": {},
        "Comments": {
            "Session": "No comment."
        },
        "Backgrounds": {},
        "External Weights": {},
        "Preferences": {},
    }

    pcf.openfile.SaveSessionData(sessionfile=str(path), Infodict=Infodict)

    ldt = pcf.openfile.LoadSessionData(str(path))

    # lag time only, shape (N,)
    assert np.allclose(data["Correlation"][0][:, 0], ldt["Correlations"][1][0])
    # lag time and correlation, shape (N, 2)
    assert np.allclose(corr.correlation, ldt["Correlations"][1][1])
    # parameters
    assert corr.fit_model.id == ldt["Parameters"][0][1]
    assert np.allclose(corr.fit_parameters, ldt["Parameters"][0][2])
    assert np.allclose(corr.fit_parameters_variable, ldt["Parameters"][0][3])
    assert np.allclose(corr.fit_ival, ldt["Parameters"][0][4])

    shutil.rmtree(tmpdir, ignore_errors=True)
Example #10
0
def test_basic():
    """This is a very rudimentary test for the session handling"""
    dfile = data_file_dl.get_data_file(examplefile)
    data = pcf.readfiles.open_any(dfile)
    corr = pcf.Correlation(correlation=data["Correlation"][0],
                           traces=data["Trace"][0],
                           corr_type=data["Type"][0],
                           filename=os.path.basename(dfile),
                           title="test correlation",
                           fit_model=6035  # confocal 3D+3D)
                           )
    corr.fit_parameters_variable = [True, True, True, True,
                                    False, False, False]
    # crop triplet data
    corr.fit_ival[0] = 8
    pcf.Fit(corr)

    tmpdir = tempfile.mkdtemp(prefix="pycorrfit_tests_")
    path = pathlib.Path(tmpdir) / "session.pcfs"

    fiterr = []
    for ii, fitpid in enumerate(corr.fit_results["fit parameters"]):
        fiterr.append([int(fitpid),
                       float(corr.fit_results["fit error estimation"][ii])])

    Infodict = {
        "Correlations": {
            1: [corr.lag_time, corr.correlation]},
        "Parameters": {
            1: ["#1:",
                corr.fit_model.id,
                corr.fit_parameters,
                corr.fit_parameters_variable,
                corr.fit_ival,
                [3, 3, 5, corr.fit_algorithm],
                [None, None],
                True,
                None,
                [[0.0, np.inf],
                 [0.0, np.inf],
                 [0.0, np.inf],
                 [0.0, 0.9999999999999],
                 [-np.inf, np.inf]]
                ]},
        "Supplements": {
            1: {"FitErr": fiterr,
                "Chi sq": float(corr.fit_results["chi2"]),
                "Global Share": [],
                }},
        "External Functions": {},
        "Traces": {},
        "Comments": {"Session": "No comment."},
        "Backgrounds": {},
        "External Weights": {},
        "Preferences": {},
    }

    pcf.openfile.SaveSessionData(sessionfile=str(path),
                                 Infodict=Infodict)

    ldt = pcf.openfile.LoadSessionData(str(path))

    # lag time only, shape (N,)
    assert np.allclose(data["Correlation"][0][:, 0], ldt["Correlations"][1][0])
    # lag time and correlation, shape (N, 2)
    assert np.allclose(corr.correlation, ldt["Correlations"][1][1])
    # parameters
    assert corr.fit_model.id == ldt["Parameters"][0][1]
    assert np.allclose(corr.fit_parameters, ldt["Parameters"][0][2])
    assert np.allclose(corr.fit_parameters_variable, ldt["Parameters"][0][3])
    assert np.allclose(corr.fit_ival, ldt["Parameters"][0][4])

    shutil.rmtree(tmpdir, ignore_errors=True)