Skip to content

Commit

Permalink
plt_imshow support complex cmap
Browse files Browse the repository at this point in the history
  • Loading branch information
ARSadri committed Jan 25, 2024
1 parent a191b5e commit d6453e2
Show file tree
Hide file tree
Showing 8 changed files with 93 additions and 25 deletions.
7 changes: 6 additions & 1 deletion HISTORY.rst
Original file line number Diff line number Diff line change
Expand Up @@ -231,4 +231,9 @@ History
* exists_ok can be given to make the log_dir of lognflow
* added get_namelist and its test
* bug fixed in plt_utils
* plt_imshow added to plt_utils
* plt_imshow added to plt_utils

0.10.10 (2024-01-25)
------------------
* rgb2hsv is added
* plt_imshow supports complex color map and is bug free
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.10.9'
__version__ = '0.10.10'

from .lognflow import lognflow
from .logviewer import logviewer
Expand Down
2 changes: 1 addition & 1 deletion lognflow/loopprocessor.py
Original file line number Diff line number Diff line change
Expand Up @@ -115,7 +115,7 @@ def __call__(self, *args, **kwargs):
self.logger('with the following index to slice the inputs:'
f' {error_ret_procID[0]}')
self.logger('to avoid seeing this message, pass the argument called '\
'legger, it is print by default.')
'logger, it is print by default.')
self.logger('-'*79)
_loopprocessor_function_test_mode(
self.targetFunction, self.aQ,
Expand Down
2 changes: 1 addition & 1 deletion lognflow/multiprocessor.py
Original file line number Diff line number Diff line change
Expand Up @@ -137,7 +137,7 @@ def _reraise_any_error(
logger('with the following index to slice the inputs:'
f' {error_ret_procID[0]}')
logger('to avoid seeing this message, pass the argument called '\
'legger, it is print by default.')
'logger, it is print by default.')
logger('-'*79)
iterables_batch = ()
for iim in iterables:
Expand Down
89 changes: 70 additions & 19 deletions lognflow/plt_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,46 @@
import matplotlib.gridspec
import numpy as np
import matplotlib.pyplot as plt
from matplotlib.colors import hsv_to_rgb

def complex2hsv(data_complex, vmin = None, vmax = None):
# Routine to visualise complex array as 2D image with colour conveying
# phase information

sy, sx = data_complex.shape

sat = np.abs(data_complex)
if vmin is None:
vmin = sat.min()
if vmax is None:
vmax = sat.max()
sat = (sat-vmin)/(vmax-vmin)

hue = (np.angle(data_complex)+np.pi)/(2*np.pi)
a, b = np.divmod(hue,1.0)

H = np.zeros((sx, sy, 3))
H[:,:,0] = b
H[:,:,1] = np.ones([sx, sy])
H[:,:,2] = sat

return hsv_to_rgb(H)

def complex2hsv_colorbar():
xx, yy = np.meshgrid(
np.linspace(-1, 1, 1000),
np.linspace(-1, 1, 1000))
conv = complex2hsv(xx + 1j*yy, vmax = 1)

xx, yy = np.meshgrid(
np.linspace(-1, 1, 1000),
np.linspace(-1, 1, 1000))
conv[(xx**2 + yy **2)>1] = 1

fig, ax = plt.subplots()
im = ax.imshow(conv)
ax.axis('off')
return fig, ax

def plt_colorbar(mappable):
""" Add colobar to the current axis
Expand All @@ -21,7 +61,7 @@ def plt_imshow(img,
colorbar = True,
remove_axis_ticks = False,
title = None,
cmap = None,
cmap = None,
**kwargs):
if(not np.iscomplexobj(img)):
fig, ax = plt.subplots()
Expand All @@ -31,24 +71,35 @@ def plt_imshow(img,
if(remove_axis_ticks):
plt.setp(ax, xticks=[], yticks=[])
else:
fig, ax = plt.subplots(1, 2)
im = ax[0].imshow(np.abs(parameter_value),
cmap = cmap, **kwargs)
if(colorbar):
plt_colorbar(im)
ax[0].set_title('Amplitude')
im = ax[1].imshow(np.angle(parameter_value),
cmap = cmap, **kwargs)
if(colorbar):
plt_colorbar(im)
ax[1].set_title('Phase')
if(remove_axis_ticks):
plt.setp(ax[0], xticks=[], yticks=[])
ax[0].xaxis.set_ticks_position('none')
ax[0].yaxis.set_ticks_position('none')
plt.setp(ax[1], xticks=[], yticks=[])
ax[1].xaxis.set_ticks_position('none')
ax[1].yaxis.set_ticks_position('none')
if cmap == 'complex':
converted = complex2hsv(img)
fig, ax = plt.subplots()
im = ax.imshow(converted, cmap = 'hsv', **kwargs)
if(colorbar):
plt_colorbar(im)

if(remove_axis_ticks):
plt.setp(ax, xticks=[], yticks=[])

else:
fig, ax = plt.subplots(1, 2)
im = ax[0].imshow(np.abs(img),
cmap = cmap, **kwargs)
if(colorbar):
plt_colorbar(im)
ax[0].set_title('Amplitude')
im = ax[1].imshow(np.angle(img),
cmap = cmap, **kwargs)
if(colorbar):
plt_colorbar(im)
ax[1].set_title('Phase')
if(remove_axis_ticks):
plt.setp(ax[0], xticks=[], yticks=[])
ax[0].xaxis.set_ticks_position('none')
ax[0].yaxis.set_ticks_position('none')
plt.setp(ax[1], xticks=[], yticks=[])
ax[1].xaxis.set_ticks_position('none')
ax[1].yaxis.set_ticks_position('none')
if title is not None:
ax.set_title(title)
return fig, ax
Expand Down
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.8
current_version = 0.10.9
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.10.9'
__version__ = '0.10.10'

with open('README.rst') as readme_file:
readme = readme_file.read()
Expand Down
12 changes: 12 additions & 0 deletions tests/test_plt_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@

import matplotlib.pyplot as plt
import lognflow
from lognflow.plt_utils import plt_imshow, complex2hsv_colorbar
import numpy as np

def test_numbers_as_images():
Expand Down Expand Up @@ -58,7 +59,18 @@ def test_imshow_by_subplots():
lognflow.plt_utils.imshow_by_subplots(data)
plt.show()

def test_plt_imshow():
data = np.random.rand(100, 100) + 1j * np.random.rand(100, 100)
plt_imshow(data, cmap = 'complex')
plt.show()

def test_complex2hsv_colorbar():
complex2hsv_colorbar()
plt.show()

if __name__ == '__main__':
test_complex2hsv_colorbar()
test_plt_imshow()
test_imshow_series()
test_imshow_by_subplots()
test_plot_gaussian_gradient()
Expand Down

0 comments on commit d6453e2

Please sign in to comment.