@@ -113,7 +113,8 @@ def ocelot2cheetah(element, warnings=True):
113113 elif isinstance (element , oc .Monitor ) and "BSC" in element .id :
114114 if warnings :
115115 print (
116- "WARNING: Diagnostic screen was converted with default screen properties."
116+ "WARNING: Diagnostic screen was converted with default screen"
117+ " properties."
117118 )
118119 return acc .Screen ((2448 , 2040 ), (3.5488e-6 , 2.5003e-6 ), name = element .id )
119120 elif isinstance (element , oc .Monitor ) and "BPM" in element .id :
@@ -141,127 +142,3 @@ def subcell_of_ocelot(cell, start, end):
141142 break
142143
143144 return subcell
144-
145-
146- _range = range
147-
148-
149- def histogramdd (sample , bins = None , range = None , weights = None , remove_overflow = True ):
150- """
151- Pytorch version of n-dimensional histogram.
152-
153- Taken from https://github.com/miranov25/RootInteractive/blob/b54446e09072e90e17f3da72d5244a20c8fdd209/RootInteractive/Tools/Histograms/histogramdd.py
154- """
155- edges = None
156- device = None
157- custom_edges = False
158- D , N = sample .shape
159- if device == None :
160- device = sample .device
161- if bins == None :
162- if edges == None :
163- bins = 10
164- custom_edges = False
165- else :
166- try :
167- bins = edges .size (1 ) - 1
168- except AttributeError :
169- bins = torch .empty (D )
170- for i in _range (len (edges )):
171- bins [i ] = edges [i ].size (0 ) - 1
172- bins = bins .to (device )
173- custom_edges = True
174- try :
175- M = bins .size (0 )
176- if M != D :
177- raise ValueError (
178- "The dimension of bins must be equal to the dimension of sample x."
179- )
180- except AttributeError :
181- # bins is either an integer or a list
182- if type (bins ) == int :
183- bins = torch .full ([D ], bins , dtype = torch .long , device = device )
184- elif torch .is_tensor (bins [0 ]):
185- custom_edges = True
186- edges = bins
187- bins = torch .empty (D , dtype = torch .long )
188- for i in _range (len (edges )):
189- bins [i ] = edges [i ].size (0 ) - 1
190- bins = bins .to (device )
191- else :
192- bins = torch .as_tensor (bins )
193- if bins .dim () == 2 :
194- custom_edges = True
195- edges = bins
196- bins = torch .full ([D ], bins .size (1 ) - 1 , dtype = torch .long , device = device )
197- if custom_edges :
198- use_old_edges = False
199- if not torch .is_tensor (edges ):
200- use_old_edges = True
201- edges_old = edges
202- m = max (i .size (0 ) for i in edges )
203- tmp = torch .empty ([D , m ], device = edges [0 ].device )
204- for i in _range (D ):
205- s = edges [i ].size (0 )
206- tmp [i , :] = edges [i ][- 1 ]
207- tmp [i , :s ] = edges [i ][:]
208- edges = tmp .to (device )
209- k = torch .searchsorted (edges , sample )
210- k = torch .min (k , (bins + 1 ).reshape (- 1 , 1 ))
211- if use_old_edges :
212- edges = edges_old
213- else :
214- edges = torch .unbind (edges )
215- else :
216- if range == None : # range is not defined
217- range = torch .empty (2 , D , device = device )
218- if N == 0 : # Empty histogram
219- range [0 , :] = 0
220- range [1 , :] = 1
221- else :
222- range [0 , :] = torch .min (sample , 1 )[0 ]
223- range [1 , :] = torch .max (sample , 1 )[0 ]
224- elif not torch .is_tensor (range ): # range is a tuple
225- r = torch .empty (2 , D )
226- for i in _range (D ):
227- if range [i ] is not None :
228- r [:, i ] = torch .as_tensor (range [i ])
229- else :
230- if N == 0 : # Edge case: empty histogram
231- r [0 , i ] = 0
232- r [1 , i ] = 1
233- r [0 , i ] = torch .min (sample [:, i ])[0 ]
234- r [1 , i ] = torch .max (sample [:, i ])[0 ]
235- range = r .to (device = device , dtype = sample .dtype )
236- singular_range = torch .eq (
237- range [0 ], range [1 ]
238- ) # If the range consists of only one point, pad it up.
239- range [0 , singular_range ] -= 0.5
240- range [1 , singular_range ] += 0.5
241- edges = [
242- torch .linspace (range [0 , i ], range [1 , i ], bins [i ] + 1 )
243- for i in _range (len (bins ))
244- ]
245- tranges = torch .empty_like (range )
246- tranges [1 , :] = bins / (range [1 , :] - range [0 , :])
247- tranges [0 , :] = 1 - range [0 , :] * tranges [1 , :]
248- k = torch .addcmul (
249- tranges [0 , :].reshape (- 1 , 1 ), sample , tranges [1 , :].reshape (- 1 , 1 )
250- ).long () # Get the right index
251- k = torch .max (
252- k , torch .zeros ([], device = device , dtype = torch .long )
253- ) # Underflow bin
254- k = torch .min (k , (bins + 1 ).reshape (- 1 , 1 ))
255-
256- multiindex = torch .ones_like (bins )
257- multiindex [1 :] = torch .cumprod (torch .flip (bins [1 :], [0 ]) + 2 , - 1 ).long ()
258- multiindex = torch .flip (multiindex , [0 ])
259- l = torch .sum (k * multiindex .reshape (- 1 , 1 ), 0 )
260- hist = torch .bincount (
261- l , minlength = (multiindex [0 ] * (bins [0 ] + 2 )).item (), weights = weights
262- )
263- hist = hist .reshape (tuple (bins + 2 ))
264- if remove_overflow :
265- core = D * (slice (1 , - 1 ),)
266- hist = hist [core ]
267- return hist , edges
0 commit comments