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

Stop concurrent requests_cache access in from_dataset() with fetch option #7

Merged
merged 11 commits into from
Apr 1, 2021
4 changes: 2 additions & 2 deletions .github/workflows/python-package.yml
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ jobs:
run: |
python -m pip install --upgrade pip
python -m pip install flake8 pytest
if [ -f requirements.txt ]; then pip install -r requirements.txt; fi
python -m pip install .
- name: Lint with flake8
run: |
# stop the build if there are Python syntax errors or undefined names
Expand All @@ -34,4 +34,4 @@ jobs:
flake8 . --count --exit-zero --max-complexity=10 --max-line-length=127 --statistics
- name: Test with pytest
run: |
pytest tests
pytest tests
23 changes: 16 additions & 7 deletions icecube_tools/detector/angular_resolution.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
from astropy.coordinates import SkyCoord
from astropy import units as u

from icecube_tools.utils.data import IceCubeData, find_files
from icecube_tools.utils.data import IceCubeData, find_files, data_directory
from icecube_tools.utils.vMF import get_kappa, get_theta_p

"""
Expand Down Expand Up @@ -267,23 +267,32 @@ def ret_ang_err(self):
return self._ret_ang_err

@classmethod
def from_dataset(cls, dataset_id, **kwargs):
def from_dataset(cls, dataset_id, fetch=True, **kwargs):
"""
Load angular resolution from publicly
available data.
"""

data_interface = IceCubeData()
:dataset_id: ID date of the dataset e.g. "20181018"
:param fetch: If true, download dataset if missing
"""

if dataset_id not in _supported_dataset_ids:

raise NotImplementedError("This dataset is not currently supported")

dataset = data_interface.find(dataset_id)
if fetch:

data_interface = IceCubeData()

dataset = data_interface.find(dataset_id)

data_interface.fetch(dataset)
data_interface.fetch(dataset)

dataset_dir = data_interface.get_path_to(dataset[0])

else:

dataset_dir = data_interface.get_path_to(dataset[0])
dataset_dir = data_directory

if dataset_id == "20181018":

Expand Down
27 changes: 19 additions & 8 deletions icecube_tools/detector/effective_area.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,11 @@
import numpy as np
from abc import ABC, abstractmethod

from icecube_tools.utils.data import IceCubeData, find_files
from icecube_tools.utils.data import (
IceCubeData,
find_files,
data_directory,
)

"""
Module for working with the public IceCube
Expand Down Expand Up @@ -258,7 +262,7 @@ def detection_probability(self, true_energy, true_cos_zenith, max_energy):
return scaled_values[energy_index]

@classmethod
def from_dataset(cls, dataset_id):
def from_dataset(cls, dataset_id, fetch=True, **kwargs):
"""
Build effective area from a public dataset.

Expand All @@ -267,19 +271,26 @@ def from_dataset(cls, dataset_id):
effective areas.

:param dataset_id: Date of dataset release e.g. 20181018
:param fetch: If true, download dataset if not existing
"""

data_interface = IceCubeData()

if dataset_id not in _supported_dataset_ids:

raise NotImplementedError("This dataset is not currently supported")

dataset = data_interface.find(dataset_id)
if fetch:

data_interface = IceCubeData()

dataset = data_interface.find(dataset_id)

data_interface.fetch(dataset)
data_interface.fetch(dataset)

dataset_dir = data_interface.get_path_to(dataset[0])

else:

dataset_dir = data_interface.get_path_to(dataset[0])
dataset_dir = data_directory

# Find filename
if dataset_id == "20181018":
Expand All @@ -296,4 +307,4 @@ def from_dataset(cls, dataset_id):
# Latest dataset
aeff_file_name = files[0]

return cls(aeff_file_name)
return cls(aeff_file_name, **kwargs)
26 changes: 18 additions & 8 deletions icecube_tools/detector/energy_resolution.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
R2015AeffReader,
R2015_AEFF_FILENAME,
)
from icecube_tools.utils.data import IceCubeData, find_files
from icecube_tools.utils.data import IceCubeData, find_files, data_directory

"""
Module for handling the energy resolution
Expand Down Expand Up @@ -119,31 +119,41 @@ def __init__(self, filename, conditional=GIVEN_ETRUE, **kwargs):
self._fit_polynomial()

@classmethod
def from_dataset(cls, dataset_id, **kwargs):
def from_dataset(cls, dataset_id, fetch=True, **kwargs):
"""
Load energy resolution from publicly
available data.
"""

data_interface = IceCubeData()
:param dataset_id: Date identifying the dataset
e.g. "20181018"
:param fetch: If true, download dataset if missing
"""

if dataset_id not in _supported_dataset_ids:

raise NotImplementedError("This dataset is not currently supported")

dataset = data_interface.find(dataset_id)
if fetch:

data_interface = IceCubeData()

dataset = data_interface.find(dataset_id)

data_interface.fetch(dataset)
data_interface.fetch(dataset)

dataset_dir = data_interface.get_path_to(dataset[0])

else:

dataset_dir = data_interface.get_path_to(dataset[0])
dataset_dir = data_directory

if dataset_id == "20150820":

files = find_files(dataset_dir, R2015_AEFF_FILENAME)

eres_file_name = files[0]

return cls(eres_file_name)
return cls(eres_file_name, **kwargs)

def _integrate_out_cos_zenith(self):
"""
Expand Down
9 changes: 8 additions & 1 deletion icecube_tools/utils/data.py
Original file line number Diff line number Diff line change
Expand Up @@ -39,10 +39,17 @@ def __init__(

self.data_directory = data_directory

requests_cache.install_cache(cache_name=cache_name)
requests_cache.install_cache(
cache_name=cache_name,
expire_after=-1,
)

self.ls(verbose=False, update=update)

# Make data directory if it doesn't exist
if not os.path.exists(self.data_directory):
os.makedirs(self.data_directory)

def ls(self, verbose=True, update=False):
"""
List the available datasets.
Expand Down
2 changes: 2 additions & 0 deletions setup.cfg
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,8 @@ install_requires =
bs4
tqdm
versioneer
vMF
pandas

[versioneer]
VCS=git
Expand Down