From 459b8cb48fc5671a90ea8b9ac1c910f927e21339 Mon Sep 17 00:00:00 2001 From: Michael Walsh Date: Thu, 24 Mar 2022 10:46:55 -0400 Subject: [PATCH] more flake8 issues --- pystog/cli.py | 4 +-- pystog/stog.py | 37 ++++++++++++++++----- tests/test_stog.py | 83 ++++++++++++++++++++++++++++++++-------------- 3 files changed, 89 insertions(+), 35 deletions(-) diff --git a/pystog/cli.py b/pystog/cli.py index 87945ed..fdb64a1 100755 --- a/pystog/cli.py +++ b/pystog/cli.py @@ -29,7 +29,7 @@ def pystog_cli(kwargs=None): args = parser.parse_args() if args.json: print("loading config from '%s'" % args.json) - with open(args.json, 'r') as f: + with open(args.json, "r") as f: kwargs = json.load(f) else: @@ -43,7 +43,7 @@ def pystog_cli(kwargs=None): stog = StoG(**kwargs) if "NexusFile" in kwargs: stog.read_all_nexus_file_banks() - else: + else: stog.read_all_data(skiprows=3) stog.merge_data() stog.write_out_merged_sq() diff --git a/pystog/stog.py b/pystog/stog.py index b551ef1..64f8015 100644 --- a/pystog/stog.py +++ b/pystog/stog.py @@ -667,7 +667,8 @@ def real_space_function(self): def real_space_function(self, real_space_function): if real_space_function not in RealSpaceChoices: raise ValueError( - "real_space_function must be of %s" % ",".join(RealSpaceChoices.keys()) + "real_space_function must be of %s" + % ",".join(RealSpaceChoices.keys()) ) self.__real_space_function = real_space_function self.__gr_ft_title = "%s FT" % real_space_function @@ -807,7 +808,7 @@ def GKofR_title(self, title): def extract(self, hdf_file, path, index=None): data = File(hdf_file, "r") - base = path.split("/")[1] + # base = path.split("/")[1] # print(data[base+"/title"].value) if index is not None: return data[path][index] @@ -873,7 +874,9 @@ def read_all_nexus_file_banks(self): # Read in all the data files for i, file_info in enumerate(self.files): self.read_nexus_file_by_bank( - self.nexus_file, int(file_info["BankNumber"]), self.workspace_name + self.nexus_file, + int(file_info["BankNumber"]), + self.workspace_name, ) def read_all_data(self, **kwargs): @@ -1036,7 +1039,9 @@ def add_dataset( self.sq_individuals = np.concatenate(array_seq, axis=1) @staticmethod - def apply_scales_and_offset(x, y, dy=None, yscale=1.0, yoffset=0.0, xoffset=0.0): + def apply_scales_and_offset( + x, y, dy=None, yscale=1.0, yoffset=0.0, xoffset=0.0 + ): """ Applies scales to the Y-axis and offsets to both X and Y axes. @@ -1125,7 +1130,11 @@ def merge_data(self): else: if _n_total > 0: data_merged.append( - [_previous_x, _n_sum / _n_total, np.sqrt(_n_err) / _n_total] + [ + _previous_x, + _n_sum / _n_total, + np.sqrt(_n_err) / _n_total, + ] ) _n_total = 1.0 _n_sum = item[1] @@ -1197,9 +1206,13 @@ def transform_merged(self): "^2": self.bcoh_sqrd, } if self.real_space_function == "g(r)": - r, gofr, dgofr = self.transformer.S_to_g(q, sq, self.dr, **transform_kwargs) + r, gofr, dgofr = self.transformer.S_to_g( + q, sq, self.dr, **transform_kwargs + ) elif self.real_space_function == "G(r)": - r, gofr, dgofr = self.transformer.S_to_G(q, sq, self.dr, **transform_kwargs) + r, gofr, dgofr = self.transformer.S_to_G( + q, sq, self.dr, **transform_kwargs + ) elif self.real_space_function == "GK(r)": r, gofr, dgofr = self.transformer.S_to_GK( q, sq, self.dr, **transform_kwargs @@ -1311,13 +1324,19 @@ def apply_lorch(self, q, sq, r): q, sq, r, **{"lorch": True, "rho": self.density} ) elif self.real_space_function == "G(r)": - r, gr_lorch, _ = self.transformer.S_to_G(q, sq, r, **{"lorch": True}) + r, gr_lorch, _ = self.transformer.S_to_G( + q, sq, r, **{"lorch": True} + ) elif self.real_space_function == "GK(r)": r, gr_lorch, _ = self.transformer.S_to_GK( q, sq, r, - **{"lorch": True, "rho": self.density, "^2": self.bcoh_sqrd} + **{ + "lorch": True, + "rho": self.density, + "^2": self.bcoh_sqrd, + } ) self.gr_master[self.gr_lorch_title] = gr_lorch diff --git a/tests/test_stog.py b/tests/test_stog.py index 2eee8b1..505fc17 100644 --- a/tests/test_stog.py +++ b/tests/test_stog.py @@ -3,8 +3,6 @@ import tempfile import unittest -from h5py import File - from tests.utils import get_data_path, get_index_of_function, load_data from tests.materials import Argon from pystog.utils import RealSpaceHeaders, ReciprocalSpaceHeaders @@ -26,9 +24,15 @@ def initialize_material(self): data = load_data(self.material.reciprocal_space_filename) self.q = data[:, get_index_of_function("Q", ReciprocalSpaceHeaders)] self.sq = data[:, get_index_of_function("S(Q)", ReciprocalSpaceHeaders)] - self.fq = data[:, get_index_of_function("Q[S(Q)-1]", ReciprocalSpaceHeaders)] - self.fq_keen = data[:, get_index_of_function("FK(Q)", ReciprocalSpaceHeaders)] - self.dcs = data[:, get_index_of_function("DCS(Q)", ReciprocalSpaceHeaders)] + self.fq = data[ + :, get_index_of_function("Q[S(Q)-1]", ReciprocalSpaceHeaders) + ] + self.fq_keen = data[ + :, get_index_of_function("FK(Q)", ReciprocalSpaceHeaders) + ] + self.dcs = data[ + :, get_index_of_function("DCS(Q)", ReciprocalSpaceHeaders) + ] # targets for 1st peaks self.sq_target = self.material.sq_target @@ -424,7 +428,9 @@ def test_stog_add_dataset(self): stog.reciprocal_individuals[0][self.first], self.reciprocal_xtarget ) self.assertAlmostEqual( - stog.reciprocal_individuals[1][self.first], self.sq_target[0], places=places + stog.reciprocal_individuals[1][self.first], + self.sq_target[0], + places=places, ) self.assertEqual(stog.reciprocal_individuals[2][self.first], 0.0) self.assertAlmostEqual( @@ -443,7 +449,9 @@ def test_stog_add_dataset(self): self.fq_target[0], places=places, ) - self.assertEqual(stog.reciprocal_individuals[2][self.first + stride], 0.0) + self.assertEqual( + stog.reciprocal_individuals[2][self.first + stride], 0.0 + ) self.assertAlmostEqual( stog.sq_individuals[1][self.first], self.sq_target[0], places=places ) @@ -459,7 +467,9 @@ def test_stog_add_dataset(self): self.fq_keen_target[0], places=places, ) - self.assertEqual(stog.reciprocal_individuals[2][self.first + stride], 0.0) + self.assertEqual( + stog.reciprocal_individuals[2][self.first + stride], 0.0 + ) self.assertAlmostEqual( stog.sq_individuals[1][self.first], self.sq_target[0], places=places ) @@ -475,7 +485,9 @@ def test_stog_add_dataset(self): self.dcs_target[0], places=places, ) - self.assertEqual(stog.reciprocal_individuals[2][self.first + stride], 0.0) + self.assertEqual( + stog.reciprocal_individuals[2][self.first + stride], 0.0 + ) self.assertAlmostEqual( stog.sq_individuals[1][self.first], self.sq_target[0], places=places ) @@ -494,7 +506,9 @@ def test_stog_add_dataset_yscale(self): stog.reciprocal_individuals[1][self.first], self.sq_target[0] ) self.assertEqual(stog.reciprocal_individuals[2][self.first], 0.0) - self.assertNotEqual(stog.sq_individuals[1][self.first], self.sq_target[0]) + self.assertNotEqual( + stog.sq_individuals[1][self.first], self.sq_target[0] + ) self.assertEqual(stog.sq_individuals[2][self.first], 0.0) def test_stog_add_dataset_yoffset(self): @@ -510,7 +524,9 @@ def test_stog_add_dataset_yoffset(self): stog.reciprocal_individuals[1][self.first], self.sq_target[0] ) self.assertEqual(stog.reciprocal_individuals[2][self.first], 0.0) - self.assertNotEqual(stog.sq_individuals[1][self.first], self.sq_target[0]) + self.assertNotEqual( + stog.sq_individuals[1][self.first], self.sq_target[0] + ) self.assertEqual(stog.sq_individuals[2][self.first], 0.0) def test_stog_add_dataset_yscale_with_dy(self): @@ -530,7 +546,9 @@ def test_stog_add_dataset_yscale_with_dy(self): dsq_target = info["Y"]["Scale"] * 2.59173 self.assertEqual(stog.reciprocal_individuals[2][self.first], dsq_target) - self.assertNotEqual(stog.sq_individuals[1][self.first], self.sq_target[0]) + self.assertNotEqual( + stog.sq_individuals[1][self.first], self.sq_target[0] + ) self.assertEqual(stog.sq_individuals[2][self.first], dsq_target) @@ -551,7 +569,9 @@ def test_stog_add_dataset_yoffset_with_dy(self): dsq_target = 2.59173 self.assertEqual(stog.reciprocal_individuals[2][self.first], dsq_target) - self.assertNotEqual(stog.sq_individuals[1][self.first], self.sq_target[0]) + self.assertNotEqual( + stog.sq_individuals[1][self.first], self.sq_target[0] + ) self.assertEqual(stog.sq_individuals[2][self.first], dsq_target) @@ -587,7 +607,9 @@ def test_stog_add_dataset_default_reciprocal_space_function(self): ) self.assertEqual(stog.reciprocal_individuals[2][self.first], 0.0) self.assertAlmostEqual( - stog.reciprocal_individuals[1][self.first], self.sq_target[0], places=places + stog.reciprocal_individuals[1][self.first], + self.sq_target[0], + places=places, ) self.assertEqual(stog.sq_individuals[2][self.first], 0.0) @@ -599,9 +621,6 @@ def test_stog_add_dataset_wrong_reciprocal_space_function_exception(self): stog.add_dataset(info) def test_stog_read_nexus_file_by_bank(self): - # Number of decimal places for precision - places = 5 - stog = StoG() bank = 1 nexus_file = "./tests/test_data/nexus/pystog_test.nxs" @@ -669,9 +688,13 @@ def test_stog_read_dataset(self): stog.reciprocal_individuals[0][self.first], self.reciprocal_xtarget ) self.assertAlmostEqual( - stog.reciprocal_individuals[1][self.first], self.sq_target[0], places=places + stog.reciprocal_individuals[1][self.first], + self.sq_target[0], + places=places, + ) + self.assertEqual( + stog.reciprocal_individuals[2][self.first], self.dsq_target ) - self.assertEqual(stog.reciprocal_individuals[2][self.first], self.dsq_target) self.assertAlmostEqual( stog.sq_individuals[1][self.first], self.sq_target[0], places=places ) @@ -717,7 +740,9 @@ def test_stog_read_dataset_dycol_too_large(self): stog.reciprocal_individuals[0][self.first], self.reciprocal_xtarget ) self.assertAlmostEqual( - stog.reciprocal_individuals[1][self.first], self.sq_target[0], places=places + stog.reciprocal_individuals[1][self.first], + self.sq_target[0], + places=places, ) self.assertEqual(stog.reciprocal_individuals[2][self.first], 0.0) self.assertAlmostEqual( @@ -762,7 +787,9 @@ def test_stog_read_all_data(self): places=places, ) self.assertAlmostEqual( - stog.sq_individuals[1][self.first], self.sq_target[0], places=places + stog.sq_individuals[1][self.first], + self.sq_target[0], + places=places, ) def test_stog_merge_data(self): @@ -780,7 +807,9 @@ def test_stog_merge_data(self): stog.q_master[stog.sq_title][self.first], self.reciprocal_xtarget ) self.assertAlmostEqual( - stog.sq_master[stog.sq_title][self.first], self.sq_target[0], places=places + stog.sq_master[stog.sq_title][self.first], + self.sq_target[0], + places=places, ) def test_stog_merge_data_qsq_opts_scale(self): @@ -1195,7 +1224,11 @@ def wrap_function(*args): np.testing.assert_allclose(data["x"], x) np.testing.assert_allclose( - data["y"] - y, np.zeros(len(y)), rtol=2.0, atol=2.0, equal_nan=True + data["y"] - y, + np.zeros(len(y)), + rtol=2.0, + atol=2.0, + equal_nan=True, ) os.remove(filename) @@ -1224,7 +1257,9 @@ def test_stog_write_df(self): y = self.stog.sq_master[self.stog.sq_title] self.stog._write_out_to_file(x, y, outfile_path) data = {} - data["x"], data["y"] = np.genfromtxt(outfile_path, skip_header=2, unpack=True) + data["x"], data["y"] = np.genfromtxt( + outfile_path, skip_header=2, unpack=True + ) q = self.stog.q_master[self.stog.sq_title] sq = self.stog.sq_master[self.stog.sq_title]