-
Notifications
You must be signed in to change notification settings - Fork 16
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #32 from alchem0x2A/pytest
Add support for purest
- Loading branch information
Showing
15 changed files
with
231 additions
and
14 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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -109,3 +109,7 @@ ENV/ | |
/examples/spectra_files/*.svg | ||
/*.csv | ||
/*.svg | ||
/bin/renishaw-export.py | ||
*.txt | ||
*.csv | ||
/examples/*.svg |
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,94 @@ | ||
#! /usr/bin/env python3 | ||
|
||
########################################################### | ||
# The example shows how to get mapping data # | ||
# The peak ratio at 1315 cm^-1 and 1380 cm^-1 are plotted # | ||
# Details see Small 14, 1804006 (2018). # | ||
########################################################### | ||
import subprocess | ||
import os | ||
from pathlib import Path | ||
|
||
import numpy as np | ||
from renishawWiRE import WDFReader | ||
from _path import curdir, imgdir | ||
try: | ||
import matplotlib.pyplot as plt | ||
import matplotlib.image as mpimg | ||
plot = True | ||
except ImportError: | ||
plot = False | ||
|
||
|
||
def call_exe(name, extras="", output=None): | ||
filename = curdir / "spectra_files" / "{0}.wdf".format(name) | ||
root = filename.parent | ||
# clean up all | ||
reader = WDFReader(filename) | ||
assert reader is not None | ||
for ext in (".csv", ".txt"): | ||
for f in root.glob("*" + ext): | ||
print(f) | ||
os.remove(f) | ||
# Initial name | ||
cmd = "wdf-export {0} {1}".format(filename.as_posix(), | ||
extras) | ||
if output is not None: | ||
cmd += "-o {0}".format(output) | ||
|
||
run = subprocess.run(cmd, shell=True) | ||
assert run.returncode == 0 | ||
|
||
# Manually set output | ||
if output is not None: | ||
output = Path(output) | ||
else: | ||
if ".txt" not in extras: | ||
output = filename.with_suffix(".csv") | ||
else: | ||
output = filename.with_suffix(".txt") | ||
|
||
assert output.is_file() is True | ||
# Read the data | ||
if output.suffix == ".csv": | ||
delimiter = "," | ||
else: | ||
delimiter = " " | ||
|
||
data = np.genfromtxt(output, delimiter=delimiter, skip_header=1) | ||
wn_data = data[:, 0] | ||
spectra_data = data[:, 1:] | ||
assert reader.xdata.shape[0] == wn_data.shape[0] | ||
assert reader.count == spectra_data.shape[1] | ||
spectra_reader = reader.spectra.reshape(reader.count, | ||
reader.xdata.shape[0]).T | ||
# Only do this for decreasing sequence | ||
if reader.xdata[0] > reader.xdata[1]: | ||
spectra_reader = spectra_reader[::-1, :] | ||
print(spectra_reader) | ||
print(spectra_data) | ||
assert np.all(np.isclose(spectra_reader, spectra_data, 1e-3)) | ||
|
||
|
||
|
||
|
||
|
||
def main(): | ||
|
||
for name in ("sp", "line", "depth", | ||
"mapping", "undefined", "streamline"): | ||
# Normal exe | ||
call_exe(name) | ||
call_exe(name, extras="-f .txt") | ||
call_exe(name, | ||
output="test.txt") | ||
call_exe(name, | ||
output="test.csv") | ||
|
||
|
||
|
||
|
||
|
||
|
||
if __name__ == "__main__": | ||
main() |
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,91 @@ | ||
#! /usr/bin/env python3 | ||
|
||
########################################################### | ||
# The example shows how to get mapping data # | ||
# The peak ratio at 1315 cm^-1 and 1380 cm^-1 are plotted # | ||
# Details see Small 14, 1804006 (2018). # | ||
########################################################### | ||
|
||
import numpy as np | ||
from renishawWiRE import WDFReader | ||
from _path import curdir, imgdir | ||
try: | ||
import matplotlib.pyplot as plt | ||
import matplotlib.image as mpimg | ||
plot = True | ||
except ImportError: | ||
plot = False | ||
|
||
|
||
def peak_in_range(spectra, wn, range, method="max", **params): | ||
"""Find the max intensity of peak within range | ||
method can be max, min, or mean | ||
""" | ||
cond = np.where((wn >= range[0]) & (wn <= range[1]))[0] | ||
spectra_cut = spectra[:, :, cond] | ||
return getattr(np, method)(spectra_cut, axis=2, **params) | ||
|
||
|
||
def main(): | ||
filename = curdir / "spectra_files" / "streamline.wdf" | ||
reader = WDFReader(filename) | ||
print("Measurement: ", reader.measurement_type) | ||
print("Scan: ", reader.scan_type) | ||
assert reader.measurement_type == 3 | ||
assert reader.scan_type == 6 | ||
wn = reader.xdata | ||
spectra = reader.spectra | ||
print(wn.shape, spectra.shape) | ||
x = reader.xpos | ||
y = reader.ypos | ||
print(len(x), len(y)) | ||
w, h = reader.map_shape | ||
print("The size of mapping is {0:d} * {1:d}". | ||
format(w, h)) | ||
# w and h are the measure in xy coordinates | ||
# Level the spectra | ||
spectra = spectra - np.min(spectra, axis=2, keepdims=True) | ||
peaks_a = peak_in_range(spectra, wn, [1295, 1340]) | ||
peaks_b = peak_in_range(spectra, wn, [1350, 1400]) | ||
|
||
ratio = peaks_a / peaks_b | ||
ratio_fl = ratio.flatten() | ||
|
||
if plot is True: | ||
plt.figure(figsize=(10, 5)) | ||
|
||
# Left plot histogram of Peak A/B ratio | ||
plt.subplot(121) | ||
img = mpimg.imread(reader.img, format="jpg") | ||
img_x0, img_y0 = reader.img_origins | ||
img_w, img_h = reader.img_dimensions | ||
plt.imshow(img, extent=(img_x0, img_x0 + img_w, | ||
img_y0 + img_h, img_y0)) | ||
plt.scatter(x, y, s=0.4, alpha=0.8) | ||
# plt.hist(ratio_fl, bins=50, range=(0.1, 2)) | ||
# plt.xlabel("Ratio peak A / peak B") | ||
# plt.ylabel("Counts") | ||
|
||
# Right plot histogram of Peak A/B mapping | ||
plt.subplot(122) | ||
|
||
plt.imshow(peaks_b, interpolation="bicubic", | ||
extent=[0, x.max() - x.min(), | ||
y.max() - y.min(), 0],) | ||
# vmin=0.5, vmax=1.5) | ||
plt.xlabel("Mapping x [μm]") | ||
plt.ylabel("Mapping y [μm]") | ||
cb = plt.colorbar() | ||
cb.ax.set_title("Signal") | ||
plt.tight_layout() | ||
plt.show(block=False) | ||
plt.pause(3) | ||
plt.savefig(imgdir / "mapping_streamline.png", dpi=100) | ||
plt.close() | ||
else: | ||
pass | ||
return | ||
|
||
|
||
if __name__ == "__main__": | ||
main() |
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,3 @@ | ||
[pytest] | ||
python_files = ex*.py | ||
python_functions = main |
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 |
---|---|---|
@@ -1 +1,2 @@ | ||
from .wdfReader import WDFReader | ||
from .export import main |
File renamed without changes.
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