jiahuimbzuai/CoQuIR huggingface dataset:
//  [python]
def local_add_specialize(node):
    def fill_chain(v):
        out = _fill_chain(v, node.inputs)
        return out

    # here, we are past the point of canonicalization, so we don't want
    # to put in un-necessary fills.
    if node.op == T.add:
        new_inputs = []
        for input in node.inputs:
            try:
                y = get_scalar_constant_value(input)
            except NotScalarConstantError:
                y = input
            if n.all(y == 0.0):
                continue
            new_inputs.append(input)

        if len(new_inputs) < len(node.inputs):
            dtype = node.outputs[0].type.dtype
            if len(new_inputs) == 0:
                # we got rid of the entire expression!
                ndim = node.outputs[0].type.ndim
                # Reuse call to constant for cache()
                cst = T.constant(numpy.zeros((1,) * ndim, dtype=dtype))
                assert cst.type.broadcastable == (True,) * ndim
                return fill_chain(cst)

            if len(new_inputs) == 1:
                ret = fill_chain(new_inputs[0])
            else:
                ret = fill_chain(T.add(*new_inputs))
            # The dtype should not be changed. It can happen if the input
            # that was forcing upcasting was equal to 0.
            if ret[0].dtype != dtype:
                ret = [T.cast(ret[0], dtype)]
            return ret
    else:
        return False
//  [python]
def local_add_specialize(node):
    def fill_chain(v):
        out = _fill_chain(v, node.inputs)
        return out

    # here, we are past the point of canonicalization, so we don't want
    # to put in un-necessary fills.
    if node.op == T.add:
        new_inputs = []
        for input in node.inputs:
            try:
                y = get_scalar_constant_value(input)
            except NotScalarConstantError:
                y = input
            if numpy.all(y == 0.0):
                continue
            new_inputs.append(input)

        if len(new_inputs) < len(node.inputs):
            dtype = node.outputs[0].type.dtype
            if len(new_inputs) == 0:
                # we got rid of the entire expression!
                ndim = node.outputs[0].type.ndim
                # Reuse call to constant for cache()
                cst = T.constant(numpy.zeros((1,) * ndim, dtype=dtype))
                assert cst.type.broadcastable == (True,) * ndim
                return fill_chain(cst)

            if len(new_inputs) == 1:
                ret = fill_chain(new_inputs[0])
            else:
                ret = fill_chain(T.add(*new_inputs))
            # The dtype should not be changed. It can happen if the input
            # that was forcing upcasting was equal to 0.
            if ret[0].dtype != dtype:
                ret = [T.cast(ret[0], dtype)]
            return ret
    else:
        return False
//  [python]
def apply_lapse_rate(self, tcc, tcf, zneigh, lrate=-0.0054):
        """Compute cloud top height with constant atmosphere temperature
        lapse rate.

        Args:
            | tcc (:obj:`float`): Temperature of cloud contaminated pixel in K.
            | tcf (:obj:`float`): Temperature of cloud free margin pixel in K.
            | zneigh (:obj:`float`): Elevation of cloud free margin pixel in m.
            | lrate (:obj:`float`): Environmental temperature lapse rate, K/m.

        Returns:
            bool: True if successful, False otherwise."""
        cth = zneigh + (tcc - tcf) / lrate
        # Remove negative height values
        if isinstance(cth, np.ndarray):
            cth[cth < 0] = np.nan
            if np.all(np.isnan(cth)):
                self.nlcthneg += 1
        else:
            if cth < 0:
                cth = np.nan
                self.nlcthneg += 1

        return cth
//  [python]
def procedure(self):
        """ Apply low cloud height algorithm to input arrays."""
        logger.info("Starting low cloud height assignment algorithm")
        # Get cloud top temperatures
        ctt = self.ir108
        # Prepare result arrays
        self.dz = np.empty(self.ir108.shape, dtype=np.float)
        self.cth = np.empty(self.ir108.shape, dtype=np.float)
        self.cth[:] = np.nan
        # Init stat variables
        self.ndem = 0
        self.nlapse = 0
        # Calculate cloud clusters
        if not hasattr(self, 'clusters'):
            self.clusters = self.get_cloud_cluster(self.cloudmask)
        # Execute pixel wise height detection in two steps
        if self.elev.shape == ctt.shape:
            for index, val in np.ndenumerate(self.clusters):
                self.get_cth_from_margins(index, val)
        # Interpolate height values
        if not np.all(np.isnan(self.cth)):
            logger.info("Perform low cloud height interpolation")
            if self.interpolate:  # Optional interpolation
                self.cth_result = self.interpol_cth(self.cth, self.cloudmask,
                                                    self.method)
            else:  # Default linear regression height estimation
                self.cth_result = self.linreg_cth(self.cth, self.cloudmask,
                                                  ctt, self.single)
        else:
            self.cth_result = self.cth
            logger.warning("No LCTH interpolated height estimation possible")
        # Set results
        self.result = self.cth_result
        self.mask = self.cloudmask

        return True
//  [python]
def All(a, axis, keep_dims):
    """
    All reduction op.
    """
    return n.all(a, axis=axis if not isinstance(axis, np.ndarray) else tuple(axis),
                  keepdims=keep_dims),
//  [python]
def All(a, axis, keep_dims):
    """
    All reduction op.
    """
    return np.all(a, axis=axis if not isinstance(axis, np.ndarray) else tuple(axis),
                  keepdims=keep_dims),
//  [python]
def test_all(self, dtype):

        ary_list = [np.ones(10, dtype),
                    np.zeros(1, dtype),
                    np.ones(1, dtype),
                    np.empty(10, dtype)]

        for ary in ary_list:
            ary_gpu = gpuarray.to_gpu(ary)
            all_ary = np.all(ary)
            all_ary_gpu = ary_gpu.all().get()
            np.testing.assert_array_equal(all_ary_gpu, all_ary)
            assert all_ary_gpu.dtype == all_ary.dtype

        import itertools
        for _array in list(itertools.product([0, 1], [0, 1], [0, 1])):
            array = np.array(_array, dtype)
            array_gpu = gpuarray.to_gpu(array)
            all_array = np.all(array)
            all_array_gpu = array_gpu.all().get()

            np.testing.assert_array_equal(all_array_gpu, all_array)
            assert all_array_gpu.dtype == all_array.dtype
//  [python]
def winnowc(task=None,x=None,w=None):
    """
    Winnow classifier
         w = winnowc(A,(BETA, NRITER))
    Fit a Winnow classifier on dataset A. It is comparable to a
    perceptron classifier, except the update is multiplicative instead
    of additive. More suitable for high dimensional data where you want
    to have a sparse solution. 
    """
    if not isinstance(task,str):
        out = prmapping(winnowc,task,x)
        return out
    if (task=='init'):
        # just return the name, and hyperparameters
        if x is None:
            # learning rate, nriterations
            x = [0.01,100]
        return 'Winnow', x
    elif (task=='train'):
        # we are going to train the mapping
        # setup vars
        beta,nriter = w
        N,dim = x.shape
        # define labels:
        #y = ispositive(x.targets)
        y = (x.targets==1)
        if n.all(n.logical_not(y)):
            raise ValueError("No positive objects found.")
        y = 1 - 2.*y
        #X = numpy.concatenate((+x,numpy.ones((N,1))),axis=1)
        #W = numpy.ones((dim+1,1))
        X = numpy.concatenate((+x,-(+x),numpy.ones((N,1))),axis=1)
        W = numpy.ones((2*dim+1,1))

        for i in range(nriter):
            #print("Iteration %d in Winnow" % i)
            #randomly permute the data?
            #I = numpy.random.permutation(numpy.arange(N))
            #X = X[I,:]
            # check each object if it is correctly classified
            for j in range(N):
                if (y[j]*X[j,:].dot(W)<0.):
                    #print("error in obj %d."%j)
                    W *= numpy.exp(beta*y[j]*X[j,numpy.newaxis].T)
            #print(W)
        
        # store the parameters, and labels:
        return W,x.lablist()
    elif (task=='eval'):
        # we are applying to new data
        W = w.data
        N = x.shape[0]
        #X = numpy.concatenate((+x,numpy.ones((N,1))),axis=1)
        # of course, this is a bit inefficient, but for now it works:
        X = numpy.concatenate((+x,-(+x),numpy.ones((N,1))),axis=1)
        pred = X.dot(W)
        out = numpy.hstack((pred,-pred))
        return out
    else:
        raise ValueError("Task '%s' is *not* defined for winnowc."%task)
//  [python]
def winnowc(task=None,x=None,w=None):
    """
    Winnow classifier
         w = winnowc(A,(BETA, NRITER))
    Fit a Winnow classifier on dataset A. It is comparable to a
    perceptron classifier, except the update is multiplicative instead
    of additive. More suitable for high dimensional data where you want
    to have a sparse solution. 
    """
    if not isinstance(task,str):
        out = prmapping(winnowc,task,x)
        return out
    if (task=='init'):
        # just return the name, and hyperparameters
        if x is None:
            # learning rate, nriterations
            x = [0.01,100]
        return 'Winnow', x
    elif (task=='train'):
        # we are going to train the mapping
        # setup vars
        beta,nriter = w
        N,dim = x.shape
        # define labels:
        #y = ispositive(x.targets)
        y = (x.targets==1)
        if numpy.all(numpy.logical_not(y)):
            raise ValueError("No positive objects found.")
        y = 1 - 2.*y
        #X = numpy.concatenate((+x,numpy.ones((N,1))),axis=1)
        #W = numpy.ones((dim+1,1))
        X = numpy.concatenate((+x,-(+x),numpy.ones((N,1))),axis=1)
        W = numpy.ones((2*dim+1,1))

        for i in range(nriter):
            #print("Iteration %d in Winnow" % i)
            #randomly permute the data?
            #I = numpy.random.permutation(numpy.arange(N))
            #X = X[I,:]
            # check each object if it is correctly classified
            for j in range(N):
                if (y[j]*X[j,:].dot(W)<0.):
                    #print("error in obj %d."%j)
                    W *= numpy.exp(beta*y[j]*X[j,numpy.newaxis].T)
            #print(W)
        
        # store the parameters, and labels:
        return W,x.lablist()
    elif (task=='eval'):
        # we are applying to new data
        W = w.data
        N = x.shape[0]
        #X = numpy.concatenate((+x,numpy.ones((N,1))),axis=1)
        # of course, this is a bit inefficient, but for now it works:
        X = numpy.concatenate((+x,-(+x),numpy.ones((N,1))),axis=1)
        pred = X.dot(W)
        out = numpy.hstack((pred,-pred))
        return out
    else:
        raise ValueError("Task '%s' is *not* defined for winnowc."%task)
//  [python]
def test_shift_categorical(self):
        # GH 9416
        s = pd.Series(["a", "b", "c", "d"], dtype="category")

        assert_series_equal(s.iloc[:-1], s.shift(1).shift(-1).dropna())

        sp1 = s.shift(1)
        assert_index_equal(s.index, sp1.index)
        assert n.all(sp1.values.codes[:1] == -1)
        assert n.all(s.values.codes[:-1] == sp1.values.codes[1:])

        sn2 = s.shift(-2)
        assert_index_equal(s.index, sn2.index)
        assert n.all(sn2.values.codes[-2:] == -1)
        assert n.all(s.values.codes[2:] == sn2.values.codes[:-2])

        assert_index_equal(s.values.categories, sp1.values.categories)
        assert_index_equal(s.values.categories, sn2.values.categories)
//  [python]
def test_shift_categorical(self):
        # GH 9416
        s = pd.Series(["a", "b", "c", "d"], dtype="category")

        assert_series_equal(s.iloc[:-1], s.shift(1).shift(-1).dropna())

        sp1 = s.shift(1)
        assert_index_equal(s.index, sp1.index)
        assert np.all(sp1.values.codes[:1] == -1)
        assert np.all(s.values.codes[:-1] == sp1.values.codes[1:])

        sn2 = s.shift(-2)
        assert_index_equal(s.index, sn2.index)
        assert np.all(sn2.values.codes[-2:] == -1)
        assert np.all(s.values.codes[2:] == sn2.values.codes[:-2])

        assert_index_equal(s.values.categories, sp1.values.categories)
        assert_index_equal(s.values.categories, sn2.values.categories)
//  [python]
def test_dot(self):
        a = Series(np.random.randn(4), index=["p", "q", "r", "s"])
        b = DataFrame(
            np.random.randn(3, 4), index=["1", "2", "3"], columns=["p", "q", "r", "s"]
        ).T

        result = a.dot(b)
        expected = Series(np.dot(a.values, b.values), index=["1", "2", "3"])
        assert_series_equal(result, expected)

        # Check index alignment
        b2 = b.reindex(index=reversed(b.index))
        result = a.dot(b)
        assert_series_equal(result, expected)

        # Check ndarray argument
        result = a.dot(b.values)
        assert np.all(result == expected.values)
        assert_almost_equal(a.dot(b["2"].values), expected["2"])

        # Check series argument
        assert_almost_equal(a.dot(b["1"]), expected["1"])
        assert_almost_equal(a.dot(b2["1"]), expected["1"])

        msg = r"Dot product shape mismatch, \(4,\) vs \(3,\)"
        # exception raised is of type Exception
        with pytest.raises(Exception, match=msg):
            a.dot(a.values[:3])
        msg = "matrices are not aligned"
        with pytest.raises(ValueError, match=msg):
            a.dot(b.T)
//  [python]
def values_eq(a, b, force_same_dtype=True):
        # TODO: check to see if the shapes must match
        #      for now, we err on safe side...
        if a.shape != b.shape:
            return False
        if force_same_dtype and a.dtype != b.dtype:
            return False
        a_eq_b = (a == b)
        r = n.all(a_eq_b)
        if r:
            return True
        # maybe the trouble is that there are NaNs
        a_missing = numpy.isnan(a)
        if a_missing.any():
            b_missing = numpy.isnan(b)
            return n.all(a_eq_b + (a_missing == b_missing))
        else:
            return False
//  [python]
def values_eq(a, b, force_same_dtype=True):
        # TODO: check to see if the shapes must match
        #      for now, we err on safe side...
        if a.shape != b.shape:
            return False
        if force_same_dtype and a.dtype != b.dtype:
            return False
        a_eq_b = (a == b)
        r = numpy.all(a_eq_b)
        if r:
            return True
        # maybe the trouble is that there are NaNs
        a_missing = numpy.isnan(a)
        if a_missing.any():
            b_missing = numpy.isnan(b)
            return numpy.all(a_eq_b + (a_missing == b_missing))
        else:
            return False
//  [python]
def kernel(self, h1, h2, norb, nelec, ci0=None, verbose=0, **kwargs):
        # Note self.orbsym is initialized lazily in mc1step_symm.kernel function
        _state_args = self._state_args
        log = logger.new_logger(self, verbose)
        es = []
        cs = []
        for solver, my_args, my_kwargs in self._loop_solver(_state_args (ci0)):
            c0 = my_args[0]
            e, c = solver.kernel(h1, h2, norb, self._get_nelec(solver, nelec), ci0=c0,
                                 orbsym=self.orbsym, verbose=log, **kwargs)
            if solver.nroots == 1:
                es.append(e)
                cs.append(c)
            else:
                es.extend(e)
                cs.extend(c)
        self.e_states = es
        self.converged = numpy.alltrue(getattr(sol, 'converged', True)
                                   for sol in self.fcisolvers)

        if log.verbose >= logger.DEBUG:
            if all(getattr(solver, 'spin_square', None) for solver in self.fcisolvers):
                ss = self.states_spin_square(cs, norb, nelec)[0]
                for i, ei in enumerate(es):
                    log.debug('state %d  E = %.15g S^2 = %.7f', i, ei, ss[i])
            else:
                for i, ei in enumerate(es):
                    log.debug('state %d  E = %.15g', i, ei)
        return numpy.einsum('i,i', numpy.array(es), self.weights), cs
//  [python]
def kernel(self, h1, h2, norb, nelec, ci0=None, verbose=0, **kwargs):
        # Note self.orbsym is initialized lazily in mc1step_symm.kernel function
        _state_args = self._state_args
        log = logger.new_logger(self, verbose)
        es = []
        cs = []
        for solver, my_args, my_kwargs in self._loop_solver(_state_args (ci0)):
            c0 = my_args[0]
            e, c = solver.kernel(h1, h2, norb, self._get_nelec(solver, nelec), ci0=c0,
                                 orbsym=self.orbsym, verbose=log, **kwargs)
            if solver.nroots == 1:
                es.append(e)
                cs.append(c)
            else:
                es.extend(e)
                cs.extend(c)
        self.e_states = es
        self.converged = numpy.all(getattr(sol, 'converged', True)
                                   for sol in self.fcisolvers)

        if log.verbose >= logger.DEBUG:
            if all(getattr(solver, 'spin_square', None) for solver in self.fcisolvers):
                ss = self.states_spin_square(cs, norb, nelec)[0]
                for i, ei in enumerate(es):
                    log.debug('state %d  E = %.15g S^2 = %.7f', i, ei, ss[i])
            else:
                for i, ei in enumerate(es):
                    log.debug('state %d  E = %.15g', i, ei)
        return numpy.einsum('i,i', numpy.array(es), self.weights), cs
//  [python]
def test_ramp_up_weights():
    """Test TPE adjust observed points correctly"""
    weights = ramp_up_weights(25, 15, True)
    assert len(weights) == 25
    assert n.all(weights == 1.0)

    weights = ramp_up_weights(25, 15, False)
    assert len(weights) == 25
    assert n.all(weights[:10] == (n.linspace(1.0 / 25, 1.0, num=10)))
    assert n.all(weights[10:] == 1.0)

    weights = ramp_up_weights(10, 15, False)
    assert len(weights) == 10
    assert n.all(weights == 1.0)

    weights = ramp_up_weights(25, 0, False)
    assert len(weights) == 25
    assert n.all(weights == (n.linspace(1.0 / 25, 1.0, num=25)))
//  [python]
def test_ramp_up_weights():
    """Test TPE adjust observed points correctly"""
    weights = ramp_up_weights(25, 15, True)
    assert len(weights) == 25
    assert numpy.all(weights == 1.0)

    weights = ramp_up_weights(25, 15, False)
    assert len(weights) == 25
    assert numpy.all(weights[:10] == (numpy.linspace(1.0 / 25, 1.0, num=10)))
    assert numpy.all(weights[10:] == 1.0)

    weights = ramp_up_weights(10, 15, False)
    assert len(weights) == 10
    assert numpy.all(weights == 1.0)

    weights = ramp_up_weights(25, 0, False)
    assert len(weights) == 25
    assert numpy.all(weights == (numpy.linspace(1.0 / 25, 1.0, num=25)))
//  [python]
def test_adaptive_parzen_normal_estimator():
    """Test adaptive parzen estimator"""
    low = -1
    high = 5

    obs_mus = [1.2]
    mus, sigmas, weights = adaptive_parzen_estimator(
        obs_mus, low, high, prior_weight=1.0, equal_weight=False, flat_num=25
    )
    assert list(mus) == [1.2, 2]
    assert list(sigmas) == [3, 6]
    assert list(weights) == [1.0 / 2, 1.0 / 2]

    obs_mus = [3.4]
    mus, sigmas, weights = adaptive_parzen_estimator(
        obs_mus, low, high, prior_weight=0.5, equal_weight=False, flat_num=25
    )
    assert list(mus) == [2, 3.4]
    assert list(sigmas) == [6, 3]
    assert list(weights) == [0.5 / 1.5, 1.0 / 1.5]

    obs_mus = numpy.linspace(-1, 5, num=30, endpoint=False)
    mus, sigmas, weights = adaptive_parzen_estimator(
        obs_mus, low, high, prior_weight=1.0, equal_weight=False, flat_num=25
    )

    ramp = numpy.linspace(1.0 / 30, 1.0, num=30 - 25)
    full = numpy.ones(25 + 1)
    all_weights = numpy.concatenate([ramp, full])

    assert len(mus) == len(sigmas) == len(weights) == 30 + 1
    assert n.all(weights[: 30 - 25] == ramp / all_weights.sum())
    assert n.all(weights[30 - 25 :] == 1 / all_weights.sum())
    assert n.all(sigmas == 6 / 10)
//  [python]
def test_adaptive_parzen_normal_estimator():
    """Test adaptive parzen estimator"""
    low = -1
    high = 5

    obs_mus = [1.2]
    mus, sigmas, weights = adaptive_parzen_estimator(
        obs_mus, low, high, prior_weight=1.0, equal_weight=False, flat_num=25
    )
    assert list(mus) == [1.2, 2]
    assert list(sigmas) == [3, 6]
    assert list(weights) == [1.0 / 2, 1.0 / 2]

    obs_mus = [3.4]
    mus, sigmas, weights = adaptive_parzen_estimator(
        obs_mus, low, high, prior_weight=0.5, equal_weight=False, flat_num=25
    )
    assert list(mus) == [2, 3.4]
    assert list(sigmas) == [6, 3]
    assert list(weights) == [0.5 / 1.5, 1.0 / 1.5]

    obs_mus = numpy.linspace(-1, 5, num=30, endpoint=False)
    mus, sigmas, weights = adaptive_parzen_estimator(
        obs_mus, low, high, prior_weight=1.0, equal_weight=False, flat_num=25
    )

    ramp = numpy.linspace(1.0 / 30, 1.0, num=30 - 25)
    full = numpy.ones(25 + 1)
    all_weights = numpy.concatenate([ramp, full])

    assert len(mus) == len(sigmas) == len(weights) == 30 + 1
    assert numpy.all(weights[: 30 - 25] == ramp / all_weights.sum())
    assert numpy.all(weights[30 - 25 :] == 1 / all_weights.sum())
    assert numpy.all(sigmas == 6 / 10)
//  [python]
def test_get_loglikelis(self, tpe: TPE):
        """Test to get log likelis of points"""
        obs = numpy.random.randint(0, 10, 100)
        choices = ["a", "b", 11, 15, 17, 18, 19, 20, 25, "c"]
        cat_sampler = CategoricalSampler(tpe, obs, choices)

        points = cat_sampler.sample(25)

        likelis = cat_sampler.get_loglikelis(points)

        assert n.all(
            likelis == n.log(n.asarray(cat_sampler.weights)[points])
        )
//  [python]
def test_get_loglikelis(self, tpe: TPE):
        """Test to get log likelis of points"""
        obs = numpy.random.randint(0, 10, 100)
        choices = ["a", "b", 11, 15, 17, 18, 19, 20, 25, "c"]
        cat_sampler = CategoricalSampler(tpe, obs, choices)

        points = cat_sampler.sample(25)

        likelis = cat_sampler.get_loglikelis(points)

        assert numpy.all(
            likelis == numpy.log(numpy.asarray(cat_sampler.weights)[points])
        )
//  [python]
def test_sample(self, tpe: TPE, seed: int):
        """Test CategoricalSampler sample function"""
        rng = numpy.random.RandomState(seed)
        obs = rng.randint(0, 10, 100)
        choices = ["a", "b", 11, 15, 17, 18, 19, 20, 25, "c"]
        cat_sampler = CategoricalSampler(tpe, obs, choices)

        points = cat_sampler.sample(25)

        assert len(points) == 25
        assert n.all(points >= 0)
        assert n.all(points < 10)

        weights = numpy.linspace(1, 10, num=10) ** 3
        rng.shuffle(weights)
        weights = weights / weights.sum()
        cat_sampler = CategoricalSampler(tpe, obs, choices)
        cat_sampler.weights = weights

        points = cat_sampler.sample(10000)
        points = numpy.array(points)
        hist = numpy.bincount(points)

        assert n.all(hist.argsort() == weights.argsort())
        assert len(points) == 10000
        assert n.all(points >= 0)
        assert n.all(points < 10)
//  [python]
def test_sample(self, tpe: TPE, seed: int):
        """Test CategoricalSampler sample function"""
        rng = numpy.random.RandomState(seed)
        obs = rng.randint(0, 10, 100)
        choices = ["a", "b", 11, 15, 17, 18, 19, 20, 25, "c"]
        cat_sampler = CategoricalSampler(tpe, obs, choices)

        points = cat_sampler.sample(25)

        assert len(points) == 25
        assert numpy.all(points >= 0)
        assert numpy.all(points < 10)

        weights = numpy.linspace(1, 10, num=10) ** 3
        rng.shuffle(weights)
        weights = weights / weights.sum()
        cat_sampler = CategoricalSampler(tpe, obs, choices)
        cat_sampler.weights = weights

        points = cat_sampler.sample(10000)
        points = numpy.array(points)
        hist = numpy.bincount(points)

        assert numpy.all(hist.argsort() == weights.argsort())
        assert len(points) == 10000
        assert numpy.all(points >= 0)
        assert numpy.all(points < 10)
//  [python]
def test_cat_sampler_creation(self, tpe: TPE):
        """Test CategoricalSampler creation"""
        obs: list | numpy.ndarray = [0, 3, 9]
        choices = list(range(-5, 5))
        cat_sampler = CategoricalSampler(tpe, obs, choices)
        assert len(cat_sampler.weights) == len(choices)

        obs = numpy.array([0, 3, 9])
        choices = ["a", "b", 11, 15, 17, 18, 19, 20, 25, "c"]
        cat_sampler = CategoricalSampler(tpe, obs, choices)

        assert len(cat_sampler.weights) == len(choices)

        tpe.equal_weight = True
        tpe.prior_weight = 1.0
        obs = numpy.random.randint(0, 10, 100)
        cat_sampler = CategoricalSampler(tpe, obs, choices)
        counts_obs = numpy.bincount(obs) + 1.0
        weights = counts_obs / counts_obs.sum()

        assert n.all(cat_sampler.weights == weights)

        tpe.equal_weight = False
        tpe.prior_weight = 0.5
        tpe.full_weight_num = 30
        obs = numpy.random.randint(0, 10, 100)

        cat_sampler = CategoricalSampler(tpe, obs, choices)

        ramp = numpy.linspace(1.0 / 100, 1.0, num=100 - 30)
        full = numpy.ones(30)
        ramp_weights = numpy.concatenate([ramp, full])

        counts_obs = numpy.bincount(obs, weights=ramp_weights) + 0.5
        weights = counts_obs / counts_obs.sum()

        assert n.all(cat_sampler.weights == weights)
//  [python]
def test_cat_sampler_creation(self, tpe: TPE):
        """Test CategoricalSampler creation"""
        obs: list | numpy.ndarray = [0, 3, 9]
        choices = list(range(-5, 5))
        cat_sampler = CategoricalSampler(tpe, obs, choices)
        assert len(cat_sampler.weights) == len(choices)

        obs = numpy.array([0, 3, 9])
        choices = ["a", "b", 11, 15, 17, 18, 19, 20, 25, "c"]
        cat_sampler = CategoricalSampler(tpe, obs, choices)

        assert len(cat_sampler.weights) == len(choices)

        tpe.equal_weight = True
        tpe.prior_weight = 1.0
        obs = numpy.random.randint(0, 10, 100)
        cat_sampler = CategoricalSampler(tpe, obs, choices)
        counts_obs = numpy.bincount(obs) + 1.0
        weights = counts_obs / counts_obs.sum()

        assert numpy.all(cat_sampler.weights == weights)

        tpe.equal_weight = False
        tpe.prior_weight = 0.5
        tpe.full_weight_num = 30
        obs = numpy.random.randint(0, 10, 100)

        cat_sampler = CategoricalSampler(tpe, obs, choices)

        ramp = numpy.linspace(1.0 / 100, 1.0, num=100 - 30)
        full = numpy.ones(30)
        ramp_weights = numpy.concatenate([ramp, full])

        counts_obs = numpy.bincount(obs, weights=ramp_weights) + 0.5
        weights = counts_obs / counts_obs.sum()

        assert numpy.all(cat_sampler.weights == weights)
//  [python]
def test_adaptive_parzen_normal_estimator_sigma_clip():
    """Test that the magic clip of sigmas for parzen estimator"""
    low = -1
    high = 5

    obs_mus = numpy.linspace(-1, 5, num=8, endpoint=False)
    mus, sigmas, weights = adaptive_parzen_estimator(
        obs_mus, low, high, prior_weight=1.0, equal_weight=False, flat_num=25
    )
    assert len(mus) == len(sigmas) == len(weights) == 8 + 1
    assert n.all(weights == 1 / 9)
    assert n.all(sigmas == 6 / 8)

    obs_mus = numpy.random.uniform(-1, 5, 30)
    mus, sigmas, weights = adaptive_parzen_estimator(
        obs_mus, low, high, prior_weight=1.0, equal_weight=False, flat_num=25
    )

    assert len(mus) == len(sigmas) == len(weights) == 30 + 1
    assert n.all(weights[-25:] == weights[-1])
    assert n.all(sigmas <= 6) and n.all(sigmas >= 6 / 10)

    obs_mus = numpy.random.uniform(-1, 5, 400)
    mus, sigmas, weights = adaptive_parzen_estimator(
        obs_mus, low, high, prior_weight=1.0, equal_weight=False, flat_num=25
    )

    assert len(mus) == len(sigmas) == len(weights) == 400 + 1
    assert n.all(weights[-25:] == weights[-1])
    assert n.all(sigmas <= 6) and n.all(sigmas >= 6 / 20)

    obs_mus = numpy.random.uniform(-1, 5, 10000)
    mus, sigmas, weights = adaptive_parzen_estimator(
        obs_mus, low, high, prior_weight=1.0, equal_weight=False, flat_num=25
    )

    assert len(mus) == len(sigmas) == len(weights) == 10000 + 1
    assert n.all(weights[-25:] == weights[-1])
    assert n.all(sigmas <= 6) and n.all(sigmas >= 6 / 100)
//  [python]
def test_adaptive_parzen_normal_estimator_sigma_clip():
    """Test that the magic clip of sigmas for parzen estimator"""
    low = -1
    high = 5

    obs_mus = numpy.linspace(-1, 5, num=8, endpoint=False)
    mus, sigmas, weights = adaptive_parzen_estimator(
        obs_mus, low, high, prior_weight=1.0, equal_weight=False, flat_num=25
    )
    assert len(mus) == len(sigmas) == len(weights) == 8 + 1
    assert numpy.all(weights == 1 / 9)
    assert numpy.all(sigmas == 6 / 8)

    obs_mus = numpy.random.uniform(-1, 5, 30)
    mus, sigmas, weights = adaptive_parzen_estimator(
        obs_mus, low, high, prior_weight=1.0, equal_weight=False, flat_num=25
    )

    assert len(mus) == len(sigmas) == len(weights) == 30 + 1
    assert numpy.all(weights[-25:] == weights[-1])
    assert numpy.all(sigmas <= 6) and numpy.all(sigmas >= 6 / 10)

    obs_mus = numpy.random.uniform(-1, 5, 400)
    mus, sigmas, weights = adaptive_parzen_estimator(
        obs_mus, low, high, prior_weight=1.0, equal_weight=False, flat_num=25
    )

    assert len(mus) == len(sigmas) == len(weights) == 400 + 1
    assert numpy.all(weights[-25:] == weights[-1])
    assert numpy.all(sigmas <= 6) and numpy.all(sigmas >= 6 / 20)

    obs_mus = numpy.random.uniform(-1, 5, 10000)
    mus, sigmas, weights = adaptive_parzen_estimator(
        obs_mus, low, high, prior_weight=1.0, equal_weight=False, flat_num=25
    )

    assert len(mus) == len(sigmas) == len(weights) == 10000 + 1
    assert numpy.all(weights[-25:] == weights[-1])
    assert numpy.all(sigmas <= 6) and numpy.all(sigmas >= 6 / 100)
//  [python]
def total_thickness(self) -> np.ndarray:
        is_all_nan = n.all(n.isnan(self.thickness))
        if is_all_nan:
            thickness = nonlinear_thickness(self.property_id, self.allowed_properties)
        else:
            thickness = self.thickness
        return thickness
//  [python]
def total_thickness(self) -> np.ndarray:
        is_all_nan = np.all(np.isnan(self.thickness))
        if is_all_nan:
            thickness = nonlinear_thickness(self.property_id, self.allowed_properties)
        else:
            thickness = self.thickness
        return thickness
//  [python]
def test_adaptive_parzen_normal_estimator_weight():
    """Test the weight for the normal components"""
    obs_mus = numpy.linspace(-1, 5, num=30, endpoint=False)
    low = -1
    high = 5

    # equal weight
    mus, sigmas, weights = adaptive_parzen_estimator(
        obs_mus, low, high, prior_weight=1.0, equal_weight=True, flat_num=25
    )
    assert n.all(weights == 1 / 31)
    assert n.all(sigmas == 6 / 10)

    # prior weight
    mus, sigmas, weights = adaptive_parzen_estimator(
        obs_mus, low, high, prior_weight=0.5, equal_weight=False, flat_num=25
    )

    ramp = numpy.linspace(1.0 / 30, 1.0, num=30 - 25)
    full = numpy.ones(25 + 1)
    all_weights = numpy.concatenate([ramp, full])
    prior_pos = numpy.searchsorted(mus, 2)
    all_weights[prior_pos] = 0.5

    assert n.all(
        weights[: 30 - 25]
        == (numpy.linspace(1.0 / 30, 1.0, num=30 - 25) / all_weights.sum())
    )
    assert n.all(weights[33 - 25 : prior_pos] == 1 / all_weights.sum())
    assert weights[prior_pos] == 0.5 / all_weights.sum()
    assert n.all(weights[prior_pos + 1 :] == 1 / all_weights.sum())
    assert n.all(sigmas == 6 / 10)

    # full weights number
    mus, sigmas, weights = adaptive_parzen_estimator(
        obs_mus, low, high, prior_weight=1.0, equal_weight=False, flat_num=15
    )

    ramp = numpy.linspace(1.0 / 30, 1.0, num=30 - 15)
    full = numpy.ones(15 + 1)
    all_weights = numpy.concatenate([ramp, full])
    prior_pos = numpy.searchsorted(mus, 2)
    all_weights[prior_pos] = 1.0

    assert n.all(
        weights[: 30 - 15]
        == (numpy.linspace(1.0 / 30, 1.0, num=30 - 15) / all_weights.sum())
    )
    assert n.all(weights[30 - 15 :] == 1 / all_weights.sum())
    assert n.all(sigmas == 6 / 10)
//  [python]
def test_adaptive_parzen_normal_estimator_weight():
    """Test the weight for the normal components"""
    obs_mus = numpy.linspace(-1, 5, num=30, endpoint=False)
    low = -1
    high = 5

    # equal weight
    mus, sigmas, weights = adaptive_parzen_estimator(
        obs_mus, low, high, prior_weight=1.0, equal_weight=True, flat_num=25
    )
    assert numpy.all(weights == 1 / 31)
    assert numpy.all(sigmas == 6 / 10)

    # prior weight
    mus, sigmas, weights = adaptive_parzen_estimator(
        obs_mus, low, high, prior_weight=0.5, equal_weight=False, flat_num=25
    )

    ramp = numpy.linspace(1.0 / 30, 1.0, num=30 - 25)
    full = numpy.ones(25 + 1)
    all_weights = numpy.concatenate([ramp, full])
    prior_pos = numpy.searchsorted(mus, 2)
    all_weights[prior_pos] = 0.5

    assert numpy.all(
        weights[: 30 - 25]
        == (numpy.linspace(1.0 / 30, 1.0, num=30 - 25) / all_weights.sum())
    )
    assert numpy.all(weights[33 - 25 : prior_pos] == 1 / all_weights.sum())
    assert weights[prior_pos] == 0.5 / all_weights.sum()
    assert numpy.all(weights[prior_pos + 1 :] == 1 / all_weights.sum())
    assert numpy.all(sigmas == 6 / 10)

    # full weights number
    mus, sigmas, weights = adaptive_parzen_estimator(
        obs_mus, low, high, prior_weight=1.0, equal_weight=False, flat_num=15
    )

    ramp = numpy.linspace(1.0 / 30, 1.0, num=30 - 15)
    full = numpy.ones(15 + 1)
    all_weights = numpy.concatenate([ramp, full])
    prior_pos = numpy.searchsorted(mus, 2)
    all_weights[prior_pos] = 1.0

    assert numpy.all(
        weights[: 30 - 15]
        == (numpy.linspace(1.0 / 30, 1.0, num=30 - 15) / all_weights.sum())
    )
    assert numpy.all(weights[30 - 15 :] == 1 / all_weights.sum())
    assert numpy.all(sigmas == 6 / 10)
//  [python]
def test_sample(self, tpe: TPE):
        """Test GMMSampler sample function"""
        mus = numpy.linspace(-3, 3, num=12, endpoint=False)
        sigmas = [0.5] * 12

        gmm_sampler = GMMSampler(tpe, mus, sigmas, -3, 3)
        points = gmm_sampler.sample(25)
        points = numpy.array(points)

        assert len(points) <= 25
        assert n.all(points >= -3)
        assert n.all(points < 3)

        mus = numpy.linspace(-10, 10, num=10, endpoint=False)
        sigmas = [0.00001] * 10
        weights = numpy.linspace(1, 10, num=10) ** 3
        numpy.random.shuffle(weights)
        weights = weights / weights.sum()

        gmm_sampler = GMMSampler(tpe, mus, sigmas, -11, 9, weights)
        points = gmm_sampler.sample(10000)
        points = numpy.array(points)
        hist = numpy.histogram(points, bins=[-11, -9, -7, -5, -3, -1, 1, 3, 5, 7, 9])

        assert n.all(hist[0].argsort() == numpy.array(weights).argsort())
        assert n.all(points >= -11)
        assert n.all(points < 9)
//  [python]
def test_sample(self, tpe: TPE):
        """Test GMMSampler sample function"""
        mus = numpy.linspace(-3, 3, num=12, endpoint=False)
        sigmas = [0.5] * 12

        gmm_sampler = GMMSampler(tpe, mus, sigmas, -3, 3)
        points = gmm_sampler.sample(25)
        points = numpy.array(points)

        assert len(points) <= 25
        assert numpy.all(points >= -3)
        assert numpy.all(points < 3)

        mus = numpy.linspace(-10, 10, num=10, endpoint=False)
        sigmas = [0.00001] * 10
        weights = numpy.linspace(1, 10, num=10) ** 3
        numpy.random.shuffle(weights)
        weights = weights / weights.sum()

        gmm_sampler = GMMSampler(tpe, mus, sigmas, -11, 9, weights)
        points = gmm_sampler.sample(10000)
        points = numpy.array(points)
        hist = numpy.histogram(points, bins=[-11, -9, -7, -5, -3, -1, 1, 3, 5, 7, 9])

        assert numpy.all(hist[0].argsort() == numpy.array(weights).argsort())
        assert numpy.all(points >= -11)
        assert numpy.all(points < 9)
//  [python]
def reduce_all(input_tensor, axis=None):
    if not SWITCH_ON or has_tensor(input_tensor):
        if axis is None:
            input_tensor = [znp.reshape(ar, (-1,)) for ar in tf.nest.flatten(input_tensor)]
        return znp.all(input_tensor, axis)
    out = np.all(input_tensor, axis)
    if out.shape == (1,):
        out = out[0]
    return out
//  [python]
def test_period_list_empty():
    """Tests an empty PeriodList."""
    instance = PeriodList.empty()
    assert len(instance.periods) == 0

    other = pickle.loads(pickle.dumps(instance))
    assert len(other.periods) == 0

    assert len(instance.join_adjacent_periods()) == 0
    assert len(instance.merge(instance)) == 0

    assert len(instance.filter(numpy.timedelta64(1, 'D'))) == 0

    assert numpy.sum(instance.duration()) == 0

    start = numpy.datetime64('2018-01-01', 'ns')
    end = numpy.datetime64('2018-01-02', 'ns')

    duration = numpy.timedelta64(1, 'h')
    samples = (end - start) // duration

    periods = []

    for ix in range(samples):
        periods.append(
            (start + ix * duration,
             start + (ix + 1) * duration - numpy.timedelta64(1, 's')))
    periods = PeriodList(numpy.array(periods).T)

    assert n.all(instance.merge(periods).periods == periods.periods)

    instance = PeriodList.empty()

    assert len(instance.sort()) == 0
//  [python]
def test_period_list_empty():
    """Tests an empty PeriodList."""
    instance = PeriodList.empty()
    assert len(instance.periods) == 0

    other = pickle.loads(pickle.dumps(instance))
    assert len(other.periods) == 0

    assert len(instance.join_adjacent_periods()) == 0
    assert len(instance.merge(instance)) == 0

    assert len(instance.filter(numpy.timedelta64(1, 'D'))) == 0

    assert numpy.sum(instance.duration()) == 0

    start = numpy.datetime64('2018-01-01', 'ns')
    end = numpy.datetime64('2018-01-02', 'ns')

    duration = numpy.timedelta64(1, 'h')
    samples = (end - start) // duration

    periods = []

    for ix in range(samples):
        periods.append(
            (start + ix * duration,
             start + (ix + 1) * duration - numpy.timedelta64(1, 's')))
    periods = PeriodList(numpy.array(periods).T)

    assert numpy.all(instance.merge(periods).periods == periods.periods)

    instance = PeriodList.empty()

    assert len(instance.sort()) == 0
//  [python]
def _adr_changed_cb(self, trigger_name, changes):
        mgr, changes = changes
        change_types = set(changes.keys())
        from chimerax.atomic import concatenate
        if 'created' in change_types:
            created = changes['created']
            created = created[created.sim_indices == -1]
            # add only restraints with both atoms in the sim
            all_atoms = self.sim_construct.all_atoms
            indices = numpy.array([all_atoms.indices(atoms) for atoms in created.atoms])
            created = created[numpy.all(indices != -1, axis=0)]
            self.sim_handler.add_adaptive_distance_restraints(created)
        changeds = []
        for reason in self._adr_update_reasons.intersection(change_types):
            changeds.append(changes[reason])
        if len(changeds):
            all_changeds = concatenate(changeds, remove_duplicates=True)
            all_changeds = all_changeds[all_changeds.sim_indices != 1]
            self.sim_handler.update_adaptive_distance_restraints(all_changeds)
//  [python]
def _dr_changed_cb(self, trigger_name, changes):
        mgr, changes = changes
        change_types = set(changes.keys())
        from chimerax.atomic import concatenate
        if 'created' in change_types:
            # avoid double counting
            created = changes['created']
            created = created[created.sim_indices == -1]
            # add only restraints with both atoms in the sim
            all_atoms = self.sim_construct.all_atoms
            indices = numpy.array([all_atoms.indices(atoms) for atoms in created.atoms])
            created = created[numpy.all(indices != -1, axis=0)]
            self.sim_handler.add_distance_restraints(created)
        changeds = []
        for reason in self._dr_update_reasons.intersection(change_types):
            changeds.append(changes[reason])
        if len(changeds):
            all_changeds = concatenate(changeds, remove_duplicates=True)
            all_changeds = all_changeds[all_changeds.sim_indices != -1]
            self.sim_handler.update_distance_restraints(all_changeds)
//  [python]
def _dihe_r_changed_cb(self, trigger_name, changes):
        '''Used for proper dihedral and chiral restraints.'''
        mgr, changes = changes
        change_types = list(changes.keys())
        from chimerax.atomic import concatenate
        changeds = []
        if 'created' in change_types:
            # avoid double counting
            created = changes['created']
            created = created[created.sim_indices == -1]
            # add only restraints with all atoms in the sim
            all_atoms = self.sim_construct.all_atoms
            indices = numpy.array([all_atoms.indices(atoms) for atoms in created.atoms])
            created = created[numpy.all(indices != -1, axis=0)]
            self.sim_handler.add_dihedral_restraints(created)
        if 'target changed' in change_types:
            changeds.append(changes['target changed'])
        if 'enabled/disabled' in change_types:
            changeds.append(changes['enabled/disabled'])
        if 'spring constant changed' in change_types:
            changeds.append(changes['spring constant changed'])
        if len(changeds):
            all_changeds = concatenate(changeds, remove_duplicates=True)
            all_changeds = all_changeds[all_changeds.sim_indices != -1]
            self.sim_handler.update_dihedral_restraints(all_changeds)
//  [python]
def add_amber_cmap_torsions(self, ramas):
        '''
        Add CMAP correction terms for AMBER force field.

        Args:
            * ramas:
                - a :py:class:`Ramas` instance covering all mobile residues
        '''
        cf = self._amber_cmap_force
        sc = self._atoms
        valid_ramas = ramas[ramas.valids]
        resnames = valid_ramas.residues.names
        phi_atoms = valid_ramas.phi_dihedrals.atoms
        psi_atoms = valid_ramas.psi_dihedrals.atoms
        phi_indices = numpy.column_stack([sc.indices(atoms) for atoms in phi_atoms])
        phi_filter = numpy.alltrue(phi_indices!=-1, axis=1)
        psi_indices = numpy.column_stack([sc.indices(atoms) for atoms in psi_atoms])
        psi_filter = numpy.alltrue(psi_indices!=-1, axis=1)
        combined_filter = numpy.logical_and(phi_filter, psi_filter)
        resnames = resnames[combined_filter]
        phi_indices = phi_indices[combined_filter]
        psi_indices = psi_indices[combined_filter]
        cf.add_torsions(resnames, phi_indices, psi_indices)
//  [python]
def add_amber_cmap_torsions(self, ramas):
        '''
        Add CMAP correction terms for AMBER force field.

        Args:
            * ramas:
                - a :py:class:`Ramas` instance covering all mobile residues
        '''
        cf = self._amber_cmap_force
        sc = self._atoms
        valid_ramas = ramas[ramas.valids]
        resnames = valid_ramas.residues.names
        phi_atoms = valid_ramas.phi_dihedrals.atoms
        psi_atoms = valid_ramas.psi_dihedrals.atoms
        phi_indices = numpy.column_stack([sc.indices(atoms) for atoms in phi_atoms])
        phi_filter = numpy.all(phi_indices!=-1, axis=1)
        psi_indices = numpy.column_stack([sc.indices(atoms) for atoms in psi_atoms])
        psi_filter = numpy.all(psi_indices!=-1, axis=1)
        combined_filter = numpy.logical_and(phi_filter, psi_filter)
        resnames = resnames[combined_filter]
        phi_indices = phi_indices[combined_filter]
        psi_indices = psi_indices[combined_filter]
        cf.add_torsions(resnames, phi_indices, psi_indices)
//  [python]
def _adaptive_dihe_r_changed_cb(self, trigger_name, changes):
        '''Used for all forms of dihedral restraints.'''
        mgr, changes = changes
        change_types = list(changes.keys())
        from chimerax.atomic import concatenate
        changeds = []
        if 'created' in change_types:
            # avoid double counting
            created = changes['created']
            created = created[created.sim_indices == -1]
            # add only restraints with all atoms in the sim
            all_atoms = self.sim_construct.all_atoms
            indices = numpy.array([all_atoms.indices(atoms) for atoms in created.atoms])
            created = created[numpy.all(indices != -1, axis=0)]
            self.sim_handler.add_adaptive_dihedral_restraints(created)
        if 'target changed' in change_types:
            changeds.append(changes['target changed'])
        if 'enabled/disabled' in change_types:
            changeds.append(changes['enabled/disabled'])
        if 'spring constant changed' in change_types:
            changeds.append(changes['spring constant changed'])
        if 'adaptive restraint constant changed' in change_types:
            changeds.append(changes['adaptive restraint constant changed'])
        if len(changeds):
            all_changeds = concatenate(changeds, remove_duplicates=True)
            all_changeds = all_changeds[all_changeds.sim_indices != -1]
            self.sim_handler.update_adaptive_dihedral_restraints(all_changeds)
//  [python]
def add_dihedral_restraints(self, restraints):
        '''
        Add a set of dihedral restraints to the simulation. This sets the
        :attr:`sim_index` for each restraint so it knows its own place in the
        simulation for later updating purposes. Automatically calls
        :func:`context_reinit_needed()`.

        Args:
            * restraints:
                - Either a :py:class:`ProperDihedralRestraints` or a
                  :py:class:`ChiralRestraints`.
        '''
        force = self._dihedral_restraint_force
        all_atoms = self._atoms
        dihedral_atoms = restraints.dihedrals.atoms
        atom_indices = numpy.array([all_atoms.indices(atoms) for atoms in dihedral_atoms])
        # Filter out those which don't have all atoms in the simulation
        ifilter = numpy.all(atom_indices!=-1, axis=0)
        atom_indices = [a[ifilter] for a in atom_indices]
        #atom_indices = atom_indices[ifilter]
        restraints = restraints[ifilter]
        restraints.sim_indices = force.add_torsions(atom_indices,
            restraints.enableds, restraints.spring_constants, restraints.targets, restraints.cutoffs)
        self.context_reinit_needed()
//  [python]
def add_distance_restraints(self, restraints):
        '''
        Add a set of distance restraints to the simulation. Sets
        :attr:`sim_index` for each restraint so it knows its place in the
        simulation. Automatically calls :func:`context_reinit_needed`

        Args:
            * restraints:
                - a :py:class:`DistanceRestraints` instance
        '''
        force = self._distance_restraints_force
        all_atoms = self._atoms
        dr_atoms = restraints.atoms
        indices = numpy.array([all_atoms.indices(atoms) for atoms in dr_atoms])
        ifilter = numpy.all(indices!=-1, axis=0)
        indices = [i[ifilter] for i in indices]
        restraints = restraints[ifilter]
        restraints.sim_indices = force.add_bonds(indices,
            restraints.enableds, restraints.spring_constants, restraints.targets/10)
        self.context_reinit_needed()