From 96721431c3d5c841be1a6f1de18554e2a0df4ca5 Mon Sep 17 00:00:00 2001 From: Fabian Joswig Date: Fri, 29 Oct 2021 12:13:04 +0100 Subject: [PATCH] speed up of merge_idx --- pyerrors/pyerrors.py | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/pyerrors/pyerrors.py b/pyerrors/pyerrors.py index bf5af0f3..89452b0a 100644 --- a/pyerrors/pyerrors.py +++ b/pyerrors/pyerrors.py @@ -8,6 +8,7 @@ import autograd.numpy as anp # Thinly-wrapped numpy from autograd import jacobian import matplotlib.pyplot as plt import numdifftools as nd +from itertools import groupby class Obs: @@ -819,6 +820,12 @@ def merge_idx(idl): ---------- idl -- List of lists or ranges. """ + + # Use groupby to efficiently check whether all elements of idl are identical + g = groupby(idl) + if next(g, True) and not next(g, False): + return idl[0] + if np.all([type(idx) is range for idx in idl]): if len(set([idx[0] for idx in idl])) == 1: idstart = min([idx.start for idx in idl])