-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
New features: 1. Set default colors for elemental mappings 2. Plot all elements in the overlayed mapping by default
- Loading branch information
Showing
6 changed files
with
251 additions
and
122 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,89 @@ | ||
import argparse | ||
|
||
def parse(): | ||
parser = argparse.ArgumentParser(description = "Convert .emd file generated by Velox to images") | ||
|
||
parser.add_argument ( | ||
"-f", | ||
"--file", | ||
type = str, | ||
metavar = "FILE", | ||
help = "Input *.emd(Velox) filename (without \".emd\" extension)", | ||
required=True | ||
) | ||
parser.add_argument ( | ||
"-o", | ||
"--out", | ||
type = str, | ||
metavar = "TYPE", | ||
help = "Type of output images.", | ||
default = "png" | ||
) | ||
parser.add_argument ( | ||
"-ns", | ||
"--no_scale", | ||
help = "Do not draw scale bar", | ||
action = "store_true" | ||
) | ||
parser.add_argument ( | ||
"-sc", | ||
"--scale_color", | ||
type = str, | ||
metavar = "COLOR", | ||
help = "Color of scale bar", | ||
default = "#ffffff" | ||
) | ||
parser.add_argument ( | ||
"-s", | ||
"--scale", | ||
type = float, | ||
nargs = 3, | ||
metavar = "FLOAT", | ||
help = "The position and width of scale bar (X_POSITION Y_POSITION WIDTH_FACTOR), the width is set as image-height/WIDTH_FACTOR", | ||
default = [0.75, 0.9167, 150.0] | ||
) | ||
parser.add_argument ( | ||
"-e", | ||
"--eds", | ||
type = str, | ||
nargs = "+", | ||
metavar = "Str", | ||
help = "The color of elemental mappings (default: gray)", | ||
default = [] | ||
) | ||
parser.add_argument ( | ||
"-oe", | ||
"--overlay", | ||
type = str, | ||
nargs = "+", | ||
metavar = "ELEMENT", | ||
help = "The elements for overlayed mapping", | ||
default = [] | ||
) | ||
parser.add_argument ( | ||
"-oa", | ||
"--overlay_alpha", | ||
type = float, | ||
metavar = "ALPHA", | ||
help = "Transparency of the overlayed elemental mapping (a value between 0 and 1, 0 means totally transparent)", | ||
default = "1.0" | ||
) | ||
parser.add_argument ( | ||
"-sa", | ||
"--substrate_alpha", | ||
type = float, | ||
metavar = "ALPHA", | ||
help = "Transparency of the HAADF substrate picture in elemental mapping (a value between 0 and 1, 0 means totally transparent)", | ||
default = "0.5" | ||
) | ||
parser.add_argument ( | ||
"-c", | ||
"--contrast_stretching", | ||
type = float, | ||
nargs = 2, | ||
metavar = "CONTRAST", | ||
help = "The parameter for contrast streaching, where the image is rescaled to include all intensities that fall within the given percentiles", | ||
default = [1, 99] | ||
) | ||
|
||
return parser |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,91 @@ | ||
import os | ||
import matplotlib.pyplot as plt | ||
import convert_emd.function as emdfun | ||
|
||
def draw_scale_bar(frame, size_x, size_y, sb_x_start, sb_y_start, width_factor, sb_color): | ||
sb_lst = [0.1,0.2,0.5,1,2,5,10,20,50,100,200,500,1000,2000,5000] | ||
scale, unit = emdfun.get_scale(frame) | ||
sb_len_float = size_x * scale / 6 | ||
sb_len = sorted(sb_lst, key=lambda a: abs(a - sb_len_float))[0] | ||
sb_len_px = sb_len / scale | ||
sb_start_x, sb_start_y, sb_width = (size_x * sb_x_start , size_y * sb_y_start, size_y / width_factor) | ||
return [plt.Rectangle((sb_start_x, sb_start_y), sb_len_px, sb_width, color=sb_color, fill=True), "_" + str(sb_len) + unit] | ||
|
||
def convert_emd(file_name, data, output_type, scale_bar, sb_color, sb_x_start, sb_y_start, sb_width_factor, stretch, overlay_alpha, sub_alpha, eds_color, mapping_overlay, overlay): | ||
output_dir = file_name + "/" | ||
output_name = output_dir + file_name + "_" | ||
|
||
if not os.path.exists(output_dir): | ||
os.makedirs(output_dir) | ||
mapping_frame = [] | ||
ele = 0 | ||
default_colors = emdfun.default_colors() | ||
|
||
for i in range(len(data)): | ||
frame = data[i] | ||
dim = frame["data"].ndim | ||
title = emdfun.get_title(frame) | ||
|
||
if dim == 2: | ||
frame["data"] = emdfun.contrast_stretch(frame, stretch) | ||
cmp = "gray" | ||
if overlay and not emdfun.is_eds_spectrum(frame): | ||
if title in mapping_overlay: mapping_frame.append(i) | ||
if title in eds_color: | ||
cmp = emdfun.create_cmp(eds_color[title]) | ||
elif title == "HAADF": | ||
mapping_frame.append(i) | ||
HAADF_frame_num = len(mapping_frame) - 1 | ||
else: | ||
cmp = emdfun.create_cmp(default_colors[ele]) | ||
eds_color[title] = default_colors[ele] | ||
ele += 1 | ||
if ele > 9: ele = 0 | ||
|
||
size_x, size_y = (frame["axes"][1]["size"], frame["axes"][0]["size"]) | ||
plt.figure(figsize=(size_x/100, size_y/100), facecolor="black") | ||
ax = plt.gca() | ||
plt.imshow(frame["data"], cmap=cmp) | ||
|
||
if scale_bar == True: | ||
bar = draw_scale_bar(frame, size_x, size_y, sb_x_start, sb_y_start, sb_width_factor, sb_color) | ||
ax.add_patch(bar[0]) | ||
sb_text = bar[1] | ||
|
||
plt.subplots_adjust(top=1, bottom=0, right=1, left=0, hspace=0, wspace=0) | ||
plt.margins = (0, 0) | ||
plt.axis("off") | ||
if scale_bar == True: | ||
plt.savefig(output_name + title + "_" + str(i) + sb_text + output_type) | ||
else: | ||
plt.savefig(output_name + title + "_" + str(i) + output_type) | ||
plt.close() | ||
|
||
if overlay: | ||
element = "" | ||
HAADF_frame = data[mapping_frame[HAADF_frame_num]] | ||
size_x, size_y = (HAADF_frame["axes"][1]["size"], HAADF_frame["axes"][0]["size"]) | ||
|
||
plt.figure(figsize=(size_x/100, size_y/100), facecolor="black") | ||
ax = plt.gca() | ||
plt.imshow(HAADF_frame["data"], cmap="gray", alpha=sub_alpha) | ||
for i in range(len(mapping_frame)): | ||
if i == HAADF_frame_num: | ||
continue | ||
title = emdfun.get_title(data[mapping_frame[i]]) | ||
plt.imshow(data[mapping_frame[i]]["data"], cmap = emdfun.create_cmp(eds_color[title]), alpha = overlay_alpha) | ||
element = element + "_" + title | ||
|
||
if scale_bar == True: | ||
bar = draw_scale_bar(HAADF_frame, size_x, size_y, sb_x_start, sb_y_start, sb_width_factor, sb_color) | ||
ax.add_patch(bar[0]) | ||
sb_text = bar[1] | ||
|
||
plt.subplots_adjust(top=1, bottom=0, right=1, left=0, hspace=0, wspace=0) | ||
plt.margins = (0, 0) | ||
plt.axis("off") | ||
if scale_bar == True: | ||
plt.savefig(output_name + "Overlay" + element + sb_text + output_type) | ||
else: | ||
plt.savefig(output_name + "Overlay" + element + output_type) | ||
plt.close() |
Oops, something went wrong.