Skip to content

Commit

Permalink
Use file lock when writing results to prevent overwriting
Browse files Browse the repository at this point in the history
  • Loading branch information
ethanbb committed Jun 7, 2024
1 parent e571706 commit 6d860b7
Show file tree
Hide file tree
Showing 7 changed files with 76 additions and 21 deletions.
1 change: 1 addition & 0 deletions environment.yml
Original file line number Diff line number Diff line change
Expand Up @@ -8,3 +8,4 @@ dependencies:
- jupyterlab
- tqdm
- psutil
- filelock
1 change: 1 addition & 0 deletions environment_rtd.yml
Original file line number Diff line number Diff line change
Expand Up @@ -8,3 +8,4 @@ dependencies:
- tqdm
- psutil
- pydata-sphinx-theme < 0.10.0
- filelock
31 changes: 24 additions & 7 deletions mesmerize_core/algorithms/cnmf.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
import os
import time
from datetime import datetime
from filelock import FileLock, Timeout

# prevent circular import
if __name__ in ["__main__", "__mp_main__"]: # when running in subprocess
Expand Down Expand Up @@ -138,13 +139,29 @@ def run_algo(batch_path, uuid, data_path: str = None):

cm.stop_server(dview=dview)

# Add dictionary to output column of series
df.loc[df["uuid"] == uuid, "outputs"] = [d]
# Add ran timestamp to ran_time column of series
df.loc[df["uuid"] == uuid, "ran_time"] = datetime.now().isoformat(timespec="seconds", sep="T")
df.loc[df["uuid"] == uuid, "algo_duration"] = str(round(time.time() - algo_start, 2)) + " sec"
# save dataframe to disc
df.to_pickle(batch_path)
# lock batch file while writing back results
batch_lock = FileLock(batch_path + '.lock', timeout=30)
try:
with batch_lock:
df = load_batch(batch_path)

# Add dictionary to output column of series
df.loc[df["uuid"] == uuid, "outputs"] = [d]
# Add ran timestamp to ran_time column of series
df.loc[df["uuid"] == uuid, "ran_time"] = datetime.now().isoformat(timespec="seconds", sep="T")
df.loc[df["uuid"] == uuid, "algo_duration"] = str(round(time.time() - algo_start, 2)) + " sec"
# save dataframe to disc
df.to_pickle(batch_path)
except Timeout:
# Print a message with details in lieu of writing to the batch file
msg = f"Batch file could not be written to within {batch_lock.timeout} seconds."
if d["success"]:
msg += f"\nRun succeeded; results are in {output_dir}."
else:
msg += f"Run failed. Traceback:\n"
msg += d["traceback"]

raise RuntimeError(msg)


@click.command()
Expand Down
31 changes: 24 additions & 7 deletions mesmerize_core/algorithms/cnmfe.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
import os
import time
from datetime import datetime
from filelock import FileLock, Timeout

if __name__ in ["__main__", "__mp_main__"]: # when running in subprocess
from mesmerize_core import set_parent_raw_data_path, load_batch
Expand Down Expand Up @@ -121,13 +122,29 @@ def run_algo(batch_path, uuid, data_path: str = None):

cm.stop_server(dview=dview)

# Add dictionary to output column of series
df.loc[df["uuid"] == uuid, "outputs"] = [d]
# Add ran timestamp to ran_time column of series
df.loc[df["uuid"] == uuid, "ran_time"] = datetime.now().isoformat(timespec="seconds", sep="T")
df.loc[df["uuid"] == uuid, "algo_duration"] = str(round(time.time() - algo_start, 2)) + " sec"
# save dataframe to disc
df.to_pickle(batch_path)
# lock batch file while writing back results
batch_lock = FileLock(batch_path + '.lock', timeout=30)
try:
with batch_lock:
df = load_batch(batch_path)

# Add dictionary to output column of series
df.loc[df["uuid"] == uuid, "outputs"] = [d]
# Add ran timestamp to ran_time column of series
df.loc[df["uuid"] == uuid, "ran_time"] = datetime.now().isoformat(timespec="seconds", sep="T")
df.loc[df["uuid"] == uuid, "algo_duration"] = str(round(time.time() - algo_start, 2)) + " sec"
# save dataframe to disc
df.to_pickle(batch_path)
except Timeout:
# Print a message with details in lieu of writing to the batch file
msg = f"Batch file could not be written to within {batch_lock.timeout} seconds."
if d["success"]:
msg += f"\nRun succeeded; results are in {output_dir}."
else:
msg += f"Run failed. Traceback:\n"
msg += d["traceback"]

raise RuntimeError(msg)


@click.command()
Expand Down
31 changes: 24 additions & 7 deletions mesmerize_core/algorithms/mcorr.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
from shutil import move as move_file
import time
from datetime import datetime
from filelock import FileLock, Timeout


# prevent circular import
Expand Down Expand Up @@ -148,13 +149,29 @@ def run_algo(batch_path, uuid, data_path: str = None):

cm.stop_server(dview=dview)

# Add dictionary to output column of series
df.loc[df["uuid"] == uuid, "outputs"] = [d]
# Add ran timestamp to ran_time column of series
df.loc[df["uuid"] == uuid, "ran_time"] = datetime.now().isoformat(timespec="seconds", sep="T")
df.loc[df["uuid"] == uuid, "algo_duration"] = str(round(time.time() - algo_start, 2)) + " sec"
# Save DataFrame to disk
df.to_pickle(batch_path)
# lock batch file while writing back results
batch_lock = FileLock(batch_path + '.lock', timeout=30)
try:
with batch_lock:
df = load_batch(batch_path)

# Add dictionary to output column of series
df.loc[df["uuid"] == uuid, "outputs"] = [d]
# Add ran timestamp to ran_time column of series
df.loc[df["uuid"] == uuid, "ran_time"] = datetime.now().isoformat(timespec="seconds", sep="T")
df.loc[df["uuid"] == uuid, "algo_duration"] = str(round(time.time() - algo_start, 2)) + " sec"
# Save DataFrame to disk
df.to_pickle(batch_path)
except Timeout:
# Print a message with details in lieu of writing to the batch file
msg = f"Batch file could not be written to within {batch_lock.timeout} seconds."
if d["success"]:
msg += f"\nRun succeeded; results are in {output_dir}."
else:
msg += f"Run failed. Traceback:\n"
msg += d["traceback"]

raise RuntimeError(msg)


@click.command()
Expand Down
1 change: 1 addition & 0 deletions requirements.txt
Original file line number Diff line number Diff line change
Expand Up @@ -9,3 +9,4 @@ matplotlib
click
psutil
jupyterlab
filelock
1 change: 1 addition & 0 deletions setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
"click",
"psutil",
"jupyterlab",
"filelock"
]


Expand Down

0 comments on commit 6d860b7

Please sign in to comment.