Skip to content

Commit

Permalink
Merge pull request #14 from fusion-energy/adding_flake8_linting
Browse files Browse the repository at this point in the history
added auto lint comments to pushes
  • Loading branch information
shimwell authored Jul 29, 2022
2 parents 6930adf + 6896bc9 commit 9b75d82
Show file tree
Hide file tree
Showing 9 changed files with 101 additions and 140 deletions.
95 changes: 49 additions & 46 deletions .github/workflows/ci_with_install.yml
Original file line number Diff line number Diff line change
@@ -1,47 +1,50 @@

# TODO this needs completing, the docker image would currently need a couple of
# versions of openmc as the current 0.13-dev doesn't create an initial_source.h5
# while the older versions don't write the required xml tag.
# <write_initial_source>true</write_initial_source>
# note to self come back to this once the current version of openmc writes out
# the initial_source.h5



# # This CI will launch a Docker image that contains all the dependencies required
# # within that image the pytest test suite is run

# name: CI with install

# on:
# pull_request:
# branches:
# - develop
# - main
# paths-ignore:
# - 'docs/**'
# - '.gitignore'
# - '*.md'
# - 'CITATION.cff'
# - 'LICENSE.txt'
# - 'readthedocs.yml'

# jobs:
# testing:
# runs-on: ubuntu-latest
# container:
# image: ghcr.io/fusion-energy/paramak:dependencies
# steps:
# - name: Checkout repository
# uses: actions/checkout@v2

# - name: install package
# run: |
# python setup.py install

# - name: Run test_utils
# run: |
# pytest tests/test_utils.py -v --cov=openmc_source_plotter --cov-append --cov-report term --cov-report xml

# - name: Upload to codecov
# uses: codecov/codecov-action@v2
# This CI will launch a Docker image that contains all the dependencies required
# within that image the pytest test suite is run

name: CI with install

on:
pull_request:
branches:
- develop
- main
paths-ignore:
- 'docs/**'
- '.gitignore'
- '*.md'
- 'CITATION.cff'
- 'LICENSE.txt'
- 'readthedocs.yml'

jobs:
testing:
runs-on: ubuntu-latest
container:
image: openmc/openmc:develop
steps:
- name: Checkout repository
uses: actions/checkout@v2

- name: install package
run: |
pip install .
python -c "import openmc_source_plotter"
- name: Run examples
run: |
python examples/example_get_particle_data.py
python examples/example_plot_source_direction.py
python examples/example_plot_source_energy.py
python examples/example_plot_source_position.py
python examples/example_plot_two_source_energies.py
pip install openmc_plasma_source
python examples/example_plot_plasma_source_position.py
- name: Run test_utils
run: |
pip install .[tests]
pytest tests -v --cov=openmc_source_plotter --cov-append --cov-report term --cov-report xml
- name: Upload to codecov
uses: codecov/codecov-action@v2
27 changes: 27 additions & 0 deletions .github/workflows/lint.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
name: Python Flake8 Lint

on:
# Trigger the workflow pull requests to any branch
pull_request:

jobs:
run-linters:
name: lint
runs-on: ubuntu-latest

steps:
- name: Check out Git repository
uses: actions/checkout@v3

- name: Set up Python
uses: actions/setup-python@v4
with:
python-version: "3.10"

- name: Install Python dependencies
run: pip install flake8

- name: Run linters
uses: wearerequired/lint-action@v2
with:
flake8: true
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -131,3 +131,6 @@ dmypy.json
# openmc files
*.h5
*.xml

# version file
openmc_source_plotter/_version.py
5 changes: 0 additions & 5 deletions openmc_source_plotter/_version.py

This file was deleted.

25 changes: 14 additions & 11 deletions openmc_source_plotter/core.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,9 @@
import plotly.graph_objects as go


def sample_initial_particles(source: openmc.source, n_samples=1000, prn_seed=None):
def sample_initial_particles(
source: openmc.source, n_samples: int = 1000, prn_seed: int = None
):

settings = openmc.Settings()
settings.particles = 1
Expand All @@ -28,10 +30,6 @@ def sample_initial_particles(source: openmc.source, n_samples=1000, prn_seed=Non

geometry.export_to_xml()

# # model = openmc.Model()
# model.geometry = geometry
# model.materials = geometry

openmc.lib.init()
particles = openmc.lib.sample_external_source(
n_samples=n_samples, prn_seed=prn_seed
Expand All @@ -44,13 +42,17 @@ def plot_source_energy(
source: Union[openmc.Source, List[openmc.Source]],
n_samples: int = 2000,
prn_seed: int = 1,
energy_bins: Union[str, np.array] = "auto",
):
"""makes a plot of the initial creation postions of an OpenMC source(s)
Args:
source: The openmc.Source object or list of openmc.Source objects to plot.
n_smaples: The number of source samples to obtain.
n_samples: The number of source samples to obtain.
prn_seed: The pseudorandom number seed
energy_bins: Defaults to 'auto' which uses inbuilt auto binning in Numpy
bins can also be manually set by passing in a numpy array of bin
edges.
"""

figure = go.Figure()
Expand All @@ -60,10 +62,12 @@ def plot_source_energy(

for single_source in source:

e_values = single_source.energy.sample(n_samples=n_samples, prn_seed=prn_seed)
data = sample_initial_particles(single_source, n_samples, prn_seed)

e_values = [particle.E for particle in data]

# Calculate pdf for source energies
probability, bin_edges = np.histogram(e_values, bins="auto", density=True)
probability, bin_edges = np.histogram(e_values, bins=energy_bins, density=True)

# Plot source energy histogram
figure.add_trace(
Expand Down Expand Up @@ -94,7 +98,7 @@ def plot_source_position(
Args:
source: The openmc.Source object or list of openmc.Source objects to plot.
n_smaples: The number of source samples to obtain.
n_samples: The number of source samples to obtain.
prn_seed: The pseudorandom number seed
"""

Expand All @@ -106,7 +110,6 @@ def plot_source_position(
for single_source in source:

data = sample_initial_particles(single_source, n_samples, prn_seed)
print([p.r for p in data])

text = ["Energy = " + str(particle.E) + " eV" for particle in data]

Expand Down Expand Up @@ -139,7 +142,7 @@ def plot_source_direction(
Args:
source: The openmc.Source object or list of openmc.Source objects to plot.
n_smaples: The number of source samples to obtain.
n_samples: The number of source samples to obtain.
prn_seed: The pseudorandom number seed
"""
figure = go.Figure()
Expand Down
Empty file removed openmc_source_plotter/utils.py
Empty file.
4 changes: 4 additions & 0 deletions setup.cfg
Original file line number Diff line number Diff line change
Expand Up @@ -32,5 +32,9 @@ install_requires=
h5py
# openmc is also required but can't yet be installed by pip

[options.extras_require]
tests =
pytest >= 5.4.3

[flake8]
per-file-ignores = __init__.py:F401
18 changes: 4 additions & 14 deletions tests/test_core.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,13 +10,6 @@
class TestUtils(unittest.TestCase):
def setUp(self):

self.openmc_exec_dict = {
"ci": "/opt/openmc/build/bin/openmc",
"laptop": "/home/jshim/miniconda3/envs/openmc_0_11_0/bin/openmc",
"desktop": "/home/jshimwell/miniconda3/envs/openmc_0_11_0/bin/openmc",
}
self.current_computer = "laptop"

# initialises a new source object
my_source = openmc.Source()

Expand All @@ -32,17 +25,16 @@ def setUp(self):
self.my_source = my_source

# makes an initial_source.h5 file with details of the particles
self.initial_source_filename = osp.create_initial_particles(
self.initial_source_filename = osp.sample_initial_particles(
source=my_source,
number_of_particles=10,
openmc_exec=self.openmc_exec_dict[self.current_computer],
n_samples=10,
)

def test_energy_plot(self):

plot = osp.plot_source_energy(
source=self.my_source,
number_of_particles=10000,
n_samples=10000,
energy_bins=np.linspace(0, 20e6, 100),
)
assert isinstance(plot, go.Figure)
Expand All @@ -51,14 +43,12 @@ def test_position_plot(self):

plot = osp.plot_source_position(
source=self.my_source,
openmc_exec=self.openmc_exec_dict[self.current_computer],
)
assert isinstance(plot, go.Figure)

def test_direction_plot(self):
plot = osp.plot_source_direction(
source=self.my_source,
number_of_particles=100,
openmc_exec=self.openmc_exec_dict["laptop"],
n_samples=100,
)
assert isinstance(plot, go.Figure)
64 changes: 0 additions & 64 deletions tests/test_utils.py

This file was deleted.

0 comments on commit 9b75d82

Please sign in to comment.