Skip to content

Commit

Permalink
Merge branch 'release-0.3.0'
Browse files Browse the repository at this point in the history
  • Loading branch information
blleng committed Sep 10, 2024
2 parents 36fae6a + 8da3e15 commit 7c0ce7d
Show file tree
Hide file tree
Showing 6 changed files with 106 additions and 35 deletions.
17 changes: 5 additions & 12 deletions .github/workflows/publish-to-testpypi.yml
Original file line number Diff line number Diff line change
Expand Up @@ -18,20 +18,13 @@ jobs:
python-version: 3.12

- name: Install pypa/build
run: >-
python -m
pip install
build
--user
run:
python -m pip install build toml --user

- name: Build a binary wheel and a source tarball
run: >-
python -m
build
--sdist
--wheel
--outdir dist/
.
run: |
python dev.py
python -m build --sdist --wheel --outdir dist/
- name: Publish distribution 📦 to Test PyPI
uses: pypa/gh-action-pypi-publish@master
Expand Down
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ NOTICE: Three arguments are required to specify the position and width of scale
Default colors of elemental mapppings are corresponding to the following list in sequnce (*Matplotlib* default colors):
<font color=#1f77b4>1f77b4</font>, <font color=#ff7f0e>ff7f0e</font>, <font color=#2ca02c>2ca02c</font>, <font color=#d62728>d62728</font>, <font color=#9467bd>9467bd</font>, <font color=#8c564b>8c564b</font>, <font color=#e377c2>e377c2</font>, <font color=7f7f7f>7f7f7f</font>', <font color=#bcbd22>bcbd22</font>, <font color=#17becf>17becf</font>
<font color=#1f77b4>1f77b4</font>, <font color=#ff7f0e>ff7f0e</font>, <font color=#2ca02c>2ca02c</font>, <font color=#d62728>d62728</font>, <font color=#9467bd>9467bd</font>, <font color=#8c564b>8c564b</font>, <font color=#e377c2>e377c2</font>, <font color=7f7f7f>7f7f7f</font>, <font color=#bcbd22>bcbd22</font>, <font color=#17becf>17becf</font>
Convert-EMD provides `-e`/`--eds` option for users to customize the color of elemental mappings.
Expand Down
13 changes: 13 additions & 0 deletions dev.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
developing = False

if developing:
import datetime
import toml
time_now = datetime.datetime.now().strftime("%y%m%d%H%M%S")
f = open("pyproject.toml", "r")
data = toml.load(f)
data["project"]["version"] = data["project"]["version"] + ".dev" + time_now
f.close()
new_f = open("pyproject.toml", "w")
new_f.write(toml.dumps(data))
new_f.close()
23 changes: 20 additions & 3 deletions pyproject.toml
Original file line number Diff line number Diff line change
@@ -1,11 +1,25 @@
[build-system]
requires = ["setuptools>=61.0"]
requires = ["setuptools>=61"]
build-backend = "setuptools.build_meta"

[project]
name = "convert-emd"
version = "0.2.5"
dependencies = ["rosettasciio[all]>=0.6"]
version = "0.3.0"
keywords = [
"electron microscopy",
"python",
"eds",
"emd",
"img"
]
dependencies = [
"matplotlib>=3.7",
"h5py>=3.7",
"numba>=0.52",
"sparse>=0.15",
"scikit-image>=0.21",
"rosettasciio>=0.6"
]
authors = [
{ name="Bing-Liang Leng", email="leng.bl@sjtu.edu.cn" },
]
Expand All @@ -16,6 +30,9 @@ classifiers = [
"Programming Language :: Python :: 3",
"License :: OSI Approved :: MIT License",
"Operating System :: OS Independent",
"Topic :: Scientific/Engineering",
"Topic :: Scientific/Engineering :: Physics",
"Topic :: Scientific/Engineering :: Chemistry"
]

[project.urls]
Expand Down
47 changes: 37 additions & 10 deletions src/convert_emd/drawing.py
Original file line number Diff line number Diff line change
@@ -1,15 +1,17 @@
import os
import re
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)
if " / " in unit: unit = re.sub(r" / ", "_", unit)
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]
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 + "/"
Expand All @@ -29,12 +31,12 @@ def convert_emd(file_name, data, output_type, scale_bar, sb_color, sb_x_start, s
if dim ==1:
save_file = open(output_name + title + "_" + str(i) + ".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).transpose()
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, stretch)
frame["data"] = emdfun.contrast_stretch(frame["data"], stretch)
cmp = "gray"
if overlay:
if title in mapping_overlay: mapping_frame.append(i)
Expand All @@ -49,7 +51,7 @@ 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 = (frame["axes"][1]["size"], frame["axes"][0]["size"])
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)
Expand All @@ -68,12 +70,37 @@ def convert_emd(file_name, data, output_type, scale_bar, sb_color, sb_x_start, s
plt.savefig(output_name + title + "_" + str(i) + output_type)
plt.close()

if dim == 3 and emdfun.is_eds_spectrum(frame):
save_file = open(output_name + title + "_" + str(i) + ".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).transpose()
emdfun.write_signal1d(save_file, signal_data)
save_file.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.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)
save_file.close()
else:
cmp = "gray"
size_x, size_y = emdfun.get_size(frame)
split_frame = emdfun.series_images(frame)
split_unit = frame["axes"][0]["units"]
for i in range(frame["axes"][0]["size"]):
frame["data"][i] = emdfun.contrast_stretch(frame["data"][i], stretch)
plt.figure(figsize=(size_x/100, size_y/100), facecolor="black")
ax = plt.gca()
plt.imshow(frame["data"][i], 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(split_frame[i])[:5] + split_unit + "_" + sb_text + output_type)
else:
plt.savefig(output_name + title + "_" + str(split_frame[i])[:5] + split_unit + output_type)
plt.close()

if overlay:
element = ""
Expand Down
39 changes: 30 additions & 9 deletions src/convert_emd/function.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,25 @@
import matplotlib.colors as mcolors
import numpy as np
from skimage import exposure
from rsciio.emd import file_reader
from rsciio.emd._emd_velox import FeiEMDReader
import h5py as h5

def get_data(file_name):
return file_reader(file_name + ".emd")
file = h5.File(file_name + ".emd","r")
emd_reader = FeiEMDReader(
lazy = False,
select_type = None,
first_frame = 0,
last_frame = None,
sum_frames = True,
sum_EDS_detectors = True,
rebin_energy = 1,
SI_dtype = None,
load_SI_image_stack = False,
)
emd_reader.read_file(file)
data = emd_reader.dictionaries
return data

def data_signal_type(frame):
return frame["metadata"]["Signal"]["signal_type"]
Expand All @@ -26,29 +41,35 @@ def eds_elements(data):
return element

def get_scale(frame):
return (frame["axes"][1]["scale"], frame["axes"][1]["units"])
return (frame["axes"][-1]["scale"], frame["axes"][-1]["units"])

def get_title(frame):
return frame["metadata"]["General"]["title"]

def get_size(frame):
return (frame["axes"][1]["size"], frame["axes"][0]["size"])
return (frame["axes"][-1]["size"], frame["axes"][-2]["size"])

def signal1d_data(frame):
offset = frame["axes"][0]["offset"]
scale = frame["axes"][0]["scale"]
size = frame["axes"][0]["size"]
x_data = np.arange(offset, scale*size+offset, scale)
y_data = frame["data"]
return np.asarray([x_data, y_data])
return np.asarray([x_data, y_data]).transpose()

def signal3d_to_1d_data(frame):
offset = frame["axes"][2]["offset"]
scale = frame["axes"][2]["scale"]
size = frame["axes"][2]["size"]
x_data = np.arange(offset, scale*size+offset, scale)
y_data = frame["data"].sum(axis=(0, 1))
return np.asarray([x_data, y_data])
return np.asarray([x_data, y_data]).transpose()

def series_images(frame):
offset = frame["axes"][0]["offset"]
scale = frame["axes"][0]["scale"]
size = frame["axes"][0]["size"]
return np.arange(offset, scale*size+offset, scale)

def write_signal1d(file, data):
return np.savetxt(file, data, delimiter="\t")
Expand All @@ -61,6 +82,6 @@ def create_cmp(color):
def default_colors():
return list(mcolors.TABLEAU_COLORS.values())

def contrast_stretch(frame, stretch):
low_constrain, high_constrain = np.percentile(frame["data"], (stretch[0], stretch[1]))
return exposure.rescale_intensity(frame["data"], in_range=(low_constrain, high_constrain))
def contrast_stretch(data, stretch):
low_constrain, high_constrain = np.percentile(data, (stretch[0], stretch[1]))
return exposure.rescale_intensity(data, in_range=(low_constrain, high_constrain))

0 comments on commit 7c0ce7d

Please sign in to comment.