Skip to content

Commit

Permalink
FIX randomize tempfolder
Browse files Browse the repository at this point in the history
  • Loading branch information
GeorgWa committed Oct 4, 2023
1 parent fcac4fb commit 0c89338
Showing 1 changed file with 37 additions and 25 deletions.
62 changes: 37 additions & 25 deletions tests/unit_tests/test_reporting.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,17 @@

from alphadia.extraction.workflow import reporting

def _random_tempfolder():
tempdir = tempfile.gettempdir()
# 6 alphanumeric characters
random_foldername = "".join(np.random.choice(list("abcdefghijklmnopqrstuvwxyz0123456789"), 6))
path = os.path.join(tempdir, random_foldername)
os.mkdir(path)
return path

def test_logging():

tempfolder = tempfile.gettempdir()
tempfolder = _random_tempfolder()

if os.path.exists(os.path.join(tempfolder, "log.txt")):
os.remove(os.path.join(tempfolder, "log.txt"))
Expand Down Expand Up @@ -44,67 +52,71 @@ def test_backend():

def test_figure_backend():

figure_backend = reporting.FigureBackend(path = tempfile.gettempdir())
tempfolder = _random_tempfolder()

figure_backend = reporting.FigureBackend(path = tempfolder)

fig, ax = plt.subplots(1, 1, figsize=(5, 5))
ax.scatter(np.random.rand(10), np.random.rand(10))

figure_backend.log_figure("scatter", fig)
plt.close(fig)

assert os.path.exists(os.path.join(tempfile.gettempdir(), figure_backend.FIGURE_PATH, "scatter.png"))
os.remove(os.path.join(tempfile.gettempdir(), figure_backend.FIGURE_PATH, "scatter.png"))
assert os.path.exists(os.path.join(tempfolder, figure_backend.FIGURE_PATH, "scatter.png"))
os.remove(os.path.join(tempfolder, figure_backend.FIGURE_PATH, "scatter.png"))
time.sleep(1)

test_figure_backend()

def test_jsonl_backend():

tempfolder = _random_tempfolder()

with reporting.JSONLBackend(path = tempfile.gettempdir()) as jsonl_backend:
with reporting.JSONLBackend(path = tempfolder) as jsonl_backend:
jsonl_backend.log_event("start_extraction", None)
jsonl_backend.log_metric("accuracy", 0.9)
jsonl_backend.log_string("test")

assert os.path.exists(os.path.join(tempfile.gettempdir(), "events.jsonl"))
with open(os.path.join(tempfile.gettempdir(), "events.jsonl"), "r") as f:
assert os.path.exists(os.path.join(tempfolder, "events.jsonl"))
with open(os.path.join(tempfolder, "events.jsonl"), "r") as f:
assert len(f.readlines()) == 5
time.sleep(1)
os.remove(os.path.join(tempfile.gettempdir(), "events.jsonl"))
os.remove(os.path.join(tempfolder, "events.jsonl"))
time.sleep(1)

test_jsonl_backend()

def test_log_backend():

tempdir = tempfile.gettempdir()
tempfolder = _random_tempfolder()

if os.path.exists(os.path.join(tempdir, "log.txt")):
os.remove(os.path.join(tempdir, "log.txt"))
if os.path.exists(os.path.join(tempfolder, "log.txt")):
os.remove(os.path.join(tempfolder, "log.txt"))

stdout_backend = reporting.LogBackend(path = tempdir)
stdout_backend = reporting.LogBackend(path = tempfolder)
stdout_backend.log_string("test", verbosity='progress')
stdout_backend.log_string("test", verbosity='info')
stdout_backend.log_string("test", verbosity='warning')
stdout_backend.log_string("test", verbosity='error')
stdout_backend.log_string("test", verbosity='critical')

assert os.path.exists(os.path.join(tempdir, "log.txt"))
with open(os.path.join(tempdir, "log.txt"), "r") as f:
assert os.path.exists(os.path.join(tempfolder, "log.txt"))
with open(os.path.join(tempfolder, "log.txt"), "r") as f:
assert len(f.readlines()) == 5
time.sleep(1)
os.remove(os.path.join(tempdir, "log.txt"))
os.remove(os.path.join(tempfolder, "log.txt"))

test_log_backend()

def test_pipeline():

tempdir = tempfile.gettempdir()
tempfolder = _random_tempfolder()

pipeline = reporting.Pipeline(
backends = [
reporting.LogBackend(path = tempdir),
reporting.JSONLBackend(path = tempdir),
reporting.FigureBackend(path = tempdir)
reporting.LogBackend(path = tempfolder),
reporting.JSONLBackend(path = tempfolder),
reporting.FigureBackend(path = tempfolder)
]
)

Expand All @@ -119,13 +131,13 @@ def test_pipeline():
pipeline.log_figure("scatter", fig)
plt.close(fig)

assert os.path.exists(os.path.join(tempdir, "log.txt"))
assert os.path.exists(os.path.join(tempdir, "events.jsonl"))
assert os.path.exists(os.path.join(tempdir, "figures", "scatter.png"))
assert os.path.exists(os.path.join(tempfolder, "log.txt"))
assert os.path.exists(os.path.join(tempfolder, "events.jsonl"))
assert os.path.exists(os.path.join(tempfolder, "figures", "scatter.png"))

os.remove(os.path.join(tempdir, "log.txt"))
os.remove(os.path.join(tempdir, "events.jsonl"))
os.remove(os.path.join(tempdir, "figures", "scatter.png"))
os.remove(os.path.join(tempfolder, "log.txt"))
os.remove(os.path.join(tempfolder, "events.jsonl"))
os.remove(os.path.join(tempfolder, "figures", "scatter.png"))

# sleep 1 second to ensure that the file has been deleted
time.sleep(1)
Expand Down

0 comments on commit 0c89338

Please sign in to comment.