Commit d04882b3 authored by Ralph Adam Yap Bulanadi's avatar Ralph Adam Yap Bulanadi
Browse files

Added print_tree, list_processes, last_process, and remove_process to core

Replaced colorm with cmap when plotting; colorm is still usable for now
parent 7ca28598
Pipeline #29820 passed with stage
in 1 minute and 16 seconds
......@@ -13,7 +13,7 @@ be called using m_, l_, or e_apply.
contour_closure : removes features in an image below a certain size
find_a_domains : finds a-domains in amplitude image
create_domain_wall_filter : creates a filter around any binarised structure
X align_rows : aligns either rows or columns in image to have same average
align_rows : aligns either rows or columns in image to have same average
threshold_ratio : threshold function, where you pass ratios between max and min
directional_skeletonize : skeletonizes figures into vertical or horizontal lines
final_a_domains : cleans a-domain images
......
......@@ -229,6 +229,126 @@ def l_apply(filename, function, all_input_criteria, repeat=None, **kwargs):
increment_proc = False
def print_tree(filename, root=''):
'''
Prints the file structure of a .hdf5 file.
Parameters
----------
filename : name of file to be examined
root : optional, allows showing only a subset of the tree (default : '')
Returns
-------
null
'''
def print_branches(name):
space = ' '
branch = '│ '
tee = '├── '
last = '└── '
shift = ''
curr_name_segments = (name.split('/'))
last_name_segments = []
for i in range((len(curr_name_segments))):
if i == 0:
if not root:
last_name_segments.append(list(f.keys())[-1])
else:
last_name_segments.append(list(f[root].keys())[-1])
else:
if not root:
last_name_segments.append(list(f[name.rsplit('/', len(curr_name_segments) - i)[0]])[-1])
else:
last_name_segments.append(list(f[root][name.rsplit('/', len(curr_name_segments) - i)[0]])[-1])
for i in range((len(curr_name_segments))):
curr_name = curr_name_segments[i]
last_name = last_name_segments[i]
if i != len(curr_name_segments) - 1:
if curr_name != last_name:
shift += branch
else:
shift += space
else:
if curr_name != last_name:
shift += tee
else:
shift += last
item_name = name.split("/")[-1]
print(shift + item_name)
with h5py.File(filename, "r") as f:
if root:
print(filename + '[' + root + ']')
f[root].visit(print_branches)
else:
print(filename)
f.visit(print_branches)
def list_processes(filename, process=''):
'''
Returns a list of all processes in a named .hdf5 file.
Parameters
----------
filename : name of file to be searched
process : optional, shortens the list of processes using regex (default : '')
Returns
-------
proclst : a list of all processes
'''
procs = np.array(pt.path_search(filename, 'process*' + process + '*')).ravel()
if len(procs) == 0:
return None
proclst = [p.split('/')[1].split('/')[0] for p in procs]
proclst = sorted(list(set(proclst)))
return proclst
def last_process(filename, process=''):
'''
Returns a the filename of the last process as a string
Parameters
----------
filename : name of file to be searched
process : optional, further qualifies the last process by a regex condition (default : '')
Returns
-------
final process filling the regex condition (if any)
'''
list_of_processes = list(list_processes(filename, process=''))
return (list_of_processes[-1])
def remove_process(filename, process):
'''
Clears a named process from a named .hdf5 file. Note that the data is not de-allocated
in computer memory; use core.deallocate_hdf5_memory to regain memory.
Parameters
----------
filename : name of file to be altered
process : string containing name of process to be removed
Returns
-------
null
'''
process_path = 'process/' + process
with h5py.File(filename, "a") as f:
if process_path in f.keys():
del f[process_path]
print(process_path + ' has been removed')
else:
print(process_path + ' not found')
# FUNCTION path_search
# Uses regex expressions to search for all paths. Useful when writing complicated custom functions
# that cannot use m_apply or l_apply
......
......@@ -17,7 +17,7 @@ def save_image(data,
labelsize=16,
full_range=False,
std_range=3,
colorm='inferno',
cmap='inferno',
colorbar=True,
scalebar=False,
physical_size=(0, 'unit'),
......@@ -27,7 +27,8 @@ def save_image(data,
image_name=None,
saving_path='',
source_path=None,
verbose=False):
verbose=False,
colorm=None):
'''
Wrapper around plt.imshow() and plt.savefig() to uniformize image saving
......@@ -47,7 +48,7 @@ def save_image(data,
(default: True)
std_range: int or float, optional
the number of standard deviation to use in case full_range is set to false. (default: 3)
colorm: str, optional
cmap: str, optional
choose the colormap, see matplotlib for the list of available colormap (default: 'inferno')
colorbar: bool, optional
allows to remove the colorbar if set to false. (default: True)
......@@ -76,6 +77,11 @@ def save_image(data,
None
'''
#Remove later
if colorm:
print('Use of \'colorm\' is depracated; use \'cmap\' instead')
cmap = colorm
# Generate size of image frame
if size is None:
figsize = (np.array(np.shape(data)) / 100)[::-1]
......@@ -107,7 +113,7 @@ def save_image(data,
v_max = mean_val + std_range * std_val
# Plot image
pos = plt.imshow(data, vmin=v_min, vmax=v_max, cmap=colorm)
pos = plt.imshow(data, vmin=v_min, vmax=v_max, cmap=cmap)
# Generate colourbar
if colorbar:
......
......@@ -380,8 +380,7 @@ def generate_transform_xy_single(img, img_orig, offset_guess=[0,0],
criteria = (term_flags, number_of_iterations, termination_eps)
warp_matrix[0, 2] = offset_guess[0]
warp_matrix[1, 2] = offset_guess[1]
(cc, tform21) = cv2.findTransformECC(img_orig, img, warp_matrix, warp_mode,
criteria, None, 1)
(cc, tform21) = cv2.findTransformECC(img_orig, img, warp_matrix, warp_mode, criteria)
return tform21
......
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment