Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use a linter in workflow to enforce PEP8 compliance #72

Merged
merged 11 commits into from
Sep 5, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .circleci/config.yml
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ jobs:
name: Run tests
# This assumes pytest is installed via the install-package step above
command: pytest

# Invoke jobs via workflows
# See: https://circleci.com/docs/2.0/configuration-reference/#workflows
workflows:
Expand Down
58 changes: 33 additions & 25 deletions .github/workflows/automated-testing.yml
Original file line number Diff line number Diff line change
@@ -1,33 +1,41 @@
# This workflow will install Python dependencies and run tests with a single version of Python
# For more information see: https://help.github.com/actions/language-and-framework-guides/using-python-with-github-actions

name: Python application

on:
push:
branches: [ "main" ]
branches:
- main
pull_request:
branches: [ "main" ]

permissions:
contents: read

branches:
- main
name: CI
jobs:
build:

runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v3
- name: Set up Python 3.10
uses: actions/setup-python@v3
with:
python-version: "3.10"
- name: Install dependencies
run: |
python -m pip install --upgrade pip
pip install pytest
python -m pip install .
- name: Test with pytest
run: |
python -m pytest

flake8-lint:
name: flake8
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v3
- name: Set up Python 3.10
uses: actions/setup-python@v3
with:
python-version: "3.10"
- name: Install dependencies
run: |
python -m pip install --upgrade pip
pip install flake8 pytest
python -m pip install .
- name: Test with pytest
run: |
python -m pytest
- uses: actions/checkout@v3
- name: Set up Python environment
uses: actions/setup-python@v4
with:
python-version: "3.10"
# Install flake8 extensions (this step is not required. Default is "None").
- name: Set up flake8
run: pip install flake8-quotes
- name: flake8 Lint
run: |
flake8 hogben
2 changes: 1 addition & 1 deletion hogben/__init__.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
"""Top-level package for HOGBEN"""

__version__ = "1.2.1"
__version__ = '1.2.1'
42 changes: 19 additions & 23 deletions hogben/angles.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import os
import sys
import time

import numpy as np
Expand All @@ -16,18 +15,14 @@ def _angle_results_visualise(save_path):
save_path (str): path to directory to save results to.

"""
from models.samples import similar_sld_sample_1, similar_sld_sample_2
from models.samples import thin_layer_sample_1, thin_layer_sample_2
from models.samples import simple_sample, many_param_sample
from models.bilayers import BilayerDMPC, BilayerDPPC
from models.magnetic import SampleYIG
from models.samples import simple_sample

# Choose sample here.
sample = simple_sample()

# Choose contrasts here (only bilayers should use this).
contrasts = []
#contrasts = [-0.56, 6.36]
# contrasts = [-0.56, 6.36]

# Number of points and counting time for the initial angle choice.
points = 100
Expand All @@ -37,43 +32,38 @@ def _angle_results_visualise(save_path):
angle_range = np.linspace(0.2, 4, 500)
initial_angle = angle_choice(sample, [], angle_range, points, time,
save_path, 'initial', contrasts)

# Plot how the choice of angle changes with initial angle counting time.
angle_range = np.linspace(0.2, 4, 50)
time_range = np.linspace(0, time*8, 50)
time_range = np.linspace(0, time * 8, 50)
angle_choice_with_time(sample, initial_angle, angle_range, time_range,
points, time, save_path, contrasts)


def _angle_results_optimise(save_path):
"""Optimises the choice measurement angles and counting times for a sample.

Args:
save_path (str): path to directory to save results to.

"""
from models.samples import similar_sld_sample_1, similar_sld_sample_2
from models.samples import thin_layer_sample_1, thin_layer_sample_2
from models.samples import simple_sample, many_param_sample
from models.bilayers import BilayerDMPC, BilayerDPPC
from models.magnetic import SampleYIG

from models.samples import simple_sample
# Choose sample here.
sample = simple_sample()

# Choose contrasts here (only bilayers should use this).
contrasts = []
#contrasts = [-0.56, 6.36]
# contrasts = [-0.56, 6.36]

# Total time budget.
total_time = 1000 # A large time improves DE convergence.
total_time = 1000 # A large time improves DE convergence.

# Interval containing angles to consider.
angle_bounds = (0.2, 4.0)

# Create a new .txt file for the results.
save_path = os.path.join(save_path, sample.name)
with open(os.path.join(save_path, 'optimised_angles.txt'), 'w') as file:
optimiser = Optimiser(sample) # Optimiser for the experiment.
optimiser = Optimiser(sample) # Optimiser for the experiment.

# Optimise the experiment using 1-4 angles.
for i, num_angles in enumerate([1, 2, 3, 4]):
Expand All @@ -89,18 +79,24 @@ def _angle_results_optimise(save_path):

# Convert to percentages.
angles, splits, val = results
splits = np.array(splits)*100
splits = np.array(splits) * 100

# Round the optimisation function value to 4 significant figures.
val = np.format_float_positional(val, precision=4, unique=False,
fractional=False, trim='k')
val = np.format_float_positional(
val, precision=4, unique=False, fractional=False, trim='k'
)

# Save the conditions, objective value and computation time.
file.write('----------- {} Angles -----------\n'.format(num_angles))
file.write(
'----------- {} Angles -----------\n'.format(num_angles)
)
file.write('Angles: {}\n'.format(list(np.round(angles, 2))))
file.write('Splits (%): {}\n'.format(list(np.round(splits, 1))))
file.write('Objective value: {}\n'.format(val))
file.write('Computation time: {}\n\n'.format(round(end-start, 1)))
file.write(
'Computation time: {}\n\n'.format(round(end - start, 1))
)


if __name__ == '__main__':
save_path = './results'
Expand Down
67 changes: 39 additions & 28 deletions hogben/contrasts.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import os
import sys
import time

import matplotlib.pyplot as plt
Expand All @@ -17,20 +16,25 @@ def _contrast_results_visualise(save_path):
save_path (str): path to directory to save results to.

"""
from models.bilayers import BilayerDMPC, BilayerDPPC
from models.bilayers import BilayerDMPC

# Choose sample here.
bilayer = BilayerDMPC()

# Number of points and counting times for each angle to simulate.
angle_times = [(0.7, 100, 10), (2.3, 100, 40)]

# Visualise single contrast choices assuming different initial measurements.
# Visualise single contrast choices assuming different initial
# measurements.
contrast_range = np.linspace(-0.56, 6.36, 500)
contrast_choice_single(bilayer, contrast_range, [], angle_times, save_path, 'initial')
contrast_choice_single(bilayer, contrast_range, [6.36], angle_times, save_path, 'D2O')
contrast_choice_single(bilayer, contrast_range, [-0.56], angle_times, save_path, 'H2O')
contrast_choice_single(bilayer, contrast_range, [-0.56, 6.36], angle_times, save_path, 'D2O_H2O')
contrast_choice_single(bilayer, contrast_range, [], angle_times,
save_path, 'initial')
contrast_choice_single(bilayer, contrast_range, [6.36], angle_times,
save_path, 'D2O')
contrast_choice_single(bilayer, contrast_range, [-0.56], angle_times,
save_path, 'H2O')
contrast_choice_single(bilayer, contrast_range, [-0.56, 6.36], angle_times,
save_path, 'D2O_H2O')

# Investigate contrast pair choices assuming no prior measurement.
contrast_range = np.linspace(-0.56, 6.36, 75)
Expand All @@ -40,20 +44,21 @@ def _contrast_results_visualise(save_path):
bilayer.nested_sampling([6.36, 6.36], angle_times, save_path, 'D2O_D2O')
bilayer.nested_sampling([-0.56, 6.36], angle_times, save_path, 'H2O_D2O')


def _contrast_results_optimise(save_path):
"""Optimises the choice of contrasts for a bilayer sample.

Args:
save_path (str): path to directory to save results to.

"""
from bilayers import BilayerDMPC, BilayerDPPC
from bilayers import BilayerDMPC

# Choose sample here.
bilayer = BilayerDMPC()

# Time budget for experiment.
total_time = 1000 # A large time improves DE convergence.
total_time = 1000 # A large time improves DE convergence.

# Points and proportion of total counting time for each angle.
angle_splits = [(0.7, 100, 0.2), (2.3, 100, 0.8)]
Expand All @@ -64,7 +69,7 @@ def _contrast_results_optimise(save_path):
# Create a new .txt file for the results.
save_path = os.path.join(save_path, bilayer.name)
with open(os.path.join(save_path, 'optimised_contrasts.txt'), 'w') as file:
optimiser = Optimiser(bilayer) # Optimiser for the experiment.
optimiser = Optimiser(bilayer) # Optimiser for the experiment.

# Optimise the experiment using 1-4 contrasts.
for i, num_contrasts in enumerate([1, 2, 3, 4]):
Expand All @@ -79,18 +84,23 @@ def _contrast_results_optimise(save_path):
end = time.time()

contrasts, splits, val = results
splits = np.array(splits)*100 # Convert to percentages.
splits = np.array(splits) * 100 # Convert to percentages.

# Round the optimisation function value to 4 significant figures.
val = np.format_float_positional(val, precision=4, unique=False,
fractional=False, trim='k')

# Write the conditions, objective value and computation time.
file.write('----------- {} Contrasts -----------\n'.format(num_contrasts))
file.write(
'----------- {} Contrasts -----------\n'.format(num_contrasts))
file.write('Contrasts: {}\n'.format(list(np.round(contrasts, 2))))
file.write('Splits (%): {}\n'.format(list(np.round(splits, 1))))
file.write('Objective value: {}\n'.format(val))
file.write('Computation time: {}\n\n'.format(round(end-start, 1)))
file.write(
'Computation time: {}\n\n'.format(
round(
end - start, 1)))


def _figure_2(save_path):
"""Creates figure 2 for the paper. The figure shows the minimum eigenvalue
Expand All @@ -117,36 +127,36 @@ def _figure_2(save_path):
h2o_split_2 = 0.245

# Define how much the third contrast will be measured for.
nxt_split_1 = 1-d2o_split_1-h2o_split_1
nxt_split_2 = 1-d2o_split_2-h2o_split_2
nxt_split_1 = 1 - d2o_split_1 - h2o_split_1
nxt_split_2 = 1 - d2o_split_2 - h2o_split_2

# Counting times for each angle when measuring D2O.
d2o_angle_times_1 = [(angle, points, total_time*split*d2o_split_1)
d2o_angle_times_1 = [(angle, points, total_time * split * d2o_split_1)
for angle, points, split in angle_splits]

d2o_angle_times_2 = [(angle, points, total_time*split*d2o_split_2)
d2o_angle_times_2 = [(angle, points, total_time * split * d2o_split_2)
for angle, points, split in angle_splits]

# Counting times for each angle when measuring H2O.
h2o_angle_times_1 = [(angle, points, total_time*split*h2o_split_1)
h2o_angle_times_1 = [(angle, points, total_time * split * h2o_split_1)
for angle, points, split in angle_splits]

h2o_angle_times_2 = [(angle, points, total_time*split*h2o_split_2)
h2o_angle_times_2 = [(angle, points, total_time * split * h2o_split_2)
for angle, points, split in angle_splits]

# Counting times for each angle when measuring the third contrast.
nxt_angle_times_1 = [(angle, points, total_time*split*(nxt_split_1))
nxt_angle_times_1 = [(angle, points, total_time * split * (nxt_split_1))
for angle, points, split in angle_splits]

nxt_angle_times_2 = [(angle, points, total_time*split*(nxt_split_2))
nxt_angle_times_2 = [(angle, points, total_time * split * (nxt_split_2))
for angle, points, split in angle_splits]

# Information from the D2O and H2O contrasts for each model.
g_init_1 = (sample_1.angle_info(d2o_angle_times_1, [6.36]) +
sample_1.angle_info(h2o_angle_times_1, [-0.56]))
g_init_1 = (sample_1.angle_info(d2o_angle_times_1, [6.36])
+ sample_1.angle_info(h2o_angle_times_1, [-0.56]))

g_init_2 = (sample_2.angle_info(d2o_angle_times_2, [6.36]) +
sample_2.angle_info(h2o_angle_times_2, [-0.56]))
g_init_2 = (sample_2.angle_info(d2o_angle_times_2, [6.36])
+ sample_2.angle_info(h2o_angle_times_2, [-0.56]))

# Calculate the minimum eigenvalue for each choice of third contrast.
min_eigs_1, min_eigs_2 = [], []
Expand All @@ -156,8 +166,8 @@ def _figure_2(save_path):
g_new_1 = sample_1.contrast_info(nxt_angle_times_1, [new_contrast])
g_new_2 = sample_2.contrast_info(nxt_angle_times_2, [new_contrast])

min_eigs_1.append(np.linalg.eigvalsh(g_init_1+g_new_1)[0])
min_eigs_2.append(np.linalg.eigvalsh(g_init_2+g_new_2)[0])
min_eigs_1.append(np.linalg.eigvalsh(g_init_1 + g_new_1)[0])
min_eigs_2.append(np.linalg.eigvalsh(g_init_2 + g_new_2)[0])

# Create the plot of third contrast versus minimum eigenvalue.
fig = plt.figure()
Expand All @@ -174,11 +184,12 @@ def _figure_2(save_path):
ax1.set_xlabel(x_label, fontsize=11, weight='bold')
ax1.set_ylabel(y_label, fontsize=11, weight='bold', color='b')
ax2.set_ylabel(y_label, fontsize=11, weight='bold', color='g')
ax1.legend(line_1+line_2, ['DMPC', 'DPPC/RaLPS'], loc=0)
ax1.legend(line_1 + line_2, ['DMPC', 'DPPC/RaLPS'], loc=0)

# Save the plot.
save_plot(fig, save_path, 'figure_2')


if __name__ == '__main__':
save_path = './results'
_contrast_results_visualise(save_path)
Expand Down
Loading