Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

dump #56

Merged
merged 3 commits into from
Mar 29, 2024
Merged

dump #56

Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 23 additions & 0 deletions news/dump.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
**Added:**

* dump method to diffraction_object

**Changed:**

* <news item>

**Deprecated:**

* <news item>

**Removed:**

* <news item>

**Fixed:**

* <news item>

**Security:**

* <news item>
4 changes: 2 additions & 2 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -40,8 +40,8 @@ Issues = "https://github.com/diffpy/diffpy.utils/issues"
[tool.setuptools-git-versioning]
enabled = true
template = "{tag}"
dev_template = "{tag}.post{ccount}"
dirty_template = "{tag}.post{ccount}"
dev_template = "{tag}"
dirty_template = "{tag}"

[tool.setuptools.packages.find]
where = ["src"] # list of folders that contain the packages (["."] by default)
Expand Down
20 changes: 19 additions & 1 deletion src/diffpy/utils/scattering_objects/diffraction_objects.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
def __init__(self, name='', wavelength=None):
self.name = name
self.wavelength = wavelength
self.scat_quantities = {}
self.scat_quantity = ""
self.on_q = [np.empty(1), np.empty(1)]
self.on_tth = [np.empty(1), np.empty(1)]
self.on_d = [np.empty(1), np.empty(1)]
Expand Down Expand Up @@ -401,3 +401,21 @@
elif xtype.lower() in DQUANTITIES:
return self.on_d
pass

def dump(self, filepath, xtype=None):
if xtype is None:
xtype = " q"

Check warning on line 407 in src/diffpy/utils/scattering_objects/diffraction_objects.py

View check run for this annotation

Codecov / codecov/patch

src/diffpy/utils/scattering_objects/diffraction_objects.py#L407

Added line #L407 was not covered by tests
if xtype == "q":
data_to_save = np.column_stack((self.on_q[0], self.on_q[1]))
elif xtype == "tth":
data_to_save = np.column_stack((self.on_tth[0], self.on_tth[1]))

Check warning on line 411 in src/diffpy/utils/scattering_objects/diffraction_objects.py

View check run for this annotation

Codecov / codecov/patch

src/diffpy/utils/scattering_objects/diffraction_objects.py#L410-L411

Added lines #L410 - L411 were not covered by tests
else:
print(f"WARNING: cannot handle the xtype '{xtype}'")

Check warning on line 413 in src/diffpy/utils/scattering_objects/diffraction_objects.py

View check run for this annotation

Codecov / codecov/patch

src/diffpy/utils/scattering_objects/diffraction_objects.py#L413

Added line #L413 was not covered by tests

with open(filepath, 'w') as f:
f.write(f"[Diffraction_object]\nname = {self.name}\nwavelength = {self.wavelength}\n"
f"scat_quantity = {self.scat_quantity}\n")
for key, value in self.metadata.items():
f.write(f"{key} = {value}\n")
f.write("\n#### start data\n")
np.savetxt(f, data_to_save, delimiter=" ")
25 changes: 25 additions & 0 deletions src/diffpy/utils/tests/test_diffraction_objects.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
import numpy as np
from pathlib import Path
from diffpy.utils.scattering_objects.diffraction_objects import Diffraction_object

def test_dump(tmp_path):
x, y = np.linspace(0, 10, 11), np.linspace(0, 10, 11)
directory = Path(tmp_path)
file = directory / "testfile"
test = Diffraction_object()
test.wavelength = 1.54
test.name = "test"
test.scat_quantity = "x-ray"
test.insert_scattering_quantity(x, y, "q", metadata={"thing1": 1, "thing2": "thing2"})
test.dump(file, "q")
with open(file, "r") as f:
actual = f.read()
expected = ("[Diffraction_object]\nname = test\nwavelength = 1.54\nscat_quantity = x-ray\nthing1 = 1\n"
"thing2 = thing2\n\n#### start data\n0.000000000000000000e+00 0.000000000000000000e+00\n"
"1.000000000000000000e+00 1.000000000000000000e+00\n2.000000000000000000e+00 2.000000000000000000e+00\n"
"3.000000000000000000e+00 3.000000000000000000e+00\n4.000000000000000000e+00 4.000000000000000000e+00\n"
"5.000000000000000000e+00 5.000000000000000000e+00\n"
"6.000000000000000000e+00 6.000000000000000000e+00\n7.000000000000000000e+00 7.000000000000000000e+00\n"
"8.000000000000000000e+00 8.000000000000000000e+00\n9.000000000000000000e+00 9.000000000000000000e+00\n"
"1.000000000000000000e+01 1.000000000000000000e+01\n")
assert actual == expected
Loading