Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

296 Add memory usage to PyOPIA log output #328

Draft
wants to merge 5 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 8 additions & 2 deletions pyopia/background.py
Original file line number Diff line number Diff line change
Expand Up @@ -56,8 +56,14 @@ def shift_bgstack_accurate(bgstack, imbg, imnew):
updated actual background image
'''
bgstack.pop(0) # pop the oldest image from the stack,
bgstack.append(imnew) # append the new image to the stack
imbg = np.mean(bgstack, axis=0)

# Append the new image to the stack. Convert to uint8 from float [0-1],
# to save memory for large bgstacks, and speed up mean calculation of mean.
bgstack.append((255 * imnew).astype(np.uint8))

# Calculate mean image (float [0 - 1])
imbg = np.mean(bgstack, axis=0) / 255
emlynjdavies marked this conversation as resolved.
Show resolved Hide resolved

return bgstack, imbg


Expand Down
14 changes: 13 additions & 1 deletion pyopia/cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
import rich.progress
import pandas as pd
import multiprocessing

import psutil
import pyopia
import pyopia.background
import pyopia.instrument.silcam
Expand All @@ -30,6 +30,15 @@
app = typer.Typer()


def log_memory_usage():
logger = logging.getLogger()
process = psutil.Process(os.getpid())
total = psutil.virtual_memory().total / 1e9
used = process.memory_info().rss / 1e9
used_percent = used / total * 100
logger.info(f'Current memory usage is: {used:.1f} / {total:.1f} GB ({used_percent:3.0f} %)')


@app.command()
def docs():
'''Open browser at PyOPIA's readthedocs page
Expand Down Expand Up @@ -186,9 +195,12 @@ def process_file_list(file_list, c):
f'(chunk {c})')
logger.error(e)
logger.debug(''.join(traceback.format_tb(e.__traceback__)))
finally:
log_memory_usage()

# With one chunk we keep the non-multiprocess functionality to ensure backwards compatibility
job_list = []
multiprocessing.set_start_method('fork')
if num_chunks == 1:
process_file_list(raw_files, 0)
else:
Expand Down
Loading