pyRADDOSE-3D 0.1.0__py3-none-any.whl
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- pyraddose_3d/__init__.py +9 -0
- pyraddose_3d/_java_format.py +157 -0
- pyraddose_3d/_raddose_txt.py +1643 -0
- pyraddose_3d/_version.py +24 -0
- pyraddose_3d/backend/__init__.py +16 -0
- pyraddose_3d/backend/_array.py +266 -0
- pyraddose_3d/backend/beam.py +565 -0
- pyraddose_3d/backend/coefficients.py +258 -0
- pyraddose_3d/backend/composition.py +866 -0
- pyraddose_3d/backend/container.py +241 -0
- pyraddose_3d/backend/decay.py +105 -0
- pyraddose_3d/backend/detector.py +116 -0
- pyraddose_3d/backend/elements.py +487 -0
- pyraddose_3d/backend/escape.py +1264 -0
- pyraddose_3d/backend/exposure.py +633 -0
- pyraddose_3d/backend/geometry.py +434 -0
- pyraddose_3d/backend/java_random.py +161 -0
- pyraddose_3d/backend/microed.py +1277 -0
- pyraddose_3d/backend/montecarlo/__init__.py +6 -0
- pyraddose_3d/backend/montecarlo/angles.py +331 -0
- pyraddose_3d/backend/montecarlo/geometry.py +272 -0
- pyraddose_3d/backend/montecarlo/gos.py +528 -0
- pyraddose_3d/backend/montecarlo/gos_transport.py +1211 -0
- pyraddose_3d/backend/montecarlo/material.py +247 -0
- pyraddose_3d/backend/montecarlo/ops.py +614 -0
- pyraddose_3d/backend/montecarlo/photons.py +531 -0
- pyraddose_3d/backend/montecarlo/simple.py +1427 -0
- pyraddose_3d/backend/montecarlo/tables.py +236 -0
- pyraddose_3d/backend/montecarlo/transport.py +387 -0
- pyraddose_3d/backend/montecarlo/xfel.py +340 -0
- pyraddose_3d/backend/residues.py +209 -0
- pyraddose_3d/backend/structure_files.py +496 -0
- pyraddose_3d/backend/summary.py +278 -0
- pyraddose_3d/backend/voxels.py +183 -0
- pyraddose_3d/backend/wireframe.py +108 -0
- pyraddose_3d/data/absorption_edges.tsv +94 -0
- pyraddose_3d/data/auger_transitions.tsv +1666 -0
- pyraddose_3d/data/best_intensity.tsv +305 -0
- pyraddose_3d/data/cross_section_coefficients.tsv +515 -0
- pyraddose_3d/data/cylinder_topology.tsv +124 -0
- pyraddose_3d/data/edge_ratios.tsv +98 -0
- pyraddose_3d/data/electron_ionisation.tsv +106 -0
- pyraddose_3d/data/elements.tsv +94 -0
- pyraddose_3d/data/elsepa_cross_sections.csv +102 -0
- pyraddose_3d/data/elsepa_elastic.tsv +95 -0
- pyraddose_3d/data/fluorescence.tsv +96 -0
- pyraddose_3d/data/fluorescence_transitions.tsv +371 -0
- pyraddose_3d/data/nist_elements.tsv +4494 -0
- pyraddose_3d/data/nist_mixtures.tsv +1982 -0
- pyraddose_3d/data/overvoltage_high.tsv +688 -0
- pyraddose_3d/data/overvoltage_low.tsv +688 -0
- pyraddose_3d/data/pe_energy_high.csv +53 -0
- pyraddose_3d/data/pe_energy_low.csv +49 -0
- pyraddose_3d/data/pe_energy_medium.csv +53 -0
- pyraddose_3d/data/residues.tsv +212 -0
- pyraddose_3d/data/sphere_mesh.tsv +122 -0
- pyraddose_3d/models/__init__.py +6 -0
- pyraddose_3d/models/_base.py +54 -0
- pyraddose_3d/models/beam.py +196 -0
- pyraddose_3d/models/composition.py +246 -0
- pyraddose_3d/models/crystal.py +399 -0
- pyraddose_3d/models/experiment.py +157 -0
- pyraddose_3d/models/wedge.py +53 -0
- pyraddose_3d/optimise.py +281 -0
- pyraddose_3d/outputs.py +1036 -0
- pyraddose_3d/plotting.py +407 -0
- pyraddose_3d/py.typed +0 -0
- pyraddose_3d/solve.py +1399 -0
- pyraddose_3d-0.1.0.dist-info/METADATA +286 -0
- pyraddose_3d-0.1.0.dist-info/RECORD +73 -0
- pyraddose_3d-0.1.0.dist-info/WHEEL +4 -0
- pyraddose_3d-0.1.0.dist-info/licenses/LICENSE +661 -0
- pyraddose_3d-0.1.0.dist-info/licenses/NOTICE +98 -0
pyraddose_3d/__init__.py
ADDED
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
"""A Python port of RADDOSE-3D, differentiable with PyTorch where installed."""
|
|
2
|
+
|
|
3
|
+
try:
|
|
4
|
+
# Written by hatch-vcs when the package is built or installed.
|
|
5
|
+
from pyraddose_3d._version import __version__
|
|
6
|
+
except ImportError: # a source tree that was never installed
|
|
7
|
+
__version__ = "uninstalled"
|
|
8
|
+
__author__ = "Josh Dickerson"
|
|
9
|
+
__email__ = "jdickerson@berkeley.edu"
|
|
@@ -0,0 +1,157 @@
|
|
|
1
|
+
"""Numbers formatted exactly as RADDOSE-3D's Java prints them.
|
|
2
|
+
|
|
3
|
+
The output files are meant to be the same files, so a script that reads
|
|
4
|
+
RADDOSE-3D's output reads the port's unchanged. Python's ``%`` formatting
|
|
5
|
+
gets the last digit wrong surprisingly often, for two reasons:
|
|
6
|
+
|
|
7
|
+
**Java rounds the shortest decimal, not the binary value.** ``String.format``
|
|
8
|
+
starts from the digits ``Double.toString`` would print and rounds those
|
|
9
|
+
half-up. Python rounds the exact binary value half-even. They part company
|
|
10
|
+
whenever the shortest form ends in a 5, which is common::
|
|
11
|
+
|
|
12
|
+
value Java %.2f Python %.2f
|
|
13
|
+
1.005 1.01 1.00 (1.005 is 1.00499999... in binary)
|
|
14
|
+
0.125 0.13 0.12 (exact, but Python rounds half-even)
|
|
15
|
+
|
|
16
|
+
**``Double.toString`` and ``Float.toString`` have their own layout.**
|
|
17
|
+
Plain decimals between 1e-3 and 1e7, otherwise ``d.dddE±n`` with a capital
|
|
18
|
+
E, no plus sign and no padding: ``1.0E-5``, ``2.6469176E9``. Both always
|
|
19
|
+
show at least one digit after the point.
|
|
20
|
+
|
|
21
|
+
Since JDK 19 the digits are the shortest that round-trip, as Python's
|
|
22
|
+
``repr`` gives, with one refinement: where a single digit would do, Java
|
|
23
|
+
considers two-digit candidates too and takes whichever is closest to the
|
|
24
|
+
exact value. That only changes the answer where the gap between adjacent
|
|
25
|
+
values is enormous -- the smallest subnormal double is ``4.9E-324``, not
|
|
26
|
+
``5.0E-324``.
|
|
27
|
+
|
|
28
|
+
Checked against every pattern RADDOSE-3D uses, for about four hundred
|
|
29
|
+
values, in ``tests/data/java_format.tsv``.
|
|
30
|
+
"""
|
|
31
|
+
|
|
32
|
+
from __future__ import annotations
|
|
33
|
+
|
|
34
|
+
import math
|
|
35
|
+
from decimal import ROUND_HALF_EVEN, ROUND_HALF_UP, Context, Decimal
|
|
36
|
+
|
|
37
|
+
import numpy as np
|
|
38
|
+
|
|
39
|
+
__all__ = ["java_double", "java_e", "java_f", "java_float"]
|
|
40
|
+
|
|
41
|
+
#: Enough digits for any double printed in full, which ``%f`` of 1e308 does.
|
|
42
|
+
_WIDE = Context(prec=800)
|
|
43
|
+
|
|
44
|
+
|
|
45
|
+
def _round_trips(candidate: Decimal, value: float, single: bool) -> bool:
|
|
46
|
+
if single:
|
|
47
|
+
return float(np.float32(float(candidate))) == value
|
|
48
|
+
return float(candidate) == value
|
|
49
|
+
|
|
50
|
+
|
|
51
|
+
def _digits(value: float, single: bool) -> tuple[str, int]:
|
|
52
|
+
"""Java's digits for a finite, non-zero value, and its decimal exponent.
|
|
53
|
+
|
|
54
|
+
Returns ``(digits, exponent)`` with the magnitude equal to
|
|
55
|
+
``d1.d2d3... * 10**exponent``.
|
|
56
|
+
"""
|
|
57
|
+
text = np.format_float_scientific(
|
|
58
|
+
np.float32(value) if single else np.float64(value), unique=True, trim="-"
|
|
59
|
+
)
|
|
60
|
+
mantissa, exponent_text = text.lstrip("-").split("e")
|
|
61
|
+
digits = mantissa.replace(".", "").rstrip("0") or "0"
|
|
62
|
+
exponent = int(exponent_text)
|
|
63
|
+
if len(digits) == 1:
|
|
64
|
+
exact = abs(Decimal(value)) # a float converts exactly
|
|
65
|
+
two = exact.scaleb(-exponent).quantize(Decimal("0.1"), ROUND_HALF_EVEN)
|
|
66
|
+
if (
|
|
67
|
+
two < 10
|
|
68
|
+
and two != int(digits)
|
|
69
|
+
and _round_trips(two.scaleb(exponent), abs(value), single)
|
|
70
|
+
and abs(two.scaleb(exponent) - exact)
|
|
71
|
+
< abs(Decimal(int(digits)).scaleb(exponent) - exact)
|
|
72
|
+
):
|
|
73
|
+
digits = str(two).replace(".", "").rstrip("0")
|
|
74
|
+
return digits, exponent
|
|
75
|
+
|
|
76
|
+
|
|
77
|
+
def _non_finite(value: float) -> str:
|
|
78
|
+
"""How Java spells NaN and the infinities, whatever the pattern."""
|
|
79
|
+
if math.isnan(value):
|
|
80
|
+
return "NaN"
|
|
81
|
+
return "Infinity" if value > 0 else "-Infinity"
|
|
82
|
+
|
|
83
|
+
|
|
84
|
+
def _to_string(value: float, single: bool) -> str:
|
|
85
|
+
if not math.isfinite(value):
|
|
86
|
+
return _non_finite(value)
|
|
87
|
+
if value == 0:
|
|
88
|
+
return "-0.0" if math.copysign(1.0, value) < 0 else "0.0"
|
|
89
|
+
|
|
90
|
+
digits, exponent = _digits(value, single)
|
|
91
|
+
sign = "-" if value < 0 else ""
|
|
92
|
+
if 1e-3 <= abs(value) < 1e7:
|
|
93
|
+
if exponent >= 0:
|
|
94
|
+
whole = digits[: exponent + 1].ljust(exponent + 1, "0")
|
|
95
|
+
fraction = digits[exponent + 1 :] or "0"
|
|
96
|
+
else:
|
|
97
|
+
whole = "0"
|
|
98
|
+
fraction = "0" * (-exponent - 1) + digits
|
|
99
|
+
return f"{sign}{whole}.{fraction}"
|
|
100
|
+
fraction = digits[1:] or "0"
|
|
101
|
+
return f"{sign}{digits[0]}.{fraction}E{exponent}"
|
|
102
|
+
|
|
103
|
+
|
|
104
|
+
def java_double(value: float) -> str:
|
|
105
|
+
"""``Double.toString``: what ``"" + someDouble`` prints in Java."""
|
|
106
|
+
return _to_string(float(value), single=False)
|
|
107
|
+
|
|
108
|
+
|
|
109
|
+
def java_float(value: float) -> str:
|
|
110
|
+
"""``Float.toString``: what ``"" + (float) someDouble`` prints in Java.
|
|
111
|
+
|
|
112
|
+
The cast comes first, so a value that rounds to a power of ten, to zero
|
|
113
|
+
or to infinity in single precision is laid out as that.
|
|
114
|
+
"""
|
|
115
|
+
with np.errstate(over="ignore"):
|
|
116
|
+
return _to_string(float(np.float32(value)), single=True)
|
|
117
|
+
|
|
118
|
+
|
|
119
|
+
def _decimal(value: float) -> Decimal:
|
|
120
|
+
"""The decimal Java's Formatter rounds: ``Double.toString``'s digits."""
|
|
121
|
+
if value == 0:
|
|
122
|
+
return Decimal(0)
|
|
123
|
+
digits, exponent = _digits(value, single=False)
|
|
124
|
+
sign = "-" if value < 0 else ""
|
|
125
|
+
return Decimal(f"{sign}{digits[0]}.{digits[1:] or '0'}E{exponent}")
|
|
126
|
+
|
|
127
|
+
|
|
128
|
+
def java_f(value: float, places: int = 6) -> str:
|
|
129
|
+
"""``String.format("%.Nf", value)``, rounding half-up as Java does."""
|
|
130
|
+
value = float(value)
|
|
131
|
+
if not math.isfinite(value):
|
|
132
|
+
return _non_finite(value)
|
|
133
|
+
quantum = Decimal(1).scaleb(-places)
|
|
134
|
+
rounded = _decimal(value).quantize(quantum, ROUND_HALF_UP, context=_WIDE)
|
|
135
|
+
text = f"{rounded.copy_abs():f}"
|
|
136
|
+
return "-" + text if math.copysign(1.0, value) < 0 else text
|
|
137
|
+
|
|
138
|
+
|
|
139
|
+
def java_e(value: float, places: int = 6) -> str:
|
|
140
|
+
"""``String.format("%.Ne", value)``: ``d.ddde±XX``, rounding half-up."""
|
|
141
|
+
value = float(value)
|
|
142
|
+
if not math.isfinite(value):
|
|
143
|
+
return _non_finite(value)
|
|
144
|
+
sign = "-" if math.copysign(1.0, value) < 0 else ""
|
|
145
|
+
if value == 0:
|
|
146
|
+
return f"{sign}{0:.{places}f}e+00"
|
|
147
|
+
number = _decimal(value).copy_abs()
|
|
148
|
+
exponent = number.adjusted()
|
|
149
|
+
quantum = Decimal(1).scaleb(-places)
|
|
150
|
+
mantissa = number.scaleb(-exponent).quantize(quantum, ROUND_HALF_UP, context=_WIDE)
|
|
151
|
+
if mantissa >= 10:
|
|
152
|
+
exponent += 1
|
|
153
|
+
mantissa = number.scaleb(-exponent).quantize(
|
|
154
|
+
quantum, ROUND_HALF_UP, context=_WIDE
|
|
155
|
+
)
|
|
156
|
+
exponent_sign = "-" if exponent < 0 else "+"
|
|
157
|
+
return f"{sign}{mantissa:f}e{exponent_sign}{abs(exponent):02d}"
|