Skip to content

Commit

Permalink
added log_code
Browse files Browse the repository at this point in the history
  • Loading branch information
ARSadri committed Mar 26, 2024
1 parent 7155846 commit 2a27394
Show file tree
Hide file tree
Showing 7 changed files with 48 additions and 28 deletions.
8 changes: 7 additions & 1 deletion HISTORY.rst
Original file line number Diff line number Diff line change
Expand Up @@ -244,4 +244,10 @@ History
* is_builtin_collection is added for multiprocessor concatenation
* setting time_tag to 'counter' or 'counter&time' will count filenames instead of time
* plt_violinplot was added
* plt_imhist is added
* plt_imhist is added

0.11.1 (2024-03-26)
------------------
* plt_imshow_by_subplots takes stacks as well now.
* log_code is added, pass __file__ for current script to be logged.
* multiple plt_imhist is possible
2 changes: 1 addition & 1 deletion lognflow/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

__author__ = 'Alireza Sadri'
__email__ = '[email protected]'
__version__ = '0.11.0'
__version__ = '0.11.1'

from .lognflow import lognflow
from .logviewer import logviewer
Expand Down
19 changes: 14 additions & 5 deletions lognflow/lognflow.py
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@
from pathlib import Path as pathlib_Path
from itertools import product as itertools_product
from sys import platform as sys_platform
from sys import argv as sys_argv
from os import system as os_system
from tempfile import gettempdir
from dataclasses import dataclass
Expand Down Expand Up @@ -209,6 +210,14 @@ def disable(self):
def enable(self):
self.enabled = True

def log_code(self, code_fpath = None):
""" log code, pass __file__
"""
if code_fpath is None:
code_fpath = sys_argv[0]
code_fpath = pathlib_Path(code_fpath)
self.copy(code_fpath.name, code_fpath)

def name_from_file(self, fpath):
"""
Given an fpath inside the logger log_dir,
Expand Down Expand Up @@ -1228,11 +1237,11 @@ def log_imshow_by_subplots(self,
:param frame_shape:
n_f images will be tiles according to thi tuple as shape.
:param grid_locations:
if this is of shape n_images x 2, then each subplot will be located
at a specific given location.
To make it beautiful, you better proveide figsize and im_sizes or
im_size_factor to merely scale them to cover a small region between
0 and 1.
if this is of shape n_images x 2, then each subplot will be
located at a specific given location.
To make it beautiful, you better proveide figsize and im_sizes
or im_size_factor to merely scale them to cover a small region
between 0 and 1.
:param time_tag: bool
Wheather if the time stamp is in the file name or not.
"""
Expand Down
19 changes: 8 additions & 11 deletions lognflow/plt_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,7 @@ def plt_violinplot(
return fig, ax

class plt_imhist:
def __init__(self, in_image, figsize=(12, 6),
def __init__(self, in_image, figsize=(12, 6), title = None,
kwargs_for_imshow = {}, kwargs_for_hist = {}):
# Adjust figsize to provide more space if needed
self.fig, axs = plt.subplots(
Expand All @@ -96,6 +96,8 @@ def __init__(self, in_image, figsize=(12, 6),

# Display the image
self.im = axs[0].imshow(in_image, **kwargs_for_imshow)
if title is not None:
axs[0].set_title(title)
axs[0].axis('off') # Hide axes for the image

cm = self.im.get_cmap()
Expand Down Expand Up @@ -136,8 +138,6 @@ def __init__(self, in_image, figsize=(12, 6),

self.slider.on_changed(self.update)

plt.show()

def update(self, val):
self.im.set_clim(val[0], val[1])
self.lower_limit_line.set_ydata([val[0], val[0]])
Expand Down Expand Up @@ -417,7 +417,10 @@ def show(self, show_legend = True):
plt.grid()

plt.show()


def __call__(self, *args, **kwargs):
self.addPlot(*args, **kwargs)

def imshow_series(list_of_stacks,
list_of_masks = None,
figsize_ratio = 1,
Expand Down Expand Up @@ -623,10 +626,4 @@ def imshow_by_subplots(
ax_local.xaxis.set_ticks_position('none')
ax_local.yaxis.set_ticks_position('none')

return fig, ax
else:
self.log_text(
self.log_name,
f'Cannot imshow variable {parameter_name} with shape' + \
f'{stack.shape}')
return
return fig, ax
2 changes: 1 addition & 1 deletion setup.cfg
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
[bumpversion]
current_version = 0.10.10
current_version = 0.11.0
commit = True
tag = True

Expand Down
2 changes: 1 addition & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

__author__ = 'Alireza Sadri'
__email__ = '[email protected]'
__version__ = '0.11.0'
__version__ = '0.11.1'

with open('README.rst') as readme_file:
readme = readme_file.read()
Expand Down
24 changes: 16 additions & 8 deletions tests/test_lognflow.py
Original file line number Diff line number Diff line change
Expand Up @@ -320,7 +320,7 @@ def test_copy_file():
logger.copy('myvar/varvar', fpath, suffix = 'pdb',
time_tag= True)

var_check = logger.logged.get_single('myvar/varvar')
var_check = logger.logged.get_single('myvar/varvar*')
assert str(var) == var_check

def test_copy_list_of_files():
Expand Down Expand Up @@ -352,14 +352,14 @@ def test_copy():
logger2 = lognflow(temp_dir)
logger2.copy('some_text.txt', fpath)

def test_log_images_in_pdf():
def test_log_images_to_pdf():
logger = lognflow(temp_dir)
logger('test log images in pdf')

logger.log_imshow('im1', np.random.randn(30, 30))
logger.log_imshow('im1', np.random.randn(20, 40))

images = logger.logged.get_stack_from_names('im1')
images = logger.logged.get_stack_from_names('im1*.*')
logger.log_images_in_pdf(
'im1_all', parameter_value = images, time_tag = False)

Expand All @@ -369,23 +369,30 @@ def test_variables_to_pdf():

logger.log_imshow('im1', np.random.randn(30, 30))
logger.log_imshow('im1', np.random.randn(20, 40))
logger.variables_to_pdf('im1_all', 'im1', time_tag = False)
logger.variables_to_pdf('im1_all', 'im1*.*', time_tag = False)

def test_log_code():
logger = lognflow(temp_dir)
logger('test log variables in pdf')

logger.log_code(__file__)

if __name__ == '__main__':
#-----IF RUN BY PYTHON------#
temp_dir = select_directory()
#---------------------------#
test_log_counter(); exit()
test_variables_to_pdf(); exit()
test_log_images_to_pdf()
test_copy_file()
test_log_code()
test_log_counter()
test_log_imshow()
test_copy_list_of_files()
test_log_images_in_pdf()
test_variables_to_pdf()
test_log_imshow_series()
test_log_imshow_by_subplots()
test_log_imshow_complex()
test_replace_time_with_index()
test_log_hist()
test_copy_file()
test_log_var()
test_log_text()
test_log_single()
Expand All @@ -404,3 +411,4 @@ def test_variables_to_pdf():
test_log_confusion_matrix()
test_names_with_slashes_and_backslashes()
test_copy()
test_log_code()

0 comments on commit 2a27394

Please sign in to comment.