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

Updates to ResultsHandler #296

Draft
wants to merge 56 commits into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
56 commits
Select commit Hold shift + click to select a range
d210b58
parameterize discovery potential significance threshold
mlincett Aug 18, 2023
1818f63
work in progress rework of results handler
mlincett Aug 18, 2023
327f18f
update dependencies
mlincett Aug 19, 2023
d89712f
fix bug in pydantic model definition
mlincett Aug 19, 2023
5a04a42
fix bug in pydantic model definition/2
mlincett Aug 19, 2023
6f7373a
progress in decoupling functionality
mlincett Aug 20, 2023
820ab24
fix typo
mlincett Aug 20, 2023
ad405b2
add mypy
mlincett Aug 21, 2023
13c9e84
mypy extensions
mlincett Aug 21, 2023
60eb1b1
poetry lock
mlincett Aug 21, 2023
d37cf84
mypy first pass
mlincett Aug 21, 2023
4f77013
updates
mlincett Aug 21, 2023
36b48f8
mypy config file
mlincett Aug 21, 2023
92f84ee
merge master
mlincett Aug 21, 2023
3dca19a
tentatively deprecate python 3.8
mlincett Aug 21, 2023
5fd38ec
rename plot ts distribution function
mlincett Aug 21, 2023
29d7a47
fix errs in find_overfluctuations
mlincett Aug 21, 2023
fadc8f8
add mypy
mlincett Aug 21, 2023
b9d149f
type hint for subclasses
mlincett Aug 21, 2023
0789a92
mypy compliance
mlincett Aug 21, 2023
4b3d77d
deprecate py 3.8
mlincett Aug 21, 2023
c175cff
mypy compliance
mlincett Aug 21, 2023
b6f5a84
ignore imports for healpy
mlincett Aug 21, 2023
f7c2dbd
subclasses typing
mlincett Aug 21, 2023
2259c15
update to actually existing dataset
mlincett Aug 21, 2023
f0e4a79
avoid redefinitions
mlincett Aug 21, 2023
6610410
rename function
mlincett Aug 21, 2023
2810481
blend stuff together
mlincett Aug 21, 2023
773fbed
mypy compliance
mlincett Aug 21, 2023
c51cf80
comment acknowledging this still needs a rework
mlincett Aug 21, 2023
59f6254
type annotation for subclasses
mlincett Aug 21, 2023
319bd3f
mypy compliance
mlincett Aug 21, 2023
1d6e058
format string / mypy compliance
mlincett Aug 21, 2023
1986712
comment out double definition of a class
mlincett Aug 21, 2023
aeea930
cosmetics
mlincett Aug 21, 2023
2132ded
mypy compliance
mlincett Aug 21, 2023
a82376c
mypy compliance
mlincett Aug 21, 2023
91142a9
comment out unused (?) code
mlincett Aug 21, 2023
e8631b6
cpu_count safeguards
mlincett Aug 21, 2023
e20771f
mypy compliance
mlincett Aug 21, 2023
e1b0c71
add typing import and reorder previous imports
mlincett Aug 22, 2023
2c20fd6
match signature with subclass, drop kwargs support
mlincett Aug 22, 2023
f26b32a
exclude file
mlincett Aug 22, 2023
ee8073a
exclude file from mypy checking
mlincett Aug 22, 2023
c86e133
exclude file from mypy checking
mlincett Aug 22, 2023
563582d
deprecate function
mlincett Aug 22, 2023
d02667b
mypy compliance
mlincett Aug 22, 2023
27dc1c8
exclude file from type checking
mlincett Aug 22, 2023
ad61c11
avoiding redefinitions makes mypy happy
mlincett Aug 22, 2023
d590295
comment out untested and non-working code
mlincett Aug 22, 2023
5787c91
Merge branch 'master' into type-checking
mlincett Aug 22, 2023
bfeee33
del temp variable
mlincett Aug 22, 2023
4116757
del temp variable
mlincett Aug 22, 2023
188fd10
undo unnecessary update
mlincett Aug 22, 2023
b46b7f4
update python version specs
mlincett Aug 22, 2023
13d6fd1
merge mypy branch
mlincett Aug 22, 2023
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 .github/workflows/continous_integration.yml
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ jobs:
# Specify the python versions to test
strategy:
matrix:
python-version: ["3.8", "3.9", "3.10", "3.11"]
python-version: ["3.9", "3.10", "3.11"]

# Steps represent a sequence of tasks that will be executed as part of the job
steps:
Expand Down
19 changes: 14 additions & 5 deletions flarestack/cluster/submitter.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,15 @@
import copy
import json
import os, subprocess, time, logging, shutil, copy, sys
import logging
import numpy as np
import os
from pathlib import Path
import shutil
import subprocess
import sys
import time
from typing import Optional

from flarestack.shared import (
fs_dir,
log_dir,
Expand All @@ -15,14 +24,14 @@
from flarestack.core.minimisation import MinimisationHandler
from flarestack.core.results import ResultsHandler

from pathlib import Path


logger = logging.getLogger(__name__)


class Submitter(object):
submitter_dict = dict()
# in other modules, such attribute is called 'subclass'
# is this functionally different?
submitter_dict: dict[str, object] = dict()

def __init__(
self,
Expand Down Expand Up @@ -302,7 +311,7 @@ class HTCondorSubmitter(Submitter):

# Log path
log_path = Path(log_dir)
override_log_path = None
override_log_path: Optional[Path] = None

def __init__(self, *args, **kwargs):
"""
Expand Down
18 changes: 13 additions & 5 deletions flarestack/core/angular_error_modifier.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@


class BaseFloorClass(object):
subclasses = {}
subclasses: dict[str, object] = {}

def __init__(self, floor_dict):
self.floor_dict = floor_dict
Expand Down Expand Up @@ -149,7 +149,7 @@ def __init__(self, floor_dict):
if not os.path.isfile(self.pickle_name):
self.create_pickle()
else:
logger.debug("Loading from".format(self.pickle_name))
logger.debug(f"Loading from {self.pickle_name}")

with open(self.pickle_name, "r") as f:
pickled_data = Pickle.load(f)
Expand Down Expand Up @@ -182,6 +182,9 @@ def create_function(self, pickled_array):
return lambda data, params: np.array([func(params) for _ in data])


"""
# This class has been commented out since its name is redefined below.
# However, it seems QuantileFloor1D is never explicitly called in the code and is hence likely untested.
@BaseFloorClass.register_subclass("quantile_floor_1d")
class QuantileFloor1D(BaseQuantileFloor, BaseStaticFloor):
def create_pickle(self):
Expand All @@ -190,6 +193,7 @@ def create_pickle(self):
def create_function(self, pickled_array):
func = interp1d(pickled_array[0], pickled_array[1])
return lambda data, params: func(data["logE"])
"""


@BaseFloorClass.register_subclass("quantile_floor_1d_e")
Expand All @@ -212,7 +216,7 @@ def create_function(self, pickled_array):


class BaseAngularErrorModifier(object):
subclasses = {}
subclasses: dict[str, object] = {}

def __init__(self, pull_dict):
self.season = pull_dict["season"]
Expand Down Expand Up @@ -245,7 +249,7 @@ def create(
e_pdf_dict,
floor_name="static_floor",
aem_name="no_modifier",
**kwargs
**kwargs,
):
pull_dict = dict()
pull_dict["season"] = season
Expand Down Expand Up @@ -335,7 +339,7 @@ def __init__(self, pull_dict):
if not os.path.isfile(self.pull_name):
self.create_pickle()
else:
logger.debug("Loading from".format(self.pull_name))
logger.debug(f"Loading from {self.pull_name}")

with open(self.pull_name, "r") as f:
self.pickled_data = Pickle.load(f)
Expand Down Expand Up @@ -466,6 +470,9 @@ def create_dynamic(self, pickled_array):
return lambda data: func(data["logE"], data["sinDec"])


"""
# NOTE: `mypy` allowed to find some deprecations in the following __main__ block that were never noticed before, because the block is not part of any testing routine. It should be checked whether equivalent code is covered by an unit test, before removing the (now commented) block altogether.

if __name__ == "__main__":
from flarestack.data.icecube.ps_tracks.ps_v002_p01 import IC86_1_dict
from flarestack.analyses.angular_error_floor.plot_bias import (
Expand Down Expand Up @@ -530,3 +537,4 @@ def med_pull(data):
# for gamma in np.linspace(1.0, 4.0, 4):
# print data_loader(IC86_1_dict["exp_path"])["logE"][:8]
# print np.degrees(x.f(data_loader(IC86_1_dict["exp_path"])[:8], [gamma]))
"""
2 changes: 1 addition & 1 deletion flarestack/core/energy_pdf.py
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ class EnergyPDF(object):
A base class for Energy PDFs.
"""

subclasses = {}
subclasses: dict[str, object] = {}

def __init__(self, e_pdf_dict):
"""
Expand Down
4 changes: 2 additions & 2 deletions flarestack/core/injector.py
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ def read_injector_dict(inj_dict):
class BaseInjector:
"""Base Injector Class"""

subclasses = {}
subclasses: dict[str, object] = {}

def __init__(self, season, sources, **kwargs):
kwargs = read_injector_dict(kwargs)
Expand Down Expand Up @@ -224,7 +224,7 @@ class MCInjector(BaseInjector):
background. This can be either MC background, or scrambled real data.
"""

subclasses = {}
subclasses: dict[str, object] = {}

def __init__(self, season, sources, **kwargs):
kwargs = read_injector_dict(kwargs)
Expand Down
18 changes: 12 additions & 6 deletions flarestack/core/llh.py
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@ def read_llh_dict(llh_dict):
class LLH(object):
"""Base class LLH."""

subclasses = {}
subclasses: dict[str, object] = {}

def __init__(self, season, sources, llh_dict):
self.season = season
Expand Down Expand Up @@ -900,7 +900,7 @@ def create_kwargs(self, data, pull_corrector, weight_f=None):

return kwargs

def calculate_test_statistic(self, params, weights, **kwargs):
def calculate_test_statistic(self, params, weights, **kwargs) -> float:
"""Calculates the test statistic, given the parameters. Uses numexpr
for faster calculations.

Expand All @@ -920,18 +920,18 @@ def calculate_test_statistic(self, params, weights, **kwargs):
# If n_s if negative, then removes the energy term from the likelihood

for i, n_j in enumerate(all_n_j):
SoB_spacetime: list = kwargs["SoB_spacetime_cache"][i]
SoB_spacetime_data: list = kwargs["SoB_spacetime_cache"][i]

# Switches off Energy term for negative n_s, which should in theory
# be a continuous change that does not alter the likelihood for
# n_s > 0 (as it is not included for n_s=0).

if len(SoB_spacetime) == 0:
if len(SoB_spacetime_data) == 0:
x.append(np.array([1.0]))

else:
SoB_spacetime = kwargs["pull_corrector"].estimate_spatial(
gamma, SoB_spacetime
gamma, SoB_spacetime_data
)

if n_j < 0:
Expand Down Expand Up @@ -1394,13 +1394,18 @@ def find_significant_events(self, coincident_data, source):
return FlareLLH(season, sources, llh_dict)


"""
# NOTE: `mypy` allowed to find some deprecations in the following __main__ block that were never noticed before, because the block is not part of any testing routine. It should be checked whether equivalent code is covered by an unit test, before removing the (now commented) block altogether.

if __name__ == "__main__":


from flarestack.shared import fs_scratch_dir
from scipy.interpolate import InterpolatedUnivariateSpline

g = EnergyPDF.create({"energy_pdf_name": "PowerLaw", "gamma": 2.2})

e_range = np.logspace(0, 7, 1e3)
e_range = np.logspace(start=0, stop=7, num=1000)

f = InterpolatedUnivariateSpline(e_range, np.log(g.f(e_range)))

Expand Down Expand Up @@ -1440,3 +1445,4 @@ def find_significant_events(self, coincident_data, source):

for i in np.linspace(0.0, 10.0, 21):
print(i, f([i], weights))
"""
4 changes: 2 additions & 2 deletions flarestack/core/minimisation.py
Original file line number Diff line number Diff line change
Expand Up @@ -93,10 +93,10 @@ class MinimisationHandler(object):
the injector and the likelihood.
"""

subclasses = {}
subclasses: dict[str, object] = {}

# Each MinimisationHandler must specify which LLH classes are compatible
compatible_llh = []
compatible_llh: list[str] = []
compatible_negative_n_s = False

def __init__(self, mh_dict):
Expand Down
12 changes: 10 additions & 2 deletions flarestack/core/multiprocess_wrapper.py
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,9 @@ def add_injector(self, season, sources):

class MultiProcessor:
queue = None
results = dict()

# this is not used within the class, maybe it can be safely dropped
# results = dict()

def __init__(self, n_cpu, **kwargs):
self.queue = JoinableQueue()
Expand Down Expand Up @@ -149,9 +151,15 @@ def run_multiprocess(n_cpu, mh_dict):

logging.basicConfig(level=logging.INFO)

# os.cpu_count() may return None, deal safely with that
cpu_count = os.cpu_count()
available_cpus = cpu_count if cpu_count is not None else 1
# 1 <= n_cpu <= 32
n_cpu = min(max(1, available_cpus - 1), 32)

parser = argparse.ArgumentParser()
parser.add_argument("-f", "--file", help="Path for analysis pkl_file")
parser.add_argument("-n", "--n_cpu", default=min(max(1, os.cpu_count() - 1), 32))
parser.add_argument("-n", "--n_cpu", default=n_cpu)
cfg = parser.parse_args()

logger.info(f"N CPU available {os.cpu_count()}. Using {cfg.n_cpu}")
Expand Down
Loading