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

Modified files to pass mypy checks, adding classes/functions to serialise objects. #43

Draft
wants to merge 3 commits into
base: main
Choose a base branch
from
Draft
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
5 changes: 5 additions & 0 deletions .azure-pipelines/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,11 @@ steps:
displayName: Run tests
workingDirectory: $(Pipeline.Workspace)/src

- script: |
PYTHONDEVMODE=1 mypy src tests
displayName: Mypy
workingDirectory: $(Pipeline.Workspace)

- bash: bash <(curl -s https://codecov.io/bash) -n "Python $(PYTHON_VERSION) $(Agent.OS)"
env:
CODECOV_TOKEN: $(CODECOV_TOKEN)
Expand Down
21 changes: 21 additions & 0 deletions .devcontainer/Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
# See here for image contents: https://github.com/microsoft/vscode-dev-containers/tree/v0.241.1/containers/python-3/.devcontainer/base.Dockerfile

# [Choice] Python version (use -bullseye variants on local arm64/Apple Silicon): 3, 3.10, 3.9, 3.8, 3.7, 3.6, 3-bullseye, 3.10-bullseye, 3.9-bullseye, 3.8-bullseye, 3.7-bullseye, 3.6-bullseye, 3-buster, 3.10-buster, 3.9-buster, 3.8-buster, 3.7-buster, 3.6-buster
ARG VARIANT="3.10-bullseye"
FROM mcr.microsoft.com/vscode/devcontainers/python:0-${VARIANT}

# [Choice] Node.js version: none, lts/*, 16, 14, 12, 10
ARG NODE_VERSION="none"
RUN if [ "${NODE_VERSION}" != "none" ]; then su vscode -c "umask 0002 && . /usr/local/share/nvm/nvm.sh && nvm install ${NODE_VERSION} 2>&1"; fi

# [Optional] If your pip requirements rarely change, uncomment this section to add them to the image.
# COPY requirements.txt /tmp/pip-tmp/
# RUN pip3 --disable-pip-version-check --no-cache-dir install -r /tmp/pip-tmp/requirements.txt \
# && rm -rf /tmp/pip-tmp

# [Optional] Uncomment this section to install additional OS packages.
# RUN apt-get update && export DEBIAN_FRONTEND=noninteractive \
# && apt-get -y install --no-install-recommends <your-package-list-here>

# [Optional] Uncomment this line to install global node packages.
# RUN su vscode -c "source /usr/local/share/nvm/nvm.sh && npm install -g <your-package-here>" 2>&1
54 changes: 54 additions & 0 deletions .devcontainer/devcontainer.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
// For format details, see https://aka.ms/devcontainer.json. For config options, see the README at:
// https://github.com/microsoft/vscode-dev-containers/tree/v0.241.1/containers/python-3
{
"name": "Python 3",
"build": {
"dockerfile": "Dockerfile",
"context": "..",
"args": {
// Update 'VARIANT' to pick a Python version: 3, 3.10, 3.9, 3.8, 3.7, 3.6
// Append -bullseye or -buster to pin to an OS version.
// Use -bullseye variants on local on arm64/Apple Silicon.
"VARIANT": "3.8",
// Options
"NODE_VERSION": "none"
}
},

// Configure tool-specific properties.
"customizations": {
// Configure properties specific to VS Code.
"vscode": {
// Set *default* container specific settings.json values on container create.
"settings": {
"python.defaultInterpreterPath": "/usr/local/bin/python",
"python.linting.enabled": true,
"python.linting.pylintEnabled": true,
"python.formatting.autopep8Path": "/usr/local/py-utils/bin/autopep8",
"python.formatting.blackPath": "/usr/local/py-utils/bin/black",
"python.formatting.yapfPath": "/usr/local/py-utils/bin/yapf",
"python.linting.banditPath": "/usr/local/py-utils/bin/bandit",
"python.linting.flake8Path": "/usr/local/py-utils/bin/flake8",
"python.linting.mypyPath": "/usr/local/py-utils/bin/mypy",
"python.linting.pycodestylePath": "/usr/local/py-utils/bin/pycodestyle",
"python.linting.pydocstylePath": "/usr/local/py-utils/bin/pydocstyle",
"python.linting.pylintPath": "/usr/local/py-utils/bin/pylint"
},

// Add the IDs of extensions you want installed when the container is created.
"extensions": [
"ms-python.python",
"ms-python.vscode-pylance"
]
}
},

// Use 'forwardPorts' to make a list of ports inside the container available locally.
// "forwardPorts": [],

// Use 'postCreateCommand' to run commands after the container is created.
// "postCreateCommand": "pip3 install --user -r requirements.txt",

// Comment out to connect as root instead. More info: https://aka.ms/vscode-remote/containers/non-root.
// "remoteUser": "vscode"
}
29 changes: 22 additions & 7 deletions src/diffcalc/hkl/calc.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,10 @@
from copy import copy
from itertools import product
from math import acos, asin, atan, atan2, cos, degrees, isnan, pi, sin, sqrt, tan
from typing import Dict, Iterator, List, Optional, Tuple
from typing import Any, Dict, Iterator, List, Optional, Tuple

import numpy as np
from diffcalc.hkl.constraints import Constraints
from diffcalc.hkl.geometry import (
Position,
get_rotation_matrices,
Expand All @@ -18,6 +19,7 @@
rot_PHI,
)
from diffcalc.log import logging
from diffcalc.ub.calc import UBCalculation
from diffcalc.util import (
SMALL,
DiffcalcException,
Expand Down Expand Up @@ -508,8 +510,8 @@ def _calc_psi(
else:
sin_psi = cos(alpha) * sin(qaz - naz)
sgn = sign(sin_tau)
eps = sin_psi ** 2 + cos_psi ** 2
sigma_ = eps / sin_tau ** 2 - 1
eps = sin_psi**2 + cos_psi**2
sigma_ = eps / sin_tau**2 - 1
if not is_small(sigma_):
print(
"WARNING: Diffcalc could not calculate a unique azimuth "
Expand Down Expand Up @@ -937,7 +939,7 @@ def __get_last_sample_angle(A: float, B: float, C: float) -> List[float]:
"Sample orientation cannot be chosen uniquely. Please choose a different set of constraints."
)
ks = atan2(A, B)
acos_alp = acos(bound(C / sqrt(A ** 2 + B ** 2)))
acos_alp = acos(bound(C / sqrt(A**2 + B**2)))
if is_small(acos_alp):
alp_list = [
ks,
Expand Down Expand Up @@ -1513,7 +1515,7 @@ def _calc_sample_angles_given_two_sample_and_detector(
acos_phi = acos(
bound(
(N_phi[2, 0] * cos(chi) - V20)
/ (sin(chi) * sqrt(A ** 2 + B ** 2))
/ (sin(chi) * sqrt(A**2 + B**2))
)
)
except AssertionError:
Expand Down Expand Up @@ -1562,7 +1564,7 @@ def _calc_sample_angles_given_two_sample_and_detector(
try:
acos_rhs = acos(
bound(
(sin(qaz) * cos(theta) / cos(eta) - V) / sqrt(X ** 2 + Y ** 2)
(sin(qaz) * cos(theta) / cos(eta) - V) / sqrt(X**2 + Y**2)
)
)
except AssertionError:
Expand Down Expand Up @@ -1605,7 +1607,7 @@ def _calc_sample_angles_given_two_sample_and_detector(
acos_V00 = acos(
bound(
(cos(theta) * sin(qaz) - N_phi[2, 0] * cos(eta) * sin(chi))
/ sqrt(A ** 2 + B ** 2)
/ sqrt(A**2 + B**2)
)
)
except AssertionError:
Expand Down Expand Up @@ -1785,3 +1787,16 @@ def _verify_virtual_angles(
"anglesToVirtualAngles of %f" % virtual_angles_readback[key]
)
raise DiffcalcException(s)

@property
def asdict(self) -> Dict[str, Any]:
return {"ubcalc": self.ubcalc.asdict, "constraints": self.constraints.asdict}

@classmethod
def fromdict(cls, data: Dict[str, Any]) -> "HklCalculation":
constraint_data = data["constraints"]
indegrees = constraint_data.pop("indegrees")
return HklCalculation(
UBCalculation.fromdict(data["ubcalc"]),
Constraints(constraint_data, indegrees),
)
7 changes: 6 additions & 1 deletion src/diffcalc/hkl/constraints.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ class _Constraint:

@property
def active(self) -> bool:
# in the below statement, how can self.value ever be False?
return self.value is not False and self.value is not None


Expand Down Expand Up @@ -184,7 +185,11 @@ def asdict(self) -> Dict[str, Union[float, bool]]:
Dict[str, Union[float, bool]]
Dictionary with all constrained angle names and values.
"""
return {con.name: getattr(self, con.name) for con in self._all if con.active}
con_dict = {
con.name: getattr(self, con.name) for con in self._all if con.active
}
con_dict["indegrees"] = self.indegrees
return con_dict

@asdict.setter
def asdict(self, constraints):
Expand Down
Loading