-
Notifications
You must be signed in to change notification settings - Fork 74
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
Add data preparation functions for the Kr map computation #865
base: master
Are you sure you want to change the base?
Changes from all commits
655182a
10affe1
528ad25
b64f9e5
3075a1a
cd737bc
e1538aa
00a3b14
e646b80
73157bd
378dcb9
56f0f70
645cafc
fd339ef
2ad45b6
c59a58e
bd5c5ce
65f0947
ae385b5
4d6bfbd
96f29e5
5a8cd65
dc0b30b
81d0f56
e013211
4d3d0e8
4977fc0
94ab349
fe37a5b
de11a09
1c0dbd3
9871e66
82b9160
fb81f7a
15ac9f7
0e64342
769968b
582b1a9
b8a251a
c76c73c
c745cc4
59386dd
0c635d0
61005e3
3bed7d9
7d12409
8b584bb
85f3834
273ce0b
c35d18a
7b5f2a9
91b69ac
0ac5006
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -3,13 +3,19 @@ | |
This module includes utility functions. | ||
""" | ||
import time | ||
import warnings | ||
|
||
import numpy as np | ||
|
||
from typing import Sequence | ||
from typing import Tuple | ||
from typing import Optional | ||
|
||
|
||
from . exceptions import ValueOutOfRange | ||
from ..types.symbols import NormMode | ||
from ..types.symbols import Strictness | ||
|
||
|
||
def timefunc(f): | ||
""" | ||
|
@@ -77,6 +83,61 @@ def in_range(data, minval=-np.inf, maxval=np.inf, left_closed=True, right_closed | |
return lower_bound & upper_bound | ||
|
||
|
||
def all_in_range(data : np.ndarray , | ||
minval : float , | ||
maxval : float , | ||
display_name : Optional[str] = '' , | ||
strictness : Optional[Strictness] = Strictness.raise_error, | ||
**kwargs)->bool: | ||
""" | ||
Checks whether input values are all inside the interval (minval, maxval). | ||
|
||
Parameters | ||
---------- | ||
data : np.ndarray | ||
Input array to check. | ||
minval: float | ||
Lower limit of the interval. | ||
maxval: float | ||
Upper limit of the interval. | ||
display_name: string | ||
Label to be displayed in case of warning or exception. | ||
strictness: Strictness | ||
Describes the behaviour when the output is `False`. If `strictness` is: | ||
- `Strictness.raise_error`: an exception is raised. | ||
- `Strictness.warning`: a warning is raised before returning. | ||
- `Strictness.silent`: the function returns quietly. | ||
|
||
**kwargs: | ||
Optional arguments being passed to `in_range`. | ||
Returns | ||
------- | ||
True if values are in the interval. False otherwise. | ||
|
||
Raises | ||
------ | ||
ValueOutOfRange: if the output is `False` and `strictness` is `Strictness.raise_error` | ||
""" | ||
|
||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Remove blank line. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Still there |
||
values_in_interval = in_range(data, minval, maxval, **kwargs) | ||
outliers = data[~values_in_interval] | ||
n_outliers = len(outliers) | ||
|
||
if values_in_interval.all(): | ||
return True | ||
elif strictness is Strictness.silent: | ||
return False | ||
|
||
text = f'Variable {display_name} has {n_outliers} values out of bounds ({minval}, {maxval}\n' | ||
text += f'Outliers: {outliers}' | ||
|
||
if strictness is Strictness.warning: | ||
warnings.warn(text, UserWarning) | ||
return False | ||
else: | ||
raise ValueOutOfRange(text) | ||
|
||
|
||
def weighted_mean_and_var(data : Sequence, | ||
weights : Sequence, | ||
unbiased : bool = False, | ||
|
Original file line number | Diff line number | Diff line change | ||||||||||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
@@ -243,6 +243,40 @@ def get_normalization_factor(map_e0 : ASectorMap, | |||||||||||||||||||||
|
||||||||||||||||||||||
return norm_value | ||||||||||||||||||||||
|
||||||||||||||||||||||
def apply_geo_correction(map_e0 : ASectorMap, | ||||||||||||||||||||||
norm_strat : NormStrategy = NormStrategy.max, | ||||||||||||||||||||||
norm_value : Optional[float] = None | ||||||||||||||||||||||
) -> Callable: | ||||||||||||||||||||||
""" | ||||||||||||||||||||||
Given a map and a correction strategy, it returns a | ||||||||||||||||||||||
function that provides a geometric only correction factor | ||||||||||||||||||||||
for a given hit collection when (x,y) is provided. | ||||||||||||||||||||||
|
||||||||||||||||||||||
Parameters | ||||||||||||||||||||||
---------- | ||||||||||||||||||||||
map_e0 : AsectorMap | ||||||||||||||||||||||
Correction map for geometric effects. | ||||||||||||||||||||||
norm_strat : NormStrategy | ||||||||||||||||||||||
Provides the desired normalization to be used. | ||||||||||||||||||||||
norm_value : Float(optional) | ||||||||||||||||||||||
If norm_strat is selected to be custom, user must provide the | ||||||||||||||||||||||
desired scale. | ||||||||||||||||||||||
Comment on lines
+259
to
+263
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||||||||||||||||||
Returns | ||||||||||||||||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Add blank line before Returns |
||||||||||||||||||||||
------- | ||||||||||||||||||||||
A function that returns geometric correction factor without passing a map. | ||||||||||||||||||||||
""" | ||||||||||||||||||||||
normalization = get_normalization_factor(map_e0, norm_strat, norm_value) | ||||||||||||||||||||||
get_xy_corr_fun = maps_coefficient_getter(map_e0.mapinfo, map_e0.e0) | ||||||||||||||||||||||
|
||||||||||||||||||||||
def geo_correction_factor(x : np.ndarray, | ||||||||||||||||||||||
y : np.ndarray)-> np.ndarray: | ||||||||||||||||||||||
geo_factor = correct_geometry_(get_xy_corr_fun(x,y)) | ||||||||||||||||||||||
factor = geo_factor * normalization | ||||||||||||||||||||||
return factor | ||||||||||||||||||||||
|
||||||||||||||||||||||
return geo_correction_factor | ||||||||||||||||||||||
|
||||||||||||||||||||||
|
||||||||||||||||||||||
|
||||||||||||||||||||||
def apply_all_correction_single_maps(map_e0 : ASectorMap, | ||||||||||||||||||||||
map_lt : ASectorMap, | ||||||||||||||||||||||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -11,6 +11,7 @@ | |
from . corrections import get_normalization_factor | ||
from . corrections import apply_all_correction_single_maps | ||
from . corrections import apply_all_correction | ||
from . corrections import apply_geo_correction | ||
|
||
from pytest import fixture | ||
from numpy.testing import assert_allclose | ||
|
@@ -268,21 +269,30 @@ def test_apply_all_correction_single_maps_raises_exception_when_invalid_map(map_ | |
|
||
@few_examples | ||
@given(float_arrays(size = 1, | ||
min_value = -198, | ||
max_value = +198), | ||
float_arrays(size = 1, | ||
min_value = -198, | ||
max_value = +198), | ||
float_arrays(size = 1, | ||
min_value = 0, | ||
max_value = 5e2), | ||
min_value = -198, | ||
max_value = +198), | ||
float_arrays(size = 1, | ||
min_value = 0, | ||
max_value = 1e5)) | ||
min_value = -198, | ||
max_value = +198)) | ||
def test_apply_geo_correction_properly(map_filename, x, y): | ||
Comment on lines
271
to
+277
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Display each |
||
""" | ||
The input map is homogeneous, therefore the geometric | ||
correction factor must be 1. | ||
""" | ||
maps = read_maps(map_filename) | ||
load_corr = apply_geo_correction(map_e0=maps, norm_strat=NormStrategy.max) | ||
corr = load_corr(x, y) | ||
assert_allclose (corr, np.ones_like(corr)) | ||
|
||
@few_examples | ||
@given(float_arrays(size = 1, min_value = -198, max_value = 198), | ||
float_arrays(size = 1, min_value = -198, max_value = 198), | ||
float_arrays(size = 1, min_value = 0, max_value = 500), | ||
float_arrays(size = 1, min_value = 0, max_value = 100*1000)) | ||
Comment on lines
+288
to
+291
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Remove unnecessary spaces after "size" |
||
def test_apply_all_correction_single_maps_properly(map_filename, x, y, z, t): | ||
""" | ||
Due the map taken as input, the geometric correction | ||
factor must be 1, the temporal correction 1 and the | ||
The input map is homogeneous, therefore the geometric | ||
correction factor must be 1, the temporal correction 1 and the | ||
lifetime one: exp(Z/5000). | ||
""" | ||
maps = read_maps(map_filename) | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
add blank line before Returns