Skip to content

Commit

Permalink
tests: improve coverage (#173)
Browse files Browse the repository at this point in the history
  • Loading branch information
paulmueller committed May 26, 2018
1 parent 1be3daa commit 7a27b1a
Show file tree
Hide file tree
Showing 3 changed files with 100 additions and 1 deletion.
1 change: 1 addition & 0 deletions .gitignore
Expand Up @@ -81,3 +81,4 @@ tests/data
.env

.pytest_cache
htmlcov
1 change: 0 additions & 1 deletion pycorrfit/openfile.py
Expand Up @@ -3,7 +3,6 @@
This file contains definitions for opening PyCorrFit sessions and
saving PyCorrFit correlation curves.
"""

import codecs
import csv
import io
Expand Down
99 changes: 99 additions & 0 deletions tests/test_session.py
@@ -0,0 +1,99 @@
"""Session files"""
import os
import pathlib
import tempfile
import shutil

import numpy as np
import pytest

import data_file_dl
import pycorrfit as pcf

NOAPITOKEN = "GITHUB_API_TOKEN" not in os.environ

examplefile = "Zeiss_Confocor3_LSM780_FCCS_HeLa_2015/019_cp_KIND+BFA.fcs"


@pytest.mark.xfail(NOAPITOKEN, reason="Restrictions to GitHub API")
def test_basic():
"""This is a very rudimentary test for the session handling"""
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)

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.git_ival, ldt["Parameters"][0][4])

shutil.rmtree(tmpdir, ignore_errors=True)


if __name__ == "__main__":
# Run all tests
loc = locals()
for key in list(loc.keys()):
if key.startswith("test_") and hasattr(loc[key], "__call__"):
loc[key]()

0 comments on commit 7a27b1a

Please sign in to comment.