From e3a14a3723b86f310f33caa335d43ccc2134e63d Mon Sep 17 00:00:00 2001 From: Alison Wu Date: Thu, 7 Nov 2024 23:23:01 -0500 Subject: [PATCH 1/8] remove relative path imports --- src/diffpy/utils/parsers/__init__.py | 8 -------- 1 file changed, 8 deletions(-) diff --git a/src/diffpy/utils/parsers/__init__.py b/src/diffpy/utils/parsers/__init__.py index a2104181..cd95dd63 100644 --- a/src/diffpy/utils/parsers/__init__.py +++ b/src/diffpy/utils/parsers/__init__.py @@ -16,12 +16,4 @@ """Various utilities related to data parsing and manipulation. """ -from .loaddata import loadData -from .resample import resample -from .serialization import deserialize_data, serialize_data - -# silence the pyflakes syntax checker -assert loadData or resample or True -assert serialize_data or deserialize_data or True - # End of file From 3c9d5177080f48de687178d3904a88b82864f272 Mon Sep 17 00:00:00 2001 From: Alison Wu Date: Thu, 7 Nov 2024 23:25:01 -0500 Subject: [PATCH 2/8] renamed functions and files to PEP8 --- .../parsers/{loaddata.py => data_loader.py} | 6 ++--- .../{serialization.py => serializer.py} | 0 .../scattering_objects/diffraction_objects.py | 24 +++++++++---------- .../utils/wx/{gridutils.py => grid_utils.py} | 0 4 files changed, 15 insertions(+), 15 deletions(-) rename src/diffpy/utils/parsers/{loaddata.py => data_loader.py} (98%) rename src/diffpy/utils/parsers/{serialization.py => serializer.py} (100%) rename src/diffpy/utils/wx/{gridutils.py => grid_utils.py} (100%) diff --git a/src/diffpy/utils/parsers/loaddata.py b/src/diffpy/utils/parsers/data_loader.py similarity index 98% rename from src/diffpy/utils/parsers/loaddata.py rename to src/diffpy/utils/parsers/data_loader.py index 18375d90..ddbd4cae 100644 --- a/src/diffpy/utils/parsers/loaddata.py +++ b/src/diffpy/utils/parsers/data_loader.py @@ -16,7 +16,7 @@ import numpy -def loadData(filename, minrows=10, headers=False, hdel="=", hignore=None, **kwargs): +def load_data(filename, minrows=10, headers=False, hdel="=", hignore=None, **kwargs): """Find and load data from a text file. The data block is identified as the first matrix block of at least minrows rows and constant number of columns. @@ -54,7 +54,7 @@ def loadData(filename, minrows=10, headers=False, hdel="=", hignore=None, **kwar set delimiter to ','. unpack: bool Return data as a sequence of columns that allows tuple unpacking such as x, y = - loadData(FILENAME, unpack=True). Note transposing the loaded array as loadData(FILENAME).T has the same + load_data(FILENAME, unpack=True). Note transposing the loaded array as load_data(FILENAME).T has the same effect. usecols: Zero-based index of columns to be loaded, by default use all detected columns. The reading skips @@ -226,7 +226,7 @@ def readfp(self, fp, append=False): File details include: * File name. - * All data blocks findable by loadData. + * All data blocks findable by load_data. * Headers (if present) for each data block. (Generally the headers contain column name information). """ self._reset() diff --git a/src/diffpy/utils/parsers/serialization.py b/src/diffpy/utils/parsers/serializer.py similarity index 100% rename from src/diffpy/utils/parsers/serialization.py rename to src/diffpy/utils/parsers/serializer.py diff --git a/src/diffpy/utils/scattering_objects/diffraction_objects.py b/src/diffpy/utils/scattering_objects/diffraction_objects.py index 94d2831a..cc707f6f 100644 --- a/src/diffpy/utils/scattering_objects/diffraction_objects.py +++ b/src/diffpy/utils/scattering_objects/diffraction_objects.py @@ -17,7 +17,7 @@ ) -class Diffraction_object: +class DiffractionObject: def __init__(self, name="", wavelength=None): self.name = name self.wavelength = wavelength @@ -29,7 +29,7 @@ def __init__(self, name="", wavelength=None): self.metadata = {} def __eq__(self, other): - if not isinstance(other, Diffraction_object): + if not isinstance(other, DiffractionObject): return NotImplemented self_attributes = [key for key in self.__dict__ if not key.startswith("_")] other_attributes = [key for key in other.__dict__ if not key.startswith("_")] @@ -59,8 +59,8 @@ def __add__(self, other): if isinstance(other, int) or isinstance(other, float) or isinstance(other, np.ndarray): summed.on_tth[1] = self.on_tth[1] + other summed.on_q[1] = self.on_q[1] + other - elif not isinstance(other, Diffraction_object): - raise TypeError("I only know how to sum two Diffraction_object objects") + elif not isinstance(other, DiffractionObject): + raise TypeError("I only know how to sum two DiffractionObject objects") elif self.on_tth[0].all() != other.on_tth[0].all(): raise RuntimeError(x_grid_emsg) else: @@ -73,7 +73,7 @@ def __radd__(self, other): if isinstance(other, int) or isinstance(other, float) or isinstance(other, np.ndarray): summed.on_tth[1] = self.on_tth[1] + other summed.on_q[1] = self.on_q[1] + other - elif not isinstance(other, Diffraction_object): + elif not isinstance(other, DiffractionObject): raise TypeError("I only know how to sum two Scattering_object objects") elif self.on_tth[0].all() != other.on_tth[0].all(): raise RuntimeError(x_grid_emsg) @@ -87,7 +87,7 @@ def __sub__(self, other): if isinstance(other, int) or isinstance(other, float) or isinstance(other, np.ndarray): subtracted.on_tth[1] = self.on_tth[1] - other subtracted.on_q[1] = self.on_q[1] - other - elif not isinstance(other, Diffraction_object): + elif not isinstance(other, DiffractionObject): raise TypeError("I only know how to subtract two Scattering_object objects") elif self.on_tth[0].all() != other.on_tth[0].all(): raise RuntimeError(x_grid_emsg) @@ -101,7 +101,7 @@ def __rsub__(self, other): if isinstance(other, int) or isinstance(other, float) or isinstance(other, np.ndarray): subtracted.on_tth[1] = other - self.on_tth[1] subtracted.on_q[1] = other - self.on_q[1] - elif not isinstance(other, Diffraction_object): + elif not isinstance(other, DiffractionObject): raise TypeError("I only know how to subtract two Scattering_object objects") elif self.on_tth[0].all() != other.on_tth[0].all(): raise RuntimeError(x_grid_emsg) @@ -115,7 +115,7 @@ def __mul__(self, other): if isinstance(other, int) or isinstance(other, float) or isinstance(other, np.ndarray): multiplied.on_tth[1] = other * self.on_tth[1] multiplied.on_q[1] = other * self.on_q[1] - elif not isinstance(other, Diffraction_object): + elif not isinstance(other, DiffractionObject): raise TypeError("I only know how to multiply two Scattering_object objects") elif self.on_tth[0].all() != other.on_tth[0].all(): raise RuntimeError(x_grid_emsg) @@ -141,7 +141,7 @@ def __truediv__(self, other): if isinstance(other, int) or isinstance(other, float) or isinstance(other, np.ndarray): divided.on_tth[1] = other / self.on_tth[1] divided.on_q[1] = other / self.on_q[1] - elif not isinstance(other, Diffraction_object): + elif not isinstance(other, DiffractionObject): raise TypeError("I only know how to multiply two Scattering_object objects") elif self.on_tth[0].all() != other.on_tth[0].all(): raise RuntimeError(x_grid_emsg) @@ -389,7 +389,7 @@ def scale_to(self, target_diff_object, xtype=None, xvalue=None): Parameters ---------- - target_diff_object: Diffraction_object + target_diff_object: DiffractionObject the diffractoin object you want to scale the current one on to xtype: string, optional. Default is Q the xtype, from {XQUANTITIES}, that you will specify a point from to scale to @@ -400,7 +400,7 @@ def scale_to(self, target_diff_object, xtype=None, xvalue=None): Returns ------- - the rescaled Diffraction_object as a new object + the rescaled DiffractionObject as a new object """ scaled = deepcopy(self) @@ -454,7 +454,7 @@ def dump(self, filepath, xtype=None): with open(filepath, "w") as f: f.write( - f"[Diffraction_object]\nname = {self.name}\nwavelength = {self.wavelength}\n" + f"[DiffractionObject]\nname = {self.name}\nwavelength = {self.wavelength}\n" f"scat_quantity = {self.scat_quantity}\n" ) for key, value in self.metadata.items(): diff --git a/src/diffpy/utils/wx/gridutils.py b/src/diffpy/utils/wx/grid_utils.py similarity index 100% rename from src/diffpy/utils/wx/gridutils.py rename to src/diffpy/utils/wx/grid_utils.py From d0ca0c2eaf09a4402d6b16bb12f749230a2fbe13 Mon Sep 17 00:00:00 2001 From: Alison Wu Date: Thu, 7 Nov 2024 23:26:05 -0500 Subject: [PATCH 3/8] moved resample out of parsers --- src/diffpy/utils/{parsers/resample.py => resampler.py} | 0 1 file changed, 0 insertions(+), 0 deletions(-) rename src/diffpy/utils/{parsers/resample.py => resampler.py} (100%) diff --git a/src/diffpy/utils/parsers/resample.py b/src/diffpy/utils/resampler.py similarity index 100% rename from src/diffpy/utils/parsers/resample.py rename to src/diffpy/utils/resampler.py From 9c4e33c86d819cf7a89f21f184e1d30c4c1f3386 Mon Sep 17 00:00:00 2001 From: Alison Wu Date: Thu, 7 Nov 2024 23:27:01 -0500 Subject: [PATCH 4/8] edited tests based on fixed file and function names --- tests/test_diffraction_objects.py | 10 +++++----- tests/test_loaddata.py | 24 ++++++++++++------------ tests/test_resample.py | 2 +- tests/test_serialization.py | 19 ++++++++++--------- 4 files changed, 28 insertions(+), 27 deletions(-) diff --git a/tests/test_diffraction_objects.py b/tests/test_diffraction_objects.py index a155b95e..b2225db8 100644 --- a/tests/test_diffraction_objects.py +++ b/tests/test_diffraction_objects.py @@ -4,7 +4,7 @@ import pytest from freezegun import freeze_time -from diffpy.utils.scattering_objects.diffraction_objects import Diffraction_object +from diffpy.utils.scattering_objects.diffraction_objects import DiffractionObject params = [ ( # Default @@ -222,8 +222,8 @@ @pytest.mark.parametrize("inputs1, inputs2, expected", params) def test_diffraction_objects_equality(inputs1, inputs2, expected): - diffraction_object1 = Diffraction_object() - diffraction_object2 = Diffraction_object() + diffraction_object1 = DiffractionObject() + diffraction_object2 = DiffractionObject() diffraction_object1_attributes = [key for key in diffraction_object1.__dict__ if not key.startswith("_")] for i, attribute in enumerate(diffraction_object1_attributes): setattr(diffraction_object1, attribute, inputs1[i]) @@ -235,7 +235,7 @@ def test_dump(tmp_path, mocker): x, y = np.linspace(0, 5, 6), np.linspace(0, 5, 6) directory = Path(tmp_path) file = directory / "testfile" - test = Diffraction_object() + test = DiffractionObject() test.wavelength = 1.54 test.name = "test" test.scat_quantity = "x-ray" @@ -251,7 +251,7 @@ def test_dump(tmp_path, mocker): with open(file, "r") as f: actual = f.read() expected = ( - "[Diffraction_object]\nname = test\nwavelength = 1.54\nscat_quantity = x-ray\nthing1 = 1\n" + "[DiffractionObject]\nname = test\nwavelength = 1.54\nscat_quantity = x-ray\nthing1 = 1\n" "thing2 = thing2\npackage_info = {'package2': '3.4.5', 'diffpy.utils': '3.3.0'}\n" "creation_time = 2012-01-14 00:00:00\n\n" "#### start data\n0.000000000000000000e+00 0.000000000000000000e+00\n" diff --git a/tests/test_loaddata.py b/tests/test_loaddata.py index 2ca2fa07..d63284cc 100644 --- a/tests/test_loaddata.py +++ b/tests/test_loaddata.py @@ -8,7 +8,7 @@ import numpy import pytest -from diffpy.utils.parsers import loadData +from diffpy.utils.parsers.data_loader import load_data ############################################################################## @@ -18,20 +18,20 @@ def prepare_fixture(self, datafile): self.datafile = datafile def test_loadData_default(self): - """check loadData() with default options""" + """check load_data() with default options""" loaddata01 = self.datafile("loaddata01.txt") d2c = numpy.array([[3, 31], [4, 32], [5, 33]]) - self.assertRaises(IOError, loadData, "doesnotexist") + self.assertRaises(IOError, load_data, "doesnotexist") # the default minrows=10 makes it read from the third line - d = loadData(loaddata01) + d = load_data(loaddata01) self.assertTrue(numpy.array_equal(d2c, d)) # the usecols=(0, 1) would make it read from the third line - d = loadData(loaddata01, minrows=1, usecols=(0, 1)) + d = load_data(loaddata01, minrows=1, usecols=(0, 1)) self.assertTrue(numpy.array_equal(d2c, d)) # check the effect of usecols effect - d = loadData(loaddata01, usecols=(0,)) + d = load_data(loaddata01, usecols=(0,)) self.assertTrue(numpy.array_equal(d2c[:, 0], d)) - d = loadData(loaddata01, usecols=(1,)) + d = load_data(loaddata01, usecols=(1,)) self.assertTrue(numpy.array_equal(d2c[:, 1], d)) return @@ -39,20 +39,20 @@ def test_loadData_1column(self): """check loading of one-column data.""" loaddata01 = self.datafile("loaddata01.txt") d1c = numpy.arange(1, 6) - d = loadData(loaddata01, usecols=[0], minrows=1) + d = load_data(loaddata01, usecols=[0], minrows=1) self.assertTrue(numpy.array_equal(d1c, d)) - d = loadData(loaddata01, usecols=[0], minrows=2) + d = load_data(loaddata01, usecols=[0], minrows=2) self.assertTrue(numpy.array_equal(d1c, d)) - d = loadData(loaddata01, usecols=[0], minrows=3) + d = load_data(loaddata01, usecols=[0], minrows=3) self.assertFalse(numpy.array_equal(d1c, d)) return def test_loadData_headers(self): - """check loadData() with headers options enabled""" + """check load_data() with headers options enabled""" loaddatawithheaders = self.datafile("loaddatawithheaders.txt") hignore = ["# ", "// ", "["] # ignore lines beginning with these strings delimiter = ": " # what our data should be separated by - hdata = loadData(loaddatawithheaders, headers=True, hdel=delimiter, hignore=hignore) + hdata = load_data(loaddatawithheaders, headers=True, hdel=delimiter, hignore=hignore) # only fourteen lines of data are formatted properly assert len(hdata) == 14 # check the following are floats diff --git a/tests/test_resample.py b/tests/test_resample.py index fa9c7e70..deddcca2 100644 --- a/tests/test_resample.py +++ b/tests/test_resample.py @@ -1,7 +1,7 @@ import numpy as np import pytest -from diffpy.utils.parsers.resample import wsinterp +from diffpy.utils.resampler import wsinterp def test_wsinterp(): diff --git a/tests/test_serialization.py b/tests/test_serialization.py index 51921e01..b8079aba 100644 --- a/tests/test_serialization.py +++ b/tests/test_serialization.py @@ -3,8 +3,9 @@ import numpy import pytest -from diffpy.utils.parsers import deserialize_data, loadData, serialize_data from diffpy.utils.parsers.custom_exceptions import ImproperSizeError, UnsupportedTypeError +from diffpy.utils.parsers.data_loader import load_data +from diffpy.utils.parsers.serializer import deserialize_data, serialize_data tests_dir = os.path.dirname(os.path.abspath(locals().get("__file__", "file.py"))) @@ -20,8 +21,8 @@ def test_load_multiple(tmp_path, datafile): for hfname in tlm_list: # gather data using loadData headerfile = os.path.normpath(os.path.join(tests_dir, "testdata", "dbload", hfname)) - hdata = loadData(headerfile, headers=True) - data_table = loadData(headerfile) + hdata = load_data(headerfile, headers=True) + data_table = load_data(headerfile) # check path extraction generated_data = serialize_data(headerfile, hdata, data_table, dt_colnames=["r", "gr"], show_path=True) @@ -50,8 +51,8 @@ def test_exceptions(datafile): loadfile = datafile("loadfile.txt") warningfile = datafile("generatewarnings.txt") nodt = datafile("loaddatawithheaders.txt") - hdata = loadData(loadfile, headers=True) - data_table = loadData(loadfile) + hdata = load_data(loadfile, headers=True) + data_table = load_data(loadfile) # improper file types with pytest.raises(UnsupportedTypeError): @@ -87,15 +88,15 @@ def test_exceptions(datafile): assert numpy.allclose(r_extract[data_name]["r"], r_list) assert numpy.allclose(gr_extract[data_name]["gr"], gr_list) # no datatable - nodt_hdata = loadData(nodt, headers=True) - nodt_dt = loadData(nodt) + nodt_hdata = load_data(nodt, headers=True) + nodt_dt = load_data(nodt) no_dt = serialize_data(nodt, nodt_hdata, nodt_dt, show_path=False) nodt_data_name = list(no_dt.keys())[0] assert numpy.allclose(no_dt[nodt_data_name]["data table"], nodt_dt) # ensure user is warned when columns are overwritten - hdata = loadData(warningfile, headers=True) - data_table = loadData(warningfile) + hdata = load_data(warningfile, headers=True) + data_table = load_data(warningfile) with pytest.warns(RuntimeWarning) as record: serialize_data( warningfile, From 8ecba3f11b59536ab1283079a786a6b50bdf1c75 Mon Sep 17 00:00:00 2001 From: Alison Wu Date: Thu, 7 Nov 2024 23:27:20 -0500 Subject: [PATCH 5/8] add news item --- news/utils-restructure.rst | 23 +++++++++++++++++++++++ 1 file changed, 23 insertions(+) create mode 100644 news/utils-restructure.rst diff --git a/news/utils-restructure.rst b/news/utils-restructure.rst new file mode 100644 index 00000000..a7a39550 --- /dev/null +++ b/news/utils-restructure.rst @@ -0,0 +1,23 @@ +**Added:** + +* + +**Changed:** + +* Moved resampler out of parsers, new path is diffpy.utils.resampler + +**Deprecated:** + +* + +**Removed:** + +* Relative imports in parser's __init__.py + +**Fixed:** + +* File and function names are now all in PEP8 format + +**Security:** + +* From 09938ac56c7fa35a8b7b780e3e78ea233c2d9ca7 Mon Sep 17 00:00:00 2001 From: Alison Wu Date: Fri, 8 Nov 2024 13:21:56 -0500 Subject: [PATCH 6/8] Revert "renamed functions and files to PEP8" This reverts commit 3c9d5177080f48de687178d3904a88b82864f272. --- .../parsers/{data_loader.py => loaddata.py} | 6 ++--- .../{serializer.py => serialization.py} | 0 .../scattering_objects/diffraction_objects.py | 24 +++++++++---------- .../utils/wx/{grid_utils.py => gridutils.py} | 0 4 files changed, 15 insertions(+), 15 deletions(-) rename src/diffpy/utils/parsers/{data_loader.py => loaddata.py} (98%) rename src/diffpy/utils/parsers/{serializer.py => serialization.py} (100%) rename src/diffpy/utils/wx/{grid_utils.py => gridutils.py} (100%) diff --git a/src/diffpy/utils/parsers/data_loader.py b/src/diffpy/utils/parsers/loaddata.py similarity index 98% rename from src/diffpy/utils/parsers/data_loader.py rename to src/diffpy/utils/parsers/loaddata.py index ddbd4cae..18375d90 100644 --- a/src/diffpy/utils/parsers/data_loader.py +++ b/src/diffpy/utils/parsers/loaddata.py @@ -16,7 +16,7 @@ import numpy -def load_data(filename, minrows=10, headers=False, hdel="=", hignore=None, **kwargs): +def loadData(filename, minrows=10, headers=False, hdel="=", hignore=None, **kwargs): """Find and load data from a text file. The data block is identified as the first matrix block of at least minrows rows and constant number of columns. @@ -54,7 +54,7 @@ def load_data(filename, minrows=10, headers=False, hdel="=", hignore=None, **kwa set delimiter to ','. unpack: bool Return data as a sequence of columns that allows tuple unpacking such as x, y = - load_data(FILENAME, unpack=True). Note transposing the loaded array as load_data(FILENAME).T has the same + loadData(FILENAME, unpack=True). Note transposing the loaded array as loadData(FILENAME).T has the same effect. usecols: Zero-based index of columns to be loaded, by default use all detected columns. The reading skips @@ -226,7 +226,7 @@ def readfp(self, fp, append=False): File details include: * File name. - * All data blocks findable by load_data. + * All data blocks findable by loadData. * Headers (if present) for each data block. (Generally the headers contain column name information). """ self._reset() diff --git a/src/diffpy/utils/parsers/serializer.py b/src/diffpy/utils/parsers/serialization.py similarity index 100% rename from src/diffpy/utils/parsers/serializer.py rename to src/diffpy/utils/parsers/serialization.py diff --git a/src/diffpy/utils/scattering_objects/diffraction_objects.py b/src/diffpy/utils/scattering_objects/diffraction_objects.py index cc707f6f..94d2831a 100644 --- a/src/diffpy/utils/scattering_objects/diffraction_objects.py +++ b/src/diffpy/utils/scattering_objects/diffraction_objects.py @@ -17,7 +17,7 @@ ) -class DiffractionObject: +class Diffraction_object: def __init__(self, name="", wavelength=None): self.name = name self.wavelength = wavelength @@ -29,7 +29,7 @@ def __init__(self, name="", wavelength=None): self.metadata = {} def __eq__(self, other): - if not isinstance(other, DiffractionObject): + if not isinstance(other, Diffraction_object): return NotImplemented self_attributes = [key for key in self.__dict__ if not key.startswith("_")] other_attributes = [key for key in other.__dict__ if not key.startswith("_")] @@ -59,8 +59,8 @@ def __add__(self, other): if isinstance(other, int) or isinstance(other, float) or isinstance(other, np.ndarray): summed.on_tth[1] = self.on_tth[1] + other summed.on_q[1] = self.on_q[1] + other - elif not isinstance(other, DiffractionObject): - raise TypeError("I only know how to sum two DiffractionObject objects") + elif not isinstance(other, Diffraction_object): + raise TypeError("I only know how to sum two Diffraction_object objects") elif self.on_tth[0].all() != other.on_tth[0].all(): raise RuntimeError(x_grid_emsg) else: @@ -73,7 +73,7 @@ def __radd__(self, other): if isinstance(other, int) or isinstance(other, float) or isinstance(other, np.ndarray): summed.on_tth[1] = self.on_tth[1] + other summed.on_q[1] = self.on_q[1] + other - elif not isinstance(other, DiffractionObject): + elif not isinstance(other, Diffraction_object): raise TypeError("I only know how to sum two Scattering_object objects") elif self.on_tth[0].all() != other.on_tth[0].all(): raise RuntimeError(x_grid_emsg) @@ -87,7 +87,7 @@ def __sub__(self, other): if isinstance(other, int) or isinstance(other, float) or isinstance(other, np.ndarray): subtracted.on_tth[1] = self.on_tth[1] - other subtracted.on_q[1] = self.on_q[1] - other - elif not isinstance(other, DiffractionObject): + elif not isinstance(other, Diffraction_object): raise TypeError("I only know how to subtract two Scattering_object objects") elif self.on_tth[0].all() != other.on_tth[0].all(): raise RuntimeError(x_grid_emsg) @@ -101,7 +101,7 @@ def __rsub__(self, other): if isinstance(other, int) or isinstance(other, float) or isinstance(other, np.ndarray): subtracted.on_tth[1] = other - self.on_tth[1] subtracted.on_q[1] = other - self.on_q[1] - elif not isinstance(other, DiffractionObject): + elif not isinstance(other, Diffraction_object): raise TypeError("I only know how to subtract two Scattering_object objects") elif self.on_tth[0].all() != other.on_tth[0].all(): raise RuntimeError(x_grid_emsg) @@ -115,7 +115,7 @@ def __mul__(self, other): if isinstance(other, int) or isinstance(other, float) or isinstance(other, np.ndarray): multiplied.on_tth[1] = other * self.on_tth[1] multiplied.on_q[1] = other * self.on_q[1] - elif not isinstance(other, DiffractionObject): + elif not isinstance(other, Diffraction_object): raise TypeError("I only know how to multiply two Scattering_object objects") elif self.on_tth[0].all() != other.on_tth[0].all(): raise RuntimeError(x_grid_emsg) @@ -141,7 +141,7 @@ def __truediv__(self, other): if isinstance(other, int) or isinstance(other, float) or isinstance(other, np.ndarray): divided.on_tth[1] = other / self.on_tth[1] divided.on_q[1] = other / self.on_q[1] - elif not isinstance(other, DiffractionObject): + elif not isinstance(other, Diffraction_object): raise TypeError("I only know how to multiply two Scattering_object objects") elif self.on_tth[0].all() != other.on_tth[0].all(): raise RuntimeError(x_grid_emsg) @@ -389,7 +389,7 @@ def scale_to(self, target_diff_object, xtype=None, xvalue=None): Parameters ---------- - target_diff_object: DiffractionObject + target_diff_object: Diffraction_object the diffractoin object you want to scale the current one on to xtype: string, optional. Default is Q the xtype, from {XQUANTITIES}, that you will specify a point from to scale to @@ -400,7 +400,7 @@ def scale_to(self, target_diff_object, xtype=None, xvalue=None): Returns ------- - the rescaled DiffractionObject as a new object + the rescaled Diffraction_object as a new object """ scaled = deepcopy(self) @@ -454,7 +454,7 @@ def dump(self, filepath, xtype=None): with open(filepath, "w") as f: f.write( - f"[DiffractionObject]\nname = {self.name}\nwavelength = {self.wavelength}\n" + f"[Diffraction_object]\nname = {self.name}\nwavelength = {self.wavelength}\n" f"scat_quantity = {self.scat_quantity}\n" ) for key, value in self.metadata.items(): diff --git a/src/diffpy/utils/wx/grid_utils.py b/src/diffpy/utils/wx/gridutils.py similarity index 100% rename from src/diffpy/utils/wx/grid_utils.py rename to src/diffpy/utils/wx/gridutils.py From 57e8a0ba242d7c7b82e9f44a21ce57134d50f397 Mon Sep 17 00:00:00 2001 From: Alison Wu Date: Fri, 8 Nov 2024 13:22:43 -0500 Subject: [PATCH 7/8] Revert "edited tests based on fixed file and function names" This reverts commit 9c4e33c86d819cf7a89f21f184e1d30c4c1f3386. --- tests/test_diffraction_objects.py | 10 +++++----- tests/test_loaddata.py | 24 ++++++++++++------------ tests/test_resample.py | 2 +- tests/test_serialization.py | 19 +++++++++---------- 4 files changed, 27 insertions(+), 28 deletions(-) diff --git a/tests/test_diffraction_objects.py b/tests/test_diffraction_objects.py index b2225db8..a155b95e 100644 --- a/tests/test_diffraction_objects.py +++ b/tests/test_diffraction_objects.py @@ -4,7 +4,7 @@ import pytest from freezegun import freeze_time -from diffpy.utils.scattering_objects.diffraction_objects import DiffractionObject +from diffpy.utils.scattering_objects.diffraction_objects import Diffraction_object params = [ ( # Default @@ -222,8 +222,8 @@ @pytest.mark.parametrize("inputs1, inputs2, expected", params) def test_diffraction_objects_equality(inputs1, inputs2, expected): - diffraction_object1 = DiffractionObject() - diffraction_object2 = DiffractionObject() + diffraction_object1 = Diffraction_object() + diffraction_object2 = Diffraction_object() diffraction_object1_attributes = [key for key in diffraction_object1.__dict__ if not key.startswith("_")] for i, attribute in enumerate(diffraction_object1_attributes): setattr(diffraction_object1, attribute, inputs1[i]) @@ -235,7 +235,7 @@ def test_dump(tmp_path, mocker): x, y = np.linspace(0, 5, 6), np.linspace(0, 5, 6) directory = Path(tmp_path) file = directory / "testfile" - test = DiffractionObject() + test = Diffraction_object() test.wavelength = 1.54 test.name = "test" test.scat_quantity = "x-ray" @@ -251,7 +251,7 @@ def test_dump(tmp_path, mocker): with open(file, "r") as f: actual = f.read() expected = ( - "[DiffractionObject]\nname = test\nwavelength = 1.54\nscat_quantity = x-ray\nthing1 = 1\n" + "[Diffraction_object]\nname = test\nwavelength = 1.54\nscat_quantity = x-ray\nthing1 = 1\n" "thing2 = thing2\npackage_info = {'package2': '3.4.5', 'diffpy.utils': '3.3.0'}\n" "creation_time = 2012-01-14 00:00:00\n\n" "#### start data\n0.000000000000000000e+00 0.000000000000000000e+00\n" diff --git a/tests/test_loaddata.py b/tests/test_loaddata.py index d63284cc..2ca2fa07 100644 --- a/tests/test_loaddata.py +++ b/tests/test_loaddata.py @@ -8,7 +8,7 @@ import numpy import pytest -from diffpy.utils.parsers.data_loader import load_data +from diffpy.utils.parsers import loadData ############################################################################## @@ -18,20 +18,20 @@ def prepare_fixture(self, datafile): self.datafile = datafile def test_loadData_default(self): - """check load_data() with default options""" + """check loadData() with default options""" loaddata01 = self.datafile("loaddata01.txt") d2c = numpy.array([[3, 31], [4, 32], [5, 33]]) - self.assertRaises(IOError, load_data, "doesnotexist") + self.assertRaises(IOError, loadData, "doesnotexist") # the default minrows=10 makes it read from the third line - d = load_data(loaddata01) + d = loadData(loaddata01) self.assertTrue(numpy.array_equal(d2c, d)) # the usecols=(0, 1) would make it read from the third line - d = load_data(loaddata01, minrows=1, usecols=(0, 1)) + d = loadData(loaddata01, minrows=1, usecols=(0, 1)) self.assertTrue(numpy.array_equal(d2c, d)) # check the effect of usecols effect - d = load_data(loaddata01, usecols=(0,)) + d = loadData(loaddata01, usecols=(0,)) self.assertTrue(numpy.array_equal(d2c[:, 0], d)) - d = load_data(loaddata01, usecols=(1,)) + d = loadData(loaddata01, usecols=(1,)) self.assertTrue(numpy.array_equal(d2c[:, 1], d)) return @@ -39,20 +39,20 @@ def test_loadData_1column(self): """check loading of one-column data.""" loaddata01 = self.datafile("loaddata01.txt") d1c = numpy.arange(1, 6) - d = load_data(loaddata01, usecols=[0], minrows=1) + d = loadData(loaddata01, usecols=[0], minrows=1) self.assertTrue(numpy.array_equal(d1c, d)) - d = load_data(loaddata01, usecols=[0], minrows=2) + d = loadData(loaddata01, usecols=[0], minrows=2) self.assertTrue(numpy.array_equal(d1c, d)) - d = load_data(loaddata01, usecols=[0], minrows=3) + d = loadData(loaddata01, usecols=[0], minrows=3) self.assertFalse(numpy.array_equal(d1c, d)) return def test_loadData_headers(self): - """check load_data() with headers options enabled""" + """check loadData() with headers options enabled""" loaddatawithheaders = self.datafile("loaddatawithheaders.txt") hignore = ["# ", "// ", "["] # ignore lines beginning with these strings delimiter = ": " # what our data should be separated by - hdata = load_data(loaddatawithheaders, headers=True, hdel=delimiter, hignore=hignore) + hdata = loadData(loaddatawithheaders, headers=True, hdel=delimiter, hignore=hignore) # only fourteen lines of data are formatted properly assert len(hdata) == 14 # check the following are floats diff --git a/tests/test_resample.py b/tests/test_resample.py index deddcca2..fa9c7e70 100644 --- a/tests/test_resample.py +++ b/tests/test_resample.py @@ -1,7 +1,7 @@ import numpy as np import pytest -from diffpy.utils.resampler import wsinterp +from diffpy.utils.parsers.resample import wsinterp def test_wsinterp(): diff --git a/tests/test_serialization.py b/tests/test_serialization.py index b8079aba..51921e01 100644 --- a/tests/test_serialization.py +++ b/tests/test_serialization.py @@ -3,9 +3,8 @@ import numpy import pytest +from diffpy.utils.parsers import deserialize_data, loadData, serialize_data from diffpy.utils.parsers.custom_exceptions import ImproperSizeError, UnsupportedTypeError -from diffpy.utils.parsers.data_loader import load_data -from diffpy.utils.parsers.serializer import deserialize_data, serialize_data tests_dir = os.path.dirname(os.path.abspath(locals().get("__file__", "file.py"))) @@ -21,8 +20,8 @@ def test_load_multiple(tmp_path, datafile): for hfname in tlm_list: # gather data using loadData headerfile = os.path.normpath(os.path.join(tests_dir, "testdata", "dbload", hfname)) - hdata = load_data(headerfile, headers=True) - data_table = load_data(headerfile) + hdata = loadData(headerfile, headers=True) + data_table = loadData(headerfile) # check path extraction generated_data = serialize_data(headerfile, hdata, data_table, dt_colnames=["r", "gr"], show_path=True) @@ -51,8 +50,8 @@ def test_exceptions(datafile): loadfile = datafile("loadfile.txt") warningfile = datafile("generatewarnings.txt") nodt = datafile("loaddatawithheaders.txt") - hdata = load_data(loadfile, headers=True) - data_table = load_data(loadfile) + hdata = loadData(loadfile, headers=True) + data_table = loadData(loadfile) # improper file types with pytest.raises(UnsupportedTypeError): @@ -88,15 +87,15 @@ def test_exceptions(datafile): assert numpy.allclose(r_extract[data_name]["r"], r_list) assert numpy.allclose(gr_extract[data_name]["gr"], gr_list) # no datatable - nodt_hdata = load_data(nodt, headers=True) - nodt_dt = load_data(nodt) + nodt_hdata = loadData(nodt, headers=True) + nodt_dt = loadData(nodt) no_dt = serialize_data(nodt, nodt_hdata, nodt_dt, show_path=False) nodt_data_name = list(no_dt.keys())[0] assert numpy.allclose(no_dt[nodt_data_name]["data table"], nodt_dt) # ensure user is warned when columns are overwritten - hdata = load_data(warningfile, headers=True) - data_table = load_data(warningfile) + hdata = loadData(warningfile, headers=True) + data_table = loadData(warningfile) with pytest.warns(RuntimeWarning) as record: serialize_data( warningfile, From f32c8054583d798c2bfad11b7923c665c4208cdb Mon Sep 17 00:00:00 2001 From: Alison Wu Date: Fri, 8 Nov 2024 13:27:19 -0500 Subject: [PATCH 8/8] re-edit tests --- news/utils-restructure.rst | 2 +- tests/test_loaddata.py | 2 +- tests/test_resample.py | 2 +- tests/test_serialization.py | 3 ++- 4 files changed, 5 insertions(+), 4 deletions(-) diff --git a/news/utils-restructure.rst b/news/utils-restructure.rst index a7a39550..220fc2f5 100644 --- a/news/utils-restructure.rst +++ b/news/utils-restructure.rst @@ -16,7 +16,7 @@ **Fixed:** -* File and function names are now all in PEP8 format +* **Security:** diff --git a/tests/test_loaddata.py b/tests/test_loaddata.py index 2ca2fa07..3d775c74 100644 --- a/tests/test_loaddata.py +++ b/tests/test_loaddata.py @@ -8,7 +8,7 @@ import numpy import pytest -from diffpy.utils.parsers import loadData +from diffpy.utils.parsers.loaddata import loadData ############################################################################## diff --git a/tests/test_resample.py b/tests/test_resample.py index fa9c7e70..deddcca2 100644 --- a/tests/test_resample.py +++ b/tests/test_resample.py @@ -1,7 +1,7 @@ import numpy as np import pytest -from diffpy.utils.parsers.resample import wsinterp +from diffpy.utils.resampler import wsinterp def test_wsinterp(): diff --git a/tests/test_serialization.py b/tests/test_serialization.py index 51921e01..fc3696bf 100644 --- a/tests/test_serialization.py +++ b/tests/test_serialization.py @@ -3,8 +3,9 @@ import numpy import pytest -from diffpy.utils.parsers import deserialize_data, loadData, serialize_data from diffpy.utils.parsers.custom_exceptions import ImproperSizeError, UnsupportedTypeError +from diffpy.utils.parsers.loaddata import loadData +from diffpy.utils.parsers.serialization import deserialize_data, serialize_data tests_dir = os.path.dirname(os.path.abspath(locals().get("__file__", "file.py")))