Code Duplication    Length = 14-16 lines in 2 locations

gammapy/maps/region/ndmap.py 1 location

@@ 366-381 (lines=16) @@
363
            idx[idx_axis] = slice(None)
364
            yield tuple(idx), self.quantity[tuple(idx)]
365
366
    def fill_by_idx(self, idx, weights=None):
367
        # TODO: too complex, simplify!
368
        idx = pix_tuple_to_idx(idx)
369
370
        msk = np.all(np.stack([t != INVALID_INDEX.int for t in idx]), axis=0)
371
        idx = [t[msk] for t in idx]
372
373
        if weights is not None:
374
            if isinstance(weights, u.Quantity):
375
                weights = weights.to_value(self.unit)
376
            weights = weights[msk]
377
378
        idx = np.ravel_multi_index(idx, self.data.T.shape)
379
        idx, idx_inv = np.unique(idx, return_inverse=True)
380
        weights = np.bincount(idx_inv, weights=weights).astype(self.data.dtype)
381
        self.data.T.flat[idx] += weights
382
383
    def get_by_idx(self, idxs):
384
        return self.data[idxs[::-1]]

gammapy/maps/wcs/ndmap.py 1 location

@@ 176-189 (lines=14) @@
173
174
        return vals
175
176
    def fill_by_idx(self, idx, weights=None):
177
        idx = pix_tuple_to_idx(idx)
178
        msk = np.all(np.stack([t != INVALID_INDEX.int for t in idx]), axis=0)
179
        idx = [t[msk] for t in idx]
180
181
        if weights is not None:
182
            if isinstance(weights, u.Quantity):
183
                weights = weights.to_value(self.unit)
184
            weights = weights[msk]
185
186
        idx = np.ravel_multi_index(idx, self.data.T.shape)
187
        idx, idx_inv = np.unique(idx, return_inverse=True)
188
        weights = np.bincount(idx_inv, weights=weights).astype(self.data.dtype)
189
        self.data.T.flat[idx] += weights
190
191
    def set_by_idx(self, idx, vals):
192
        idx = pix_tuple_to_idx(idx)