Skip to content

Commit

Permalink
Merge branch 'release-0.3.1'
Browse files Browse the repository at this point in the history
  • Loading branch information
blleng committed Sep 11, 2024
2 parents 7c0ce7d + 7debb0c commit 10a781b
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 29 deletions.
2 changes: 1 addition & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ build-backend = "setuptools.build_meta"

[project]
name = "convert-emd"
version = "0.3.0"
version = "0.3.1"
keywords = [
"electron microscopy",
"python",
Expand Down
66 changes: 38 additions & 28 deletions src/convert_emd/drawing.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,6 @@ def draw_scale_bar(frame, size_x, size_y, sb_x_start, sb_y_start, width_factor,

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 = []
Expand All @@ -27,16 +25,16 @@ def convert_emd(file_name, data, output_type, scale_bar, sb_color, sb_x_start, s
frame = data[i]
dim = frame["data"].ndim
title = emdfun.get_title(frame)
title_attr = "_" + str(i) + "_"

if dim ==1:
save_file = open(output_name + title + "_" + str(i) + ".txt", "w", encoding = "utf-8")
save_file = open(output_dir + title + title_attr + ".txt", "w", encoding = "utf-8")
save_file.write(frame["axes"][0]["name"] + "(" + frame["axes"][0]["units"] + ")" + "\t" +"Intensity(a.u.)" + "\n")
signal_data = emdfun.signal1d_data(frame)
emdfun.write_signal1d(save_file, signal_data)
save_file.close()

if dim == 2:
frame["data"] = emdfun.contrast_stretch(frame["data"], stretch)
cmp = "gray"
if overlay:
if title in mapping_overlay: mapping_frame.append(i)
Expand All @@ -51,28 +49,38 @@ def convert_emd(file_name, data, output_type, scale_bar, sb_color, sb_x_start, s
ele += 1
if ele > 9: ele = 0

size_x, size_y = emdfun.get_size(frame)
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()

is_complex = True if frame["data"].dtype == "complex64" else False
if is_complex:
idata = [frame["data"].real, frame["data"].imag]
title_attr = title_attr + "/"
if not os.path.exists(output_dir + title + title_attr):
os.makedirs(output_dir + title + title_attr)
else: idata = [frame["data"]]

for j in range(len(idata)):
if not is_complex: idata[j] = emdfun.contrast_stretch(idata[j], stretch)
size_x, size_y = emdfun.get_size(frame)
plt.figure(figsize=(size_x/100, size_y/100), facecolor="black")
ax = plt.gca()
plt.imshow(idata[j], 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_dir + title + title_attr + str(j) + sb_text + output_type)
else:
plt.savefig(output_dir + title + title_attr + str(j) + output_type)
plt.close()

if dim == 3:
if emdfun.is_eds_spectrum(frame):
save_file = open(output_name + title + "_" + str(i) + ".txt", "w", encoding = "utf-8")
save_file = open(output_dir + title + title_attr + ".txt", "w", encoding = "utf-8")
save_file.write(frame["axes"][2]["name"] + "(" + frame["axes"][2]["units"] + ")" + "\t" +"Intensity(a.u.)" + "\n")
signal_data = emdfun.signal3d_to_1d_data(frame)
emdfun.write_signal1d(save_file, signal_data)
Expand All @@ -97,9 +105,9 @@ def convert_emd(file_name, data, output_type, scale_bar, sb_color, sb_x_start, s
plt.margins = (0, 0)
plt.axis("off")
if scale_bar == True:
plt.savefig(output_name + title + "_" + str(split_frame[i])[:5] + split_unit + "_" + sb_text + output_type)
plt.savefig(output_dir + title + title_attr + str(split_frame[i])[:6] + split_unit + sb_text + output_type)
else:
plt.savefig(output_name + title + "_" + str(split_frame[i])[:5] + split_unit + output_type)
plt.savefig(output_dir + title + title_attr + str(split_frame[i])[:6] + split_unit + output_type)
plt.close()

if overlay:
Expand All @@ -109,11 +117,13 @@ def convert_emd(file_name, data, output_type, scale_bar, sb_color, sb_x_start, s

plt.figure(figsize=(size_x/100, size_y/100), facecolor="black")
ax = plt.gca()
HAADF_frame["data"] = emdfun.contrast_stretch(HAADF_frame["data"], stretch)
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]])
data[mapping_frame[i]]["data"] = emdfun.contrast_stretch(data[mapping_frame[i]]["data"], stretch)
plt.imshow(data[mapping_frame[i]]["data"], cmap = emdfun.create_cmp(eds_color[title]), alpha = overlay_alpha)
element = element + "_" + title

Expand All @@ -126,7 +136,7 @@ def convert_emd(file_name, data, output_type, scale_bar, sb_color, sb_x_start, s
plt.margins = (0, 0)
plt.axis("off")
if scale_bar == True:
plt.savefig(output_name + "Overlay" + element + sb_text + output_type)
plt.savefig(output_dir + "Overlay" + element + sb_text + output_type)
else:
plt.savefig(output_name + "Overlay" + element + output_type)
plt.savefig(output_dir + "Overlay" + element + output_type)
plt.close()

0 comments on commit 10a781b

Please sign in to comment.