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

Refactoring and Multiprocessing #21

Open
wants to merge 104 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
104 commits
Select commit Hold shift + click to select a range
3c0fa9c
Add halo_model param description to docstring
lorenzomag Jul 14, 2024
b0fe5f2
Add basic file
lorenzomag Jul 22, 2024
a40921d
Moved Ibe's model
lorenzomag Jul 22, 2024
9ce21df
Added Cox model package at tag
lorenzomag Jul 22, 2024
b851471
Moving submodule
lorenzomag Aug 6, 2024
328478a
Updating import statements
lorenzomag Aug 6, 2024
5ad10fc
Add utility function to preformat data for Cox's interpolators
lorenzomag Aug 6, 2024
ef27b65
Wrapper module to include Cox's package
lorenzomag Aug 6, 2024
28ba5d8
Add docstring to cox_wrapper.py
lorenzomag Aug 6, 2024
6fc1f86
Implemented Peter Cox et al. Migdal model
lorenzomag Aug 6, 2024
e4a5802
Remove debug print statement
lorenzomag Aug 6, 2024
502fa08
Add dockstrings
lorenzomag Aug 6, 2024
6128b40
Update tutorial notebook for Migdal
lorenzomag Aug 6, 2024
8bde989
Remove typing.Self for python3.9
lorenzomag Aug 6, 2024
e0d51d4
Require seaborn
lorenzomag Aug 6, 2024
f8e8684
Add output to notebook
lorenzomag Aug 6, 2024
9e6db2f
Revert "Remove typing.Self for python3.9"
lorenzomag Aug 6, 2024
5b608a5
Revert "Require seaborn"
lorenzomag Aug 6, 2024
e6d05cc
Require Python3.11
lorenzomag Aug 6, 2024
ea47279
Require importlib package for Cox model
lorenzomag Aug 6, 2024
1eed196
Add __init__.py in tests to fix module structure
lorenzomag Aug 6, 2024
853c4f0
Add all content of data/ for build
lorenzomag Aug 6, 2024
c95fdbe
Add deprecation warning for old func
lorenzomag Aug 6, 2024
986937d
Fix typing
lorenzomag Aug 7, 2024
176bf94
Reduce required python3 version to 3.9
lorenzomag Aug 7, 2024
fd9c56e
Update notebook output
lorenzomag Aug 7, 2024
df7d904
Update pytest.yml
lorenzomag Aug 7, 2024
c1d26c0
Update pytest.yml + fix test
lorenzomag Aug 7, 2024
a7c2e0d
Downgrade jinja for pytest.yml
lorenzomag Aug 7, 2024
d2b44da
Update pytest.yml
lorenzomag Aug 7, 2024
180e391
Update pytest.yml
lorenzomag Aug 7, 2024
062f66b
Add j2000 to datetime utility function
lorenzomag Aug 8, 2024
3bf17b6
Add j2000_to_datetime pytest
lorenzomag Aug 8, 2024
1679466
Update pytest.yml
lorenzomag Aug 7, 2024
97fcd25
Update pytest.yml + fix test
lorenzomag Aug 7, 2024
03f61dd
Downgrade jinja for pytest.yml
lorenzomag Aug 7, 2024
f5db5f7
Update pytest.yml
lorenzomag Aug 7, 2024
8866e90
Update pytest.yml
lorenzomag Aug 7, 2024
e5d38ef
Revert "Update pytest.yml"
lorenzomag Aug 8, 2024
c9d9d42
Revert "Update pytest.yml"
lorenzomag Aug 8, 2024
e579c2f
Revert "Downgrade jinja for pytest.yml"
lorenzomag Aug 8, 2024
7859f3e
Revert "Update pytest.yml + fix test"
lorenzomag Aug 8, 2024
687bc5e
Revert "Update pytest.yml"
lorenzomag Aug 8, 2024
834ebea
Merge branch 'j2000_to_datetime'
lorenzomag Aug 8, 2024
8693a69
Merge branch 'update_cicd'
lorenzomag Aug 8, 2024
0aeec68
Add j2000 to datetime utility function
lorenzomag Aug 8, 2024
9088162
Add j2000_to_datetime pytest
lorenzomag Aug 8, 2024
020d015
Add halo_model param description to docstring
lorenzomag Jul 14, 2024
3e4ad01
Add basic file
lorenzomag Jul 22, 2024
393ef75
Moved Ibe's model
lorenzomag Jul 22, 2024
6af8458
Added Cox model package at tag
lorenzomag Jul 22, 2024
453eb5b
Moving submodule
lorenzomag Aug 6, 2024
6254779
Updating import statements
lorenzomag Aug 6, 2024
5dc19ac
Add utility function to preformat data for Cox's interpolators
lorenzomag Aug 6, 2024
c9a703a
Wrapper module to include Cox's package
lorenzomag Aug 6, 2024
331f4b8
Add docstring to cox_wrapper.py
lorenzomag Aug 6, 2024
2050efc
Implemented Peter Cox et al. Migdal model
lorenzomag Aug 6, 2024
0978c49
Remove debug print statement
lorenzomag Aug 6, 2024
d4b33d2
Add dockstrings
lorenzomag Aug 6, 2024
4fcb1bd
Update tutorial notebook for Migdal
lorenzomag Aug 6, 2024
0380e5a
Remove typing.Self for python3.9
lorenzomag Aug 6, 2024
381a30d
Require seaborn
lorenzomag Aug 6, 2024
1113870
Add output to notebook
lorenzomag Aug 6, 2024
28e67a3
Revert "Remove typing.Self for python3.9"
lorenzomag Aug 6, 2024
cc39206
Revert "Require seaborn"
lorenzomag Aug 6, 2024
0b79d66
Require Python3.11
lorenzomag Aug 6, 2024
a0ada21
Require importlib package for Cox model
lorenzomag Aug 6, 2024
426a0ed
Add __init__.py in tests to fix module structure
lorenzomag Aug 6, 2024
7b3fbc9
Add all content of data/ for build
lorenzomag Aug 6, 2024
f59cc33
Add deprecation warning for old func
lorenzomag Aug 6, 2024
fa79a1a
Fix typing
lorenzomag Aug 7, 2024
e69cc84
Reduce required python3 version to 3.9
lorenzomag Aug 7, 2024
65430fa
Update notebook output
lorenzomag Aug 7, 2024
c71e0c6
Revert "Update pytest.yml"
lorenzomag Aug 8, 2024
1714627
Revert "Update pytest.yml"
lorenzomag Aug 8, 2024
07ca8ba
Revert "Downgrade jinja for pytest.yml"
lorenzomag Aug 8, 2024
5955350
Revert "Update pytest.yml + fix test"
lorenzomag Aug 8, 2024
3033247
Revert "Update pytest.yml"
lorenzomag Aug 8, 2024
86d8f2c
Merge branch 'migdal_cox_model' of github.com:lorenzomag/wimprates in…
lorenzomag Aug 9, 2024
5286f6c
Rebase on update_cicd
lorenzomag Aug 9, 2024
a7c985c
Merge branch 'migdal_cox_model'
lorenzomag Aug 11, 2024
b4a8e7c
Merge branch 'j2000_to_datetime'
lorenzomag Aug 11, 2024
fa88982
Merge branch 'j2000_to_datetime' of github.com:lorenzomag/wimprates i…
lorenzomag Aug 11, 2024
9adc858
Merge branch 'j2000_to_datetime'
lorenzomag Aug 11, 2024
ca17367
Merge branch 'JelleAalbers:master' into master
lorenzomag Aug 11, 2024
22e9ffe
Round j2000_to_datetime output to fix pytest
lorenzomag Aug 15, 2024
c071c3b
Reduce precision of j2000_to_datetime return to seconds
lorenzomag Aug 15, 2024
cfbd5de
Ignore notebooks in root dir
lorenzomag Aug 15, 2024
22e70fa
Remove overhead on import
lorenzomag Aug 15, 2024
3ab6231
Implement optional multiprocessing for Migdal
lorenzomag Aug 15, 2024
411bfd6
Make Multiprocessing loop more Pythonic
lorenzomag Aug 15, 2024
a0e4d87
Cache Cox Model instance during runtime
lorenzomag Aug 15, 2024
8e6c595
Add caching functionality to save and load results
lorenzomag Aug 15, 2024
3996166
Round j2000_to_datetime output to fix pytest
lorenzomag Aug 15, 2024
98c52f5
Reduce precision of j2000_to_datetime return to seconds
lorenzomag Aug 15, 2024
c5573f8
Merge branch 'j2000_to_datetime'
lorenzomag Aug 15, 2024
f25c5ff
Remove overhead on import
lorenzomag Aug 15, 2024
e6ac016
Implement optional multiprocessing for Migdal
lorenzomag Aug 15, 2024
8c3e5eb
Make Multiprocessing loop more Pythonic
lorenzomag Aug 15, 2024
bfa4394
Cache Cox Model instance during runtime
lorenzomag Aug 15, 2024
623ed89
Add caching functionality to save and load results
lorenzomag Aug 15, 2024
1d7ab55
Merge branch 'optimisation' of github.com:lorenzomag/wimprates into o…
lorenzomag Aug 15, 2024
0034cb4
Update Notebook + remove redundant arg in Cox interpolator
lorenzomag Oct 8, 2024
62e11e9
Add figure needed for notebook tutorial
lorenzomag Oct 8, 2024
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
7 changes: 6 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
# Notebooks in root dir
/*.ipynb

# Byte-compiled / optimized / DLL files
__pycache__/
*.py[cod]
Expand Down Expand Up @@ -72,4 +75,6 @@ target/
*.pdf

# Temp
notebooks/dm_e
notebooks/dm_e

!notebooks/*.png
3 changes: 3 additions & 0 deletions .gitmodules
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
[submodule "wimprates/data/migdal/Cox"]
path = wimprates/data/migdal/Cox/cox_submodule
url = https://github.com/petercox/Migdal.git
1,374 changes: 927 additions & 447 deletions notebooks/Migdal.ipynb

Large diffs are not rendered by default.

Binary file added notebooks/rates_fixed_binding_energies.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
3 changes: 3 additions & 0 deletions pyproject.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
[build-system]
requires = ["setuptools"]
build-backend = "setuptools.build_meta"
1 change: 1 addition & 0 deletions requirements.txt
Original file line number Diff line number Diff line change
Expand Up @@ -4,3 +4,4 @@ pandas
tqdm
boltons
numericalunits
importlib_metadata
53 changes: 31 additions & 22 deletions setup.py
Original file line number Diff line number Diff line change
@@ -1,28 +1,37 @@
import setuptools

readme = open('README.md').read()
history = open('HISTORY.md').read().replace('.. :changelog:', '')
requirements = open('requirements.txt').read().splitlines()
readme = open("README.md").read()
history = open("HISTORY.md").read().replace(".. :changelog:", "")
requirements = open("requirements.txt").read().splitlines()

setuptools.setup(
name='wimprates',
version='0.5.0',
description='Differential rates of WIMP-nucleus scattering',
long_description=readme + '\n\n' + history,
long_description_content_type='text/markdown',
author='Jelle Aalbers',
url='https://github.com/jelleaalbers/wimprates',
license='MIT',
name="wimprates",
version="0.5.0",
description="Differential rates of WIMP-nucleus scattering",
long_description=readme + "\n\n" + history,
long_description_content_type="text/markdown",
author="Jelle Aalbers",
url="https://github.com/jelleaalbers/wimprates",
license="MIT",
packages=setuptools.find_packages(),
setup_requires=['pytest-runner'],
setup_requires=["pytest-runner"],
install_requires=requirements,
package_dir={'wimprates': 'wimprates'},
package_data={'wimprates': [
'data/bs/*', 'data/migdal/*', 'data/sd/*', 'data/dme/*']},
tests_require=requirements + ['pytest', 'unittest'],
keywords='wimp,spin-dependent,spin-independent,bremsstrahlung,migdal',
classifiers=['Intended Audience :: Science/Research',
'Development Status :: 3 - Alpha',
'Programming Language :: Python',
'Programming Language :: Python :: 3'],
zip_safe=False)
package_dir={"wimprates": "wimprates"},
package_data={
"wimprates": ["data/bs/*", "data/migdal/**", "data/sd/*", "data/dme/*"]
},
tests_require=requirements + ["pytest", "unittest"],
keywords="wimp,spin-dependent,spin-independent,bremsstrahlung,migdal",
classifiers=[
"Intended Audience :: Science/Research",
"Development Status :: 3 - Alpha",
"Programming Language :: Python",
"Programming Language :: Python :: 3",
"Programming Language :: Python :: 3.9",
"Programming Language :: Python :: 3.10",
"Programming Language :: Python :: 3.11",
"Programming Language :: Python :: 3.12",
],
python_requires=">=3.9",
zip_safe=False,
)
Empty file added tests/__init__.py
Empty file.
7 changes: 6 additions & 1 deletion tests/test_halo.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
from datetime import datetime

import pandas as pd
from wimprates import j2000, StandardHaloModel, j2000_from_ymd
from wimprates import j2000, StandardHaloModel, j2000_from_ymd, j2000_to_datetime
import numericalunits as nu
import numpy as np

Expand All @@ -21,6 +21,11 @@ def test_j2000_datetime():
assert j2000(date) == 3318.25


def test_datetime_j2000():
date = 3318.25
assert j2000_to_datetime(date) == datetime(year=2009, month=1, day=31, hour=18)


def test_j2000_ns_int():
date = datetime(year=2009, month=1, day=31, hour=18)
value = pd.to_datetime(date).value
Expand Down
19 changes: 17 additions & 2 deletions tests/test_wimprates.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@
class TestBenchmarks(unittest.TestCase):
opts = dict(mw=50,
sigma_nucleon=1e-45,
save_cache=False,
load_cache=False,
)
def test_elastic(self):
ref = 30.39515403337126
Expand All @@ -40,9 +42,19 @@ def test_spindependent(self):
0.00019944698779638946)


def test_migdal(self):
self.assertAlmostEqual(wr.rate_wimp_std(1, detection_mechanism='migdal', **self.opts),
def test_migdal_Ibe(self):
self.assertAlmostEqual(wr.rate_wimp_std(1, detection_mechanism='migdal', migdal_model="Ibe", **self.opts),
0.27459766238555017)


def test_migdal_Cox(self):
self.assertAlmostEqual(wr.rate_wimp_std(1, detection_mechanism='migdal', migdal_model="Cox", **self.opts),
0.2843514286729741)


def test_migdal_Cox_dipole(self):
self.assertAlmostEqual(wr.rate_wimp_std(1, detection_mechanism='migdal', migdal_model="Cox", dipole=True, **self.opts),
0.30438231874513705)


def test_brems(self):
Expand Down Expand Up @@ -104,3 +116,6 @@ def test_average_v_earth(self):
# places=1 means that we get the same results at the first decimal (fine for 500.0<?>)
places=1
)

if __name__ == "__main__":
unittest.main()
2 changes: 1 addition & 1 deletion wimprates/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,4 +14,4 @@
from .migdal import *
from .electron import *
from .summary import *

from .data.migdal.Cox.cox_wrapper import *
1 change: 1 addition & 0 deletions wimprates/data/migdal/Cox/cox_submodule
Submodule cox_submodule added at 99bb84
52 changes: 52 additions & 0 deletions wimprates/data/migdal/Cox/cox_wrapper.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
"""
The paths in the code in Peter Cox's package assume the working directory to be
the root of its package. With this wrapper we change the working dir when computing
the interpolators, then returning the Migdal class instance once they've been instantiated.
The working directory is then reset.
"""

from functools import lru_cache
import os
import sys

from .cox_submodule.Migdal import Migdal
import wimprates as wr

export, __all__ = wr.exporter()


@export
@lru_cache
def cox_migdal_model(element: str, **kwargs) -> Migdal:
"""
This function creates a Cox Migdal model for a given element.

Parameters:
- element (str): The element for which the Cox Migdal model is created.
- **kwargs: Additional keyword arguments for loading probabilities and total probabilities.

Returns:
- material: The Cox Migdal material object.

Example usage:
cox_migdal_model("carbon", arg1=value1, arg2=value2)

Note: The Cox's model assumes that the main process is running in its root directory and uses
relative paths. Therefore, we need to switch the working directory to the root of the package
when computing the interpolators.
This wrapper function changes the working directory temporarily, instantiates the Migdal class,
and then resets the working directory back to its original state.
"""
original_cwd = os.getcwd()

try:
migdal_directory = os.path.join(os.path.dirname(__file__), "cox_submodule")
os.chdir(migdal_directory)

material = Migdal(element)
material.load_probabilities(**kwargs)
material.load_total_probabilities(**kwargs)
finally:
os.chdir(original_cwd)

return material
File renamed without changes.
File renamed without changes.
28 changes: 19 additions & 9 deletions wimprates/electron.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
"""Dark matter - electron scattering
"""
from functools import lru_cache
import numericalunits as nu
import numpy as np
from scipy.interpolate import RegularGridInterpolator, interp1d
Expand All @@ -9,13 +10,17 @@
export, __all__ = wr.exporter()
__all__ += ['dme_shells', 'l_to_letter', 'l_to_number']

# Load form factor and construct interpolators
shell_data = wr.load_pickle('dme/dme_ionization_ff.pkl')
for _shell, _sd in shell_data.items():
_sd['log10ffsquared_itp'] = RegularGridInterpolator(
(_sd['lnks'], _sd['lnqs']),
np.log10(_sd['ffsquared']),
bounds_error=False, fill_value=-float('inf'),)

@lru_cache()
def get_shell_data():
"""Load form factor and construct interpolators"""
shell_data = wr.load_pickle('dme/dme_ionization_ff.pkl')
for _shell, _sd in shell_data.items():
_sd['log10ffsquared_itp'] = RegularGridInterpolator(
(_sd['lnks'], _sd['lnqs']),
np.log10(_sd['ffsquared']),
bounds_error=False, fill_value=-float('inf'),)
return shell_data


dme_shells = [(5, 1), (5, 0), (4, 2), (4, 1), (4, 0)]
Expand Down Expand Up @@ -54,6 +59,9 @@ def dme_ionization_ff(shell, e_er, q):
# Ry = rydberg = 13.6 eV
ry = nu.me * nu.e ** 4 / (8 * nu.eps0 ** 2 * nu.hPlanck ** 2)
lnk = np.log(e_er / ry) / 2

shell_data = get_shell_data()

return 10**(shell_data[shell]['log10ffsquared_itp'](
np.vstack([lnk, lnq]).T))

Expand Down Expand Up @@ -84,8 +92,8 @@ def v_min_dme(eb, erec, q, mw):
return (erec + eb) / q + q / (2 * mw)


# Precompute velocity integrals for t=None
@export
@lru_cache()
def velocity_integral_without_time(halo_model=None):
halo_model = wr.StandardHaloModel() if halo_model is None else halo_model
_v_mins = np.linspace(0, 1, 1000) * wr.v_max(None, halo_model.v_esc)
Expand All @@ -105,7 +113,6 @@ def velocity_integral_without_time(halo_model=None):
fill_value=0, bounds_error=False)
return inverse_mean_speed_kms

inverse_mean_speed_kms = velocity_integral_without_time()


@export
Expand Down Expand Up @@ -141,9 +148,12 @@ def rate_dme(erec, n, l, mw, sigma_dme,

# No bounds are given for the q integral
# but the form factors are only specified in a limited range of q
shell_data = get_shell_data()
qmax = (np.exp(shell_data[shell]['lnqs'].max())
* (nu.me * nu.c0 * nu.alphaFS))

# Precompute velocity integrals for t=None
inverse_mean_speed_kms = velocity_integral_without_time()
if t is None:
# Use precomputed inverse mean speed,
# so we only have to do a single integral
Expand Down
11 changes: 11 additions & 0 deletions wimprates/halo.py
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,17 @@ def j2000_from_ymd(year, month, day_of_month):
+ np.floor(30.61 * (m + 1))
+ day_of_month - 730563.5)

@export
def j2000_to_datetime(j2000_date):
"""
Returns date in np.datetime64 instance from the fractional number of days since J2000.0 epoch.
It is effectively the inverse of the j2000 function.
"""
zero_value = pd.to_datetime("2000-01-01T12:00").value

nanoseconds_per_day = nu.day / nu.ns
_date = pd.to_datetime(j2000_date * nanoseconds_per_day).value
return pd.to_datetime(_date + zero_value).round("s")

@export
def earth_velocity(t, v_0 = None):
Expand Down
Loading
Loading