Skip to content

Commit

Permalink
more flake8 issues
Browse files Browse the repository at this point in the history
  • Loading branch information
walshmm committed Mar 24, 2022
1 parent 0faac35 commit 459b8cb
Show file tree
Hide file tree
Showing 3 changed files with 89 additions and 35 deletions.
4 changes: 2 additions & 2 deletions pystog/cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand All @@ -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()
Expand Down
37 changes: 28 additions & 9 deletions pystog/stog.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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]
Expand Down Expand Up @@ -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):
Expand Down Expand Up @@ -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.
Expand Down Expand Up @@ -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]
Expand Down Expand Up @@ -1197,9 +1206,13 @@ def transform_merged(self):
"<b_coh>^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
Expand Down Expand Up @@ -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, "<b_coh>^2": self.bcoh_sqrd}
**{
"lorch": True,
"rho": self.density,
"<b_coh>^2": self.bcoh_sqrd,
}
)

self.gr_master[self.gr_lorch_title] = gr_lorch
Expand Down
83 changes: 59 additions & 24 deletions tests/test_stog.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand Down Expand Up @@ -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(
Expand All @@ -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
)
Expand All @@ -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
)
Expand All @@ -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
)
Expand All @@ -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):
Expand All @@ -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):
Expand All @@ -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)

Expand All @@ -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)

Expand Down Expand Up @@ -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)

Expand All @@ -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"
Expand Down Expand Up @@ -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
)
Expand Down Expand Up @@ -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(
Expand Down Expand Up @@ -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):
Expand All @@ -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):
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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]
Expand Down

0 comments on commit 459b8cb

Please sign in to comment.