@@ -45,19 +45,21 @@ def to_events(
45
45
edge_sizes = {dim : da .sizes [dim ] for dim in edge_dims }
46
46
for dim in edge_dims :
47
47
coord = da .coords [dim ]
48
- low = sc .broadcast (coord [dim , :- 1 ], sizes = edge_sizes ).values
49
- high = sc .broadcast (coord [dim , 1 :], sizes = edge_sizes ).values
48
+ left = sc .broadcast (coord [dim , :- 1 ], sizes = edge_sizes ).values
49
+ right = sc .broadcast (coord [dim , 1 :], sizes = edge_sizes ).values
50
50
51
51
# The numpy.random.uniform function below does not support NaNs, so we need to
52
52
# replace them with zeros, and then replace them back after the random numbers
53
53
# have been generated.
54
- nans = np .isnan (low ) | np .isnan (high )
55
- low = np .where (nans , 0.0 , low )
56
- high = np .where (nans , 0.0 , high )
54
+ nans = np .isnan (left ) | np .isnan (right )
55
+ left = np .where (nans , 0.0 , left )
56
+ right = np .where (nans , 0.0 , right )
57
+ # Ensure left <= right
58
+ left , right = np .minimum (left , right ), np .maximum (left , right )
57
59
58
60
# In each bin, we generate a number of events with a uniform distribution.
59
61
events = rng .uniform (
60
- low , high , size = (events_per_bin , * list (edge_sizes .values ()))
62
+ left , right , size = (events_per_bin , * list (edge_sizes .values ()))
61
63
)
62
64
events [..., nans ] = np .nan
63
65
event_coords [dim ] = sc .array (
@@ -77,20 +79,20 @@ def to_events(
77
79
data = da .data
78
80
if event_masks :
79
81
inv_mask = (~ reduce (lambda a , b : a | b , event_masks .values ())).to (dtype = int )
80
- inv_mask .unit = ''
82
+ inv_mask .unit = ""
81
83
data = data * inv_mask
82
84
83
85
# Create the data counts, which are the original counts divided by the number of
84
86
# events per bin
85
87
sizes = {event_dim : events_per_bin } | da .sizes
86
88
val = sc .broadcast (sc .values (data ) / float (events_per_bin ), sizes = sizes )
87
- kwargs = {' dims' : sizes .keys (), ' values' : val .values , ' unit' : data .unit }
89
+ kwargs = {" dims" : sizes .keys (), " values" : val .values , " unit" : data .unit }
88
90
if data .variances is not None :
89
91
# Note here that all the events are correlated.
90
92
# If we later histogram the events with different edges than the original
91
93
# histogram, then neighboring bins will be correlated, and the error obtained
92
94
# will be too small. It is however not clear what can be done to improve this.
93
- kwargs [' variances' ] = sc .broadcast (
95
+ kwargs [" variances" ] = sc .broadcast (
94
96
sc .variances (data ) / float (events_per_bin ), sizes = sizes
95
97
).values
96
98
new_data = sc .array (** kwargs )
0 commit comments