Skip to content

Commit

Permalink
Fix linting
Browse files Browse the repository at this point in the history
Fix quotes
  • Loading branch information
sstendahl committed Sep 5, 2023
1 parent c455176 commit 45e846e
Show file tree
Hide file tree
Showing 2 changed files with 47 additions and 35 deletions.
16 changes: 7 additions & 9 deletions hogben/models/samples.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import os
import sys
from typing import Optional

import matplotlib.pyplot as plt
Expand Down Expand Up @@ -150,8 +149,8 @@ def _get_sld_profile(self):

# Determine if the structure was defined in Refl1D.
elif isinstance(self.structure, refl1d.model.Stack):
q = np.geomspace(0.005, 0.3, 500) # This is not used.
scale, bkg, dq = 1, 1e-6, 2 # These are not used.
q = np.geomspace(0.005, 0.3, 500) # This is not used.
scale, bkg, dq = 1, 1e-6, 2 # These are not used.
experiment = refl1d_experiment(self.structure, q, scale, bkg, dq)
z, slds, _ = experiment.smooth_profile()
# Otherwise, the structure is invalid.
Expand Down Expand Up @@ -183,7 +182,7 @@ def reflectivity_profile(
"""
# Calculate the model reflectivity.
q, r = self._get_reflectivity_profile(q_min, q_max, points, scale,
bkg, dq)
bkg, dq)

# Plot Q versus model reflectivity.
fig = plt.figure()
Expand Down Expand Up @@ -215,7 +214,8 @@ def _get_reflectivity_profile(self, q_min, q_max, points, scale, bkg, dq):

# Determine if the structure was defined in refnx.
if isinstance(self.structure, refnx.reflect.Structure):
model = refnx.reflect.ReflectModel(self.structure, scale=scale, bkg=bkg, dq=dq)
model = refnx.reflect.ReflectModel(self.structure, scale=scale,
bkg=bkg, dq=dq)

# Determine if the structure was defined in Refl1D.
elif isinstance(self.structure, refl1d.model.Stack):
Expand All @@ -228,8 +228,6 @@ def _get_reflectivity_profile(self, q_min, q_max, points, scale, bkg, dq):
r = reflectivity(q, model)
return q, r



def nested_sampling(self,
angle_times: list,
save_path: str,
Expand Down Expand Up @@ -416,6 +414,7 @@ def similar_sld_sample_2():
structure.name = 'similar_sld_sample_2'
return Sample(structure)


def run_main(save_path: Optional[str] = '../results') -> None:
"""
Runs the main function of the module, retrieves an SLD and
Expand All @@ -439,6 +438,5 @@ def run_main(save_path: Optional[str] = '../results') -> None:
plt.close('all')



if __name__ == '__main__':
run_main()
run_main()
66 changes: 40 additions & 26 deletions hogben/tests/test_samples.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ def refnx_sample():
structure = air | layer1 | layer2 | substrate
return Sample(structure)


@pytest.fixture
def refl1d_sample():
"""Define a bilayer sample, and return the associated refl1d model"""
Expand All @@ -38,6 +39,7 @@ def refl1d_sample():
structure = substrate | layer2 | layer1 | air
return Sample(structure)


def mock_save_plot(fig: matplotlib.figure.Figure,
save_path: str,
filename: str) -> None:
Expand All @@ -55,6 +57,7 @@ def mock_save_plot(fig: matplotlib.figure.Figure,
file_path = os.path.join(save_path, filename + '.png')
fig.savefig(file_path, dpi=40)


def compare_sample_structure(refl1d: refl1d.model.Stack,
refnx: refnx.reflect.Structure) -> None:
"""
Expand All @@ -75,20 +78,20 @@ def compare_sample_structure(refl1d: refl1d.model.Stack,
# Check structure in reversed order, so it matches with refl1d
for component in list(reversed(refnx.structure))[1:]:
refnx_params[component.name] = {
"sld": component.sld.real.value,
"thick": component.thick.value,
"rough": component.rough.value
'sld': component.sld.real.value,
'thick': component.thick.value,
'rough': component.rough.value
}
for component in refl1d.structure[1:]:
refl1d_params[component.name] = {
"sld": component.material.rho.value,
"thick": component.thickness.value,
"rough": component.interface.value
'sld': component.material.rho.value,
'thick': component.thickness.value,
'rough': component.interface.value
}
return refnx_params == refl1d_params

@pytest.mark.parametrize('sample_class', ("refnx_sample",
"refl1d_sample"))

@pytest.mark.parametrize('sample_class', ('refnx_sample', 'refl1d_sample'))
def test_angle_info(sample_class, request):
"""
Tests whether the angle_info function correctly calculates the Fisher
Expand All @@ -108,6 +111,7 @@ def test_angle_info(sample_class, request):

np.testing.assert_allclose(g, angle_info, rtol=1e-08)


@patch('hogben.models.samples.Sample._get_sld_profile')
@patch('hogben.models.samples.save_plot', side_effect=mock_save_plot)
def test_sld_profile_valid_figure(_mock_save_plot,
Expand All @@ -118,13 +122,13 @@ def test_sld_profile_valid_figure(_mock_save_plot,
mock_sld_profile.return_value = ([0, 10, 60, 110, 160, 210],
[4, 9, -2, 9, -2, 9])


# Use temporary directory, so it doesn't leave any files after testing
with tempfile.TemporaryDirectory() as temp_dir:
refnx_sample.sld_profile(temp_dir)
img_test = os.path.join(temp_dir, refnx_sample.name, 'sld_profile.png')
assert os.path.isfile(img_test)


@patch('hogben.models.samples.Sample._get_reflectivity_profile')
@patch('hogben.models.samples.save_plot', side_effect=mock_save_plot)
def test_reflectivity_profile_valid_figure(_mock_save_plot,
Expand All @@ -143,24 +147,22 @@ def test_reflectivity_profile_valid_figure(_mock_save_plot,
'reflectivity_profile.png')
assert os.path.isfile(img_test)


@patch('hogben.models.samples.save_plot', side_effect=mock_save_plot)
@pytest.mark.parametrize('sample_class', ("refnx_sample",
"refl1d_sample"))
def test_sld_profile_length(_mock_save_plot, sample_class,
request):
@pytest.mark.parametrize('sample_class', ('refnx_sample', 'refl1d_sample'))
def test_sld_profile_length(_mock_save_plot, sample_class, request):
"""
Tests whether _get_sld_profile() succesfully retrieves two arrays with
equal lengths, representing an SLD profile that can be plotted in a figure
"""
sample = request.getfixturevalue(sample_class)
z, slds = sample._get_sld_profile()
assert len(z) == len(slds)
assert len(z) > 0 # Make sure arrays are not empty
assert len(z) > 0 # Make sure arrays are not empty


@pytest.mark.parametrize('sample_class', ("refnx_sample",
"refl1d_sample"))
def test_reflectivity_profile_positive(sample_class,
request):
@pytest.mark.parametrize('sample_class', ('refnx_sample', 'refl1d_sample'))
def test_reflectivity_profile_positive(sample_class, request):
"""
Tests whether _get_reflectivity_profile() succesfully obtains reflectivity
values that are all positively valued
Expand All @@ -169,15 +171,16 @@ def test_reflectivity_profile_positive(sample_class,
q, r = sample._get_reflectivity_profile(0.005, 0.4, 500, 1, 1e-7, 2)
assert min(r) > 0


def test_reflectivity_invalid_structure():
"""
Test whether a RunTimeError is correctly given when an invalid sample
structure is used in get_reflectivity_profile
"""
sample = Mock(spec=None)
with pytest.raises(RuntimeError):
Sample._get_reflectivity_profile(sample, 0.005, 0.4, 500, 1,
1e-7, 2)
Sample._get_reflectivity_profile(sample, 0.005, 0.4, 500, 1, 1e-7, 2)


def test_sld_invalid_structure():
"""
Expand All @@ -188,6 +191,7 @@ def test_sld_invalid_structure():
with pytest.raises(RuntimeError):
Sample._get_sld_profile(sample)


def test_vary_structure_invalid_structure():
"""
Test whether a RunTimeError is correctly given when an invalid sample
Expand All @@ -197,10 +201,9 @@ def test_vary_structure_invalid_structure():
with pytest.raises(RuntimeError):
Sample._Sample__vary_structure(structure)

@pytest.mark.parametrize('sample_class', ("refnx_sample",
"refl1d_sample"))
def test_reflectivity_profile_length(sample_class,
request):

@pytest.mark.parametrize('sample_class', ('refnx_sample', 'refl1d_sample'))
def test_reflectivity_profile_length(sample_class, request):
"""
Tests whether _get_reflectivity_profile() succesfully retrieves two arrays
with equal lengths, representing a reflectivity profile that can be
Expand All @@ -209,7 +212,8 @@ def test_reflectivity_profile_length(sample_class,
sample = request.getfixturevalue(sample_class)
q, r = sample._get_reflectivity_profile(0.005, 0.4, 500, 1, 1e-7, 2)
assert len(q) == len(r)
assert len(q) > 0 # Make sure array is not empty
assert len(q) > 0 # Make sure array is not empty


def test_to_refl1d_instance(refnx_sample):
"""
Expand All @@ -219,6 +223,7 @@ def test_to_refl1d_instance(refnx_sample):
refnx_sample.to_refl1d()
assert isinstance(refnx_sample.structure, refl1d.model.Stack)


def test_to_refnx_instance(refl1d_sample):
"""
Tests whether a conversion from refl1d to a refnx structure correctly
Expand All @@ -227,6 +232,7 @@ def test_to_refnx_instance(refl1d_sample):
refl1d_sample.to_refnx()
assert isinstance(refl1d_sample.structure, refnx.reflect.Structure)


def test_to_refl1d_values(refnx_sample):
"""
Tests whether the strucutral parameters are correctly carried over when
Expand All @@ -236,6 +242,7 @@ def test_to_refl1d_values(refnx_sample):
refl1d_sample.to_refl1d()
assert compare_sample_structure(refl1d_sample, refnx_sample)


def test_to_refnx_values(refl1d_sample):
"""
Tests whether the structural parameters are correctly carried over when
Expand All @@ -245,6 +252,7 @@ def test_to_refnx_values(refl1d_sample):
refnx_sample.to_refnx()
assert compare_sample_structure(refl1d_sample, refnx_sample)


def test_simple_sample():
"""
Tests whether simple_sample leads to a valid refnx structure that can be
Expand All @@ -253,6 +261,7 @@ def test_simple_sample():
simple_sample = samples.simple_sample()
simple_sample.to_refl1d()


def test_many_param_sample():
"""
Tests whether many_param_sample leads to a valid refnx structure that
Expand All @@ -261,6 +270,7 @@ def test_many_param_sample():
many_param_sample = samples.many_param_sample()
many_param_sample.to_refl1d()


def test_thin_layer_sample_1():
"""
Tests whether thin_layer_sample_1 leads to a valid refnx structure that
Expand All @@ -269,6 +279,7 @@ def test_thin_layer_sample_1():
thin_layer_sample_1 = samples.thin_layer_sample_1()
thin_layer_sample_1.to_refl1d()


def test_thin_layer_sample_2():
"""
Tests whether thin_layer_sample_2 leads to a valid refnx structure that
Expand All @@ -277,6 +288,7 @@ def test_thin_layer_sample_2():
thin_layer_sample_2 = samples.thin_layer_sample_2()
thin_layer_sample_2.to_refl1d()


def test_similar_sld_sample_1():
"""
Tests whether similar_sld_sample_1 leads to a valid refnx structure that
Expand All @@ -285,6 +297,7 @@ def test_similar_sld_sample_1():
similar_sld_sample_1 = samples.similar_sld_sample_1()
similar_sld_sample_1.to_refl1d()


def test_similar_sld_sample_2():
"""
Tests whether similar_sld_sample_2 leads to a valid refnx structure that
Expand All @@ -293,6 +306,7 @@ def test_similar_sld_sample_2():
similar_sld_sample_2 = samples.similar_sld_sample_2()
similar_sld_sample_2.to_refl1d()


@patch('hogben.models.samples.save_plot', side_effect=mock_save_plot)
def test_main_function(_mock_save_plot):
"""
Expand All @@ -310,4 +324,4 @@ def test_main_function(_mock_save_plot):
'reflectivity_profile.png')
sld_profile = os.path.join(save_path, subfolder, 'sld_profile.png')
assert os.path.isfile(reflectivity_profile)
assert os.path.isfile(sld_profile)
assert os.path.isfile(sld_profile)

0 comments on commit 45e846e

Please sign in to comment.