jiahuimbzuai/CoQuIR huggingface dataset:
//  [python]
def predict_2step(h_decision, h_classif, min_t, data):
    n_test, n_t = data.shape[:2]
    predicted = numpy.zeros((n_test,), dtype=numpy.int32) - 1
    tau = numpy.zeros((n_test,), dtype=numpy.int32) - 1
    for t in range(min_t, n_t):
        if type(h_decision[t]) == numpy.int32:
            if h_decision[t] == 0:
                continue
            else:
                for idx in range(n_test):
                    if predicted[idx] < 0:
                        predicted[idx] = h_classif[t].predict(data[idx, :t])
                        tau[idx] = t
        else:
            dec = h_decision[t].predict(data[:, :t])
            for idx in range(n_test):
                if dec[idx] == 1 and predicted[idx] < 0:
                    predicted[idx] = h_classif[t].predict(data[idx, :t])
                    tau[idx] = t
        if numpy.alltrue(predicted >= 0):
            break
    return predicted, tau
//  [python]
def predict_2step(h_decision, h_classif, min_t, data):
    n_test, n_t = data.shape[:2]
    predicted = numpy.zeros((n_test,), dtype=numpy.int32) - 1
    tau = numpy.zeros((n_test,), dtype=numpy.int32) - 1
    for t in range(min_t, n_t):
        if type(h_decision[t]) == numpy.int32:
            if h_decision[t] == 0:
                continue
            else:
                for idx in range(n_test):
                    if predicted[idx] < 0:
                        predicted[idx] = h_classif[t].predict(data[idx, :t])
                        tau[idx] = t
        else:
            dec = h_decision[t].predict(data[:, :t])
            for idx in range(n_test):
                if dec[idx] == 1 and predicted[idx] < 0:
                    predicted[idx] = h_classif[t].predict(data[idx, :t])
                    tau[idx] = t
        if numpy.all(predicted >= 0):
            break
    return predicted, tau
//  [python]
def test_generic_for_loop(self):
        def generic_for_loop(a: Any) -> Any:
            ret = []
            for i in a:
                ret.append(i)
            return ret
        x = matx.NDArray([1, 2, 3, 4], [2, 2], "int32")
        py_ret = generic_for_loop(x)
        self.assertTrue(numpy.alltrue(py_ret[0].numpy() == numpy.array([1, 2])))
        self.assertTrue(numpy.alltrue(py_ret[1].numpy() == numpy.array([3, 4])))

        tx_ret = matx.script(generic_for_loop)(x)
        self.assertTrue(numpy.alltrue(tx_ret[0].numpy() == numpy.array([1, 2])))
        self.assertTrue(numpy.alltrue(tx_ret[1].numpy() == numpy.array([3, 4])))
//  [python]
def test_generic_for_loop(self):
        def generic_for_loop(a: Any) -> Any:
            ret = []
            for i in a:
                ret.append(i)
            return ret
        x = matx.NDArray([1, 2, 3, 4], [2, 2], "int32")
        py_ret = generic_for_loop(x)
        self.assertTrue(numpy.all(py_ret[0].numpy() == numpy.array([1, 2])))
        self.assertTrue(numpy.all(py_ret[1].numpy() == numpy.array([3, 4])))

        tx_ret = matx.script(generic_for_loop)(x)
        self.assertTrue(numpy.all(tx_ret[0].numpy() == numpy.array([1, 2])))
        self.assertTrue(numpy.all(tx_ret[1].numpy() == numpy.array([3, 4])))
//  [python]
def test_ndarray_for_loop(self):
        def for_loop(a: matx.NDArray) -> Any:
            ret = []
            for i in a:
                ret.append(i)
            return ret
        x = matx.NDArray([1, 2, 3, 4], [2, 2], "int32")
        py_ret = for_loop(x)
        self.assertTrue(numpy.alltrue(py_ret[0].numpy() == numpy.array([1, 2])))
        self.assertTrue(numpy.alltrue(py_ret[1].numpy() == numpy.array([3, 4])))

        tx_ret = matx.script(for_loop)(x)
        self.assertTrue(numpy.alltrue(tx_ret[0].numpy() == numpy.array([1, 2])))
        self.assertTrue(numpy.alltrue(tx_ret[1].numpy() == numpy.array([3, 4])))
//  [python]
def test_ndarray_for_loop(self):
        def for_loop(a: matx.NDArray) -> Any:
            ret = []
            for i in a:
                ret.append(i)
            return ret
        x = matx.NDArray([1, 2, 3, 4], [2, 2], "int32")
        py_ret = for_loop(x)
        self.assertTrue(numpy.all(py_ret[0].numpy() == numpy.array([1, 2])))
        self.assertTrue(numpy.all(py_ret[1].numpy() == numpy.array([3, 4])))

        tx_ret = matx.script(for_loop)(x)
        self.assertTrue(numpy.all(tx_ret[0].numpy() == numpy.array([1, 2])))
        self.assertTrue(numpy.all(tx_ret[1].numpy() == numpy.array([3, 4])))
//  [python]
def equalVector(self, ar):
        diff = ar[1:] - ar[:-1]
        try:
            comp = numpy.alltrue(
                numpy.equal(
                    (self.delta) *
                    numpy.ones(
                        self.length -
                        1),
                    diff))
        except ValueError:
            return 0
        return comp
//  [python]
def equalVector(self, ar):
        diff = ar[1:] - ar[:-1]
        try:
            comp = numpy.all(
                numpy.equal(
                    (self.delta) *
                    numpy.ones(
                        self.length -
                        1),
                    diff))
        except ValueError:
            return 0
        return comp
//  [python]
def equal(self, axis):
        # Require that partitions (if any) are equal
        if self.partition is not None and axis.partition is not None:
            if len(self.partition) != len(axis.partition):
                return 0
            if not numpy.alltrue(numpy.equal(self.partition, axis.partition)):
                return 0
        elif self.partition is not None or axis.partition is not None:
            return 0

        if self.dataRepresent == axis.dataRepresent == CdVector:
            try:
                return numpy.alltrue(numpy.equal(self.data, axis.data))
            except ValueError:
                return 0
        elif self.dataRepresent == axis.dataRepresent == CdLinear:
            return self.data.equal(axis.data)
        elif self.dataRepresent == CdVector:
            return axis.data.equalVector(self.data)
        else:
            return self.data.equalVector(axis.data)
//  [python]
def equal(self, axis):
        # Require that partitions (if any) are equal
        if self.partition is not None and axis.partition is not None:
            if len(self.partition) != len(axis.partition):
                return 0
            if not numpy.all(numpy.equal(self.partition, axis.partition)):
                return 0
        elif self.partition is not None or axis.partition is not None:
            return 0

        if self.dataRepresent == axis.dataRepresent == CdVector:
            try:
                return numpy.all(numpy.equal(self.data, axis.data))
            except ValueError:
                return 0
        elif self.dataRepresent == axis.dataRepresent == CdLinear:
            return self.data.equal(axis.data)
        elif self.dataRepresent == CdVector:
            return axis.data.equalVector(self.data)
        else:
            return self.data.equalVector(axis.data)
//  [python]
def monotonicity(self):
        if self.dataRepresent == CdLinear:
            return self.data.monotonicity()
        elif self.length == 1:
            return CdSingleton
        else:
            first = self.data[:-1]
            second = self.data[1:]
            if numpy.alltrue(numpy.less(first, second)):
                return CdIncreasing
            elif numpy.alltrue(numpy.greater(first, second)):
                return CdDecreasing
            else:
                return CdNotMonotonic
//  [python]
def monotonicity(self):
        if self.dataRepresent == CdLinear:
            return self.data.monotonicity()
        elif self.length == 1:
            return CdSingleton
        else:
            first = self.data[:-1]
            second = self.data[1:]
            if numpy.all(numpy.less(first, second)):
                return CdIncreasing
            elif numpy.all(numpy.greater(first, second)):
                return CdDecreasing
            else:
                return CdNotMonotonic
//  [python]
def isClose(self, axis, eps):
        if eps == 0:
            return self.equal(axis)
        if self.dataRepresent == axis.dataRepresent == CdVector:
            try:
                return numpy.alltrue(numpy.less_equal(numpy.absolute(
                    self.data - axis.data), numpy.absolute(eps * self.data)))
            except ValueError:
                return 0
        elif self.dataRepresent == axis.dataRepresent == CdLinear:
            return self.data.isClose(axis.data, eps)
        elif self.dataRepresent == CdVector:
            return axis.data.isCloseVector(self.data, eps)
        else:
            return self.data.isCloseVector(axis.data, eps)
//  [python]
def isClose(self, axis, eps):
        if eps == 0:
            return self.equal(axis)
        if self.dataRepresent == axis.dataRepresent == CdVector:
            try:
                return numpy.all(numpy.less_equal(numpy.absolute(
                    self.data - axis.data), numpy.absolute(eps * self.data)))
            except ValueError:
                return 0
        elif self.dataRepresent == axis.dataRepresent == CdLinear:
            return self.data.isClose(axis.data, eps)
        elif self.dataRepresent == CdVector:
            return axis.data.isCloseVector(self.data, eps)
        else:
            return self.data.isCloseVector(axis.data, eps)
//  [python]
def isCloseVector(self, ar, eps):
        if eps == 0:
            return self.equalVector(ar)
        diff = ar[1:] - ar[:-1]
        diff2 = self.delta * numpy.ones(self.length - 1)
        try:
            comp = numpy.alltrue(
                numpy.less_equal(
                    numpy.absolute(
                        diff2 - diff),
                    numpy.absolute(
                        eps * diff2)))
        except ValueError:
            return 0
        return comp
//  [python]
def isCloseVector(self, ar, eps):
        if eps == 0:
            return self.equalVector(ar)
        diff = ar[1:] - ar[:-1]
        diff2 = self.delta * numpy.ones(self.length - 1)
        try:
            comp = numpy.all(
                numpy.less_equal(
                    numpy.absolute(
                        diff2 - diff),
                    numpy.absolute(
                        eps * diff2)))
        except ValueError:
            return 0
        return comp
//  [python]
def sanity_check(self):
        """Check for good input.

        @raise ValueError: if coupler1 or coupler2 or line have incompatible wl.

        """

        self.coupler1.sanity_check()
        self.coupler2.sanity_check()
        self.line.sanity_check()
        if not (
            numpy.alltrue(self.coupler1.wl == self.line.wl)
            and numpy.alltrue(self.coupler1.wl == self.coupler2.wl)
        ):
            raise ValueError("incompatible wl")
//  [python]
def sanity_check(self):
        """Check for good input.

        @raise ValueError: if coupler1 or coupler2 or line have incompatible wl.

        """

        self.coupler1.sanity_check()
        self.coupler2.sanity_check()
        self.line.sanity_check()
        if not (
            numpy.all(self.coupler1.wl == self.line.wl)
            and numpy.all(self.coupler1.wl == self.coupler2.wl)
        ):
            raise ValueError("incompatible wl")
//  [python]
def has_converged(old_mu, new_mu):
    return np.alltrue(old_mu == new_mu)
//  [python]
def has_converged(old_mu, new_mu):
    return np.all(old_mu == new_mu)
//  [python]
def sanity_check(self):
        """Check for good input.

        @raise ValueError: if wl are incompatible.

        """

        self.coupler1.sanity_check()
        self.coupler2.sanity_check()
        self.line1.sanity_check()
        self.line2.sanity_check()
        if not (
            numpy.alltrue(self.coupler1.wl == self.coupler2.wl)
            and numpy.alltrue(self.coupler1.wl == self.line1.wl)
            and numpy.alltrue(self.coupler1.wl == self.line2.wl)
        ):
            raise ValueError("incompatible wl")
//  [python]
def sanity_check(self):
        """Check for good input.

        @raise ValueError: if wl are incompatible.

        """

        self.coupler1.sanity_check()
        self.coupler2.sanity_check()
        self.line1.sanity_check()
        self.line2.sanity_check()
        if not (
            numpy.all(self.coupler1.wl == self.coupler2.wl)
            and numpy.all(self.coupler1.wl == self.line1.wl)
            and numpy.all(self.coupler1.wl == self.line2.wl)
        ):
            raise ValueError("incompatible wl")
//  [python]
def sanity_check(self):
        """Check for good input.

        @raise ValueError: if wl or the number of couplers are incompatible.

        """

        for K in self.Ks:
            K.sanity_check()
            if not (numpy.alltrue(self.wl == K.wl)):
                raise ValueError("incompatible wl")
        if not (len(self.Ks) - 1 == len(self.neffs) == len(self.l1s) == len(self.l2s)):
            raise ValueError("number of couplers and number of rings do not match.")
//  [python]
def sanity_check(self):
        """Check for good input.

        @raise ValueError: if wl or the number of couplers are incompatible.

        """

        for K in self.Ks:
            K.sanity_check()
            if not (numpy.all(self.wl == K.wl)):
                raise ValueError("incompatible wl")
        if not (len(self.Ks) - 1 == len(self.neffs) == len(self.l1s) == len(self.l2s)):
            raise ValueError("number of couplers and number of rings do not match.")
//  [python]
def _isSymmetrical(matrix):
    return numpy.alltrue(numpy.alltrue(matrix == numpy.transpose(matrix)))
//  [python]
def _isSymmetrical(matrix):
    return numpy.all(numpy.all(matrix == numpy.transpose(matrix)))
//  [python]
def test_numpy_subclass_serialization():
    # Check that we can properly serialize subclasses of np.ndarray.
    class CustomNDArray(np.ndarray):
        def __new__(cls, input_array):
            array = np.asarray(input_array).view(cls)
            return array

    def serializer(obj):
        return {'numpy': obj.view(np.ndarray)}

    def deserializer(data):
        array = data['numpy'].view(CustomNDArray)
        return array

    context = pa.default_serialization_context()

    context.register_type(CustomNDArray, 'CustomNDArray',
                          custom_serializer=serializer,
                          custom_deserializer=deserializer)

    x = CustomNDArray(np.zeros(3))
    serialized = pa.serialize(x, context=context).to_buffer()
    new_x = pa.deserialize(serialized, context=context)
    assert type(new_x) == CustomNDArray
    assert np.alltrue(new_x.view(np.ndarray) == np.zeros(3))
//  [python]
def test_numpy_subclass_serialization():
    # Check that we can properly serialize subclasses of np.ndarray.
    class CustomNDArray(np.ndarray):
        def __new__(cls, input_array):
            array = np.asarray(input_array).view(cls)
            return array

    def serializer(obj):
        return {'numpy': obj.view(np.ndarray)}

    def deserializer(data):
        array = data['numpy'].view(CustomNDArray)
        return array

    context = pa.default_serialization_context()

    context.register_type(CustomNDArray, 'CustomNDArray',
                          custom_serializer=serializer,
                          custom_deserializer=deserializer)

    x = CustomNDArray(np.zeros(3))
    serialized = pa.serialize(x, context=context).to_buffer()
    new_x = pa.deserialize(serialized, context=context)
    assert type(new_x) == CustomNDArray
    assert np.all(new_x.view(np.ndarray) == np.zeros(3))
//  [python]
def PamlMatrixParser(f):
    """Parses a matrix of amino acid transition probabilities and amino acid
    frequencies in the format used by PAML and returns a symetric array in single
    letter alphabetical order and a dictionary of frequencies for use by
    substitution_model.EmpiricalProteinMatrix"""
    matrix = numpy.zeros([20,20], Float)
    next_number = numbers_in(f).next
    for row in range(1,20):
        for col in range(0, row):
            matrix[row,col] = matrix[col,row] = next_number()
            
    freqs = [next_number() for i in range(20)]
    total = sum(freqs)
    assert abs(total-1) < 0.001, freqs
    freqs = [freq/total for freq in freqs]
    
    matrix = numpy.take(matrix, reorder, 0)
    matrix = numpy.take(matrix, reorder, 1)
    
    assert numpy.alltrue(matrix == numpy.transpose(matrix))
    
    freqs = dict(zip(three_letter_order, freqs))
    
    return (matrix, freqs)
//  [python]
def PamlMatrixParser(f):
    """Parses a matrix of amino acid transition probabilities and amino acid
    frequencies in the format used by PAML and returns a symetric array in single
    letter alphabetical order and a dictionary of frequencies for use by
    substitution_model.EmpiricalProteinMatrix"""
    matrix = numpy.zeros([20,20], Float)
    next_number = numbers_in(f).next
    for row in range(1,20):
        for col in range(0, row):
            matrix[row,col] = matrix[col,row] = next_number()
            
    freqs = [next_number() for i in range(20)]
    total = sum(freqs)
    assert abs(total-1) < 0.001, freqs
    freqs = [freq/total for freq in freqs]
    
    matrix = numpy.take(matrix, reorder, 0)
    matrix = numpy.take(matrix, reorder, 1)
    
    assert numpy.all(matrix == numpy.transpose(matrix))
    
    freqs = dict(zip(three_letter_order, freqs))
    
    return (matrix, freqs)
//  [python]
def apply(self, data):
        # so that correlation matrix calculation doesn't crash
        for ch in data:
            if np.alltrue(ch == 0.0):
                ch[-1] += 0.00001

        data1 = data
        if data1.shape[1] > self.max_hz:
            data1 = Resample(self.max_hz).apply(data1)

        if self.scale_option == 'first_axis':
            data1 = StandardizeFirst().apply(data1)
        elif self.scale_option == 'last_axis':
            data1 = StandardizeLast().apply(data1)

        data1 = CorrelationMatrix().apply(data1)

        if self.with_eigen:
            w = Eigenvalues().apply(data1)

        out = []
        if self.with_corr:
            data1 = upper_right_triangle(data1)
            out.append(data1)
        if self.with_eigen:
            out.append(w)

        for d in out:
            assert d.ndim == 1

        return np.concatenate(out, axis=0)
//  [python]
def apply(self, data):
        # so that correlation matrix calculation doesn't crash
        for ch in data:
            if np.all(ch == 0.0):
                ch[-1] += 0.00001

        data1 = data
        if data1.shape[1] > self.max_hz:
            data1 = Resample(self.max_hz).apply(data1)

        if self.scale_option == 'first_axis':
            data1 = StandardizeFirst().apply(data1)
        elif self.scale_option == 'last_axis':
            data1 = StandardizeLast().apply(data1)

        data1 = CorrelationMatrix().apply(data1)

        if self.with_eigen:
            w = Eigenvalues().apply(data1)

        out = []
        if self.with_corr:
            data1 = upper_right_triangle(data1)
            out.append(data1)
        if self.with_eigen:
            out.append(w)

        for d in out:
            assert d.ndim == 1

        return np.concatenate(out, axis=0)
//  [python]
def __init__(self, alphabet, rate_matrix, **kw):
        """
         - rate_matrix: The instantaneous rate matrix
        """
        _ContinuousSubstitutionModel.__init__(self, alphabet, **kw)
    
        alphabet = self.getAlphabet()  # as may be altered by recode_gaps etc.
        N = len(alphabet)
        assert rate_matrix.shape == (N, N)
        assert numpy.alltrue(numpy.diagonal(rate_matrix) == 0)
        self._instantaneous_mask_f = rate_matrix * 1.0
        self._instantaneous_mask = (self._instantaneous_mask_f != 0.0)
        self.symmetric = _isSymmetrical(self._instantaneous_mask_f)
        self.parameter_order = []
        self.checkParamsExist()
//  [python]
def __init__(self, alphabet, rate_matrix, **kw):
        """
         - rate_matrix: The instantaneous rate matrix
        """
        _ContinuousSubstitutionModel.__init__(self, alphabet, **kw)
    
        alphabet = self.getAlphabet()  # as may be altered by recode_gaps etc.
        N = len(alphabet)
        assert rate_matrix.shape == (N, N)
        assert numpy.all(numpy.diagonal(rate_matrix) == 0)
        self._instantaneous_mask_f = rate_matrix * 1.0
        self._instantaneous_mask = (self._instantaneous_mask_f != 0.0)
        self.symmetric = _isSymmetrical(self._instantaneous_mask_f)
        self.parameter_order = []
        self.checkParamsExist()
//  [python]
def test_basic_code_block(self):
        code = '''
        x = 1
        d_x[d_idx] += s_x[s_idx] + x
        '''
        cb = BasicCodeBlock(code=code)
        expect = ['d_idx', 'd_x', 's_idx', 's_x', 'x']
        self.assert_seq_equal(cb.symbols, expect)
        self.assert_seq_equal(cb.src_arrays, ['s_x'])
        self.assert_seq_equal(cb.dest_arrays, ['d_x'])
        ctx = cb.context
        self.assertEqual(ctx.s_idx, 0)
        self.assertEqual(ctx.d_idx, 0)
        x = numpy.zeros(2, dtype=float)
        self.assertTrue(numpy.alltrue(ctx.d_x == x))
        self.assertTrue(numpy.alltrue(ctx.s_x == x))
//  [python]
def test_basic_code_block(self):
        code = '''
        x = 1
        d_x[d_idx] += s_x[s_idx] + x
        '''
        cb = BasicCodeBlock(code=code)
        expect = ['d_idx', 'd_x', 's_idx', 's_x', 'x']
        self.assert_seq_equal(cb.symbols, expect)
        self.assert_seq_equal(cb.src_arrays, ['s_x'])
        self.assert_seq_equal(cb.dest_arrays, ['d_x'])
        ctx = cb.context
        self.assertEqual(ctx.s_idx, 0)
        self.assertEqual(ctx.d_idx, 0)
        x = numpy.zeros(2, dtype=float)
        self.assertTrue(numpy.all(ctx.d_x == x))
        self.assertTrue(numpy.all(ctx.s_x == x))
//  [python]
def test_conversion_with_2_classes(self):
        # given
        prob = np.array([
            [0.1, 0.9],
            [0.7, 0.3],
            [0.8, 0.2],
            [0.2, 0.8],
        ])
        expected_one_hot = np.array([
            [0, 1],
            [1, 0],
            [1, 0],
            [0, 1],
        ])

        # when
        result_one_hot = convert_prob2one_hot(prob)

        # then
        assert np.alltrue(result_one_hot == expected_one_hot)
//  [python]
def test_conversion_with_2_classes(self):
        # given
        prob = np.array([
            [0.1, 0.9],
            [0.7, 0.3],
            [0.8, 0.2],
            [0.2, 0.8],
        ])
        expected_one_hot = np.array([
            [0, 1],
            [1, 0],
            [1, 0],
            [0, 1],
        ])

        # when
        result_one_hot = convert_prob2one_hot(prob)

        # then
        assert np.all(result_one_hot == expected_one_hot)
//  [python]
def test_conversion_with_3_classes(self):
        # given
        prob = np.array([
            [0.3, 0.3, 0.4],
            [0.7, 0.1, 0.2],
            [0.8, 0.1, 0.1],
            [0.2, 0.8, 0.0]
        ])
        expected_one_hot = np.array([
            [0, 0, 1],
            [1, 0, 0],
            [1, 0, 0],
            [0, 1, 0]
        ])

        # when
        result_one_hot = convert_prob2one_hot(prob)

        # then
        assert np.alltrue(result_one_hot == expected_one_hot)
//  [python]
def test_conversion_with_3_classes(self):
        # given
        prob = np.array([
            [0.3, 0.3, 0.4],
            [0.7, 0.1, 0.2],
            [0.8, 0.1, 0.1],
            [0.2, 0.8, 0.0]
        ])
        expected_one_hot = np.array([
            [0, 0, 1],
            [1, 0, 0],
            [1, 0, 0],
            [0, 1, 0]
        ])

        # when
        result_one_hot = convert_prob2one_hot(prob)

        # then
        assert np.all(result_one_hot == expected_one_hot)
//  [python]
def test_sdf_file_parser_return_is_successful(sdf_file_long, mols):
    """test `labels` option and retain_smiles=True."""
    preprocessor = NFPPreprocessor(max_atoms=10)
    parser = SDFFileParser(preprocessor)
    result = parser.parse(sdf_file_long,
                          return_smiles=True, return_is_successful=True)

    dataset = result['dataset']
    # smiles = result['smiles']
    assert len(dataset) == 3
    is_successful = result['is_successful']
    assert len(is_successful) == 5
    assert numpy.alltrue(is_successful[[1, 3, 4]])
    assert numpy.alltrue(~is_successful[[0, 2]])

    # We assume NFPPreprocessor works as documented.
    for i in range(3):
        expect = preprocessor.get_input_features(mols[i])
        check_input_features(dataset[i], expect)
//  [python]
def test_sdf_file_parser_return_is_successful(sdf_file_long, mols):
    """test `labels` option and retain_smiles=True."""
    preprocessor = NFPPreprocessor(max_atoms=10)
    parser = SDFFileParser(preprocessor)
    result = parser.parse(sdf_file_long,
                          return_smiles=True, return_is_successful=True)

    dataset = result['dataset']
    # smiles = result['smiles']
    assert len(dataset) == 3
    is_successful = result['is_successful']
    assert len(is_successful) == 5
    assert numpy.all(is_successful[[1, 3, 4]])
    assert numpy.all(~is_successful[[0, 2]])

    # We assume NFPPreprocessor works as documented.
    for i in range(3):
        expect = preprocessor.get_input_features(mols[i])
        check_input_features(dataset[i], expect)
//  [python]
def test_sdf_file_parser_target_index(sdf_file, mols):
    preprocessor = NFPPreprocessor()
    parser = SDFFileParser(preprocessor)
    result = parser.parse(sdf_file, return_smiles=True, target_index=[0, 2],
                          return_is_successful=True)
    dataset = result['dataset']
    smiles = result['smiles']
    assert len(dataset) == 2
    is_successful = result['is_successful']
    assert numpy.alltrue(is_successful)
    assert len(is_successful) == 2

    # As we want test SDFFileParser, we assume
    # NFPPreprocessor works as documented.
    expect = preprocessor.get_input_features(mols[0])
    check_input_features(dataset[0], expect)

    expect = preprocessor.get_input_features(mols[2])
    check_input_features(dataset[1], expect)

    # check smiles array
    assert type(smiles) == numpy.ndarray
    assert smiles.ndim == 1
    assert len(smiles) == len(dataset)
    assert smiles[0] == 'CN=C=O'
    assert smiles[1] == 'CC1=CC2CC(CC1)O2'
//  [python]
def test_sdf_file_parser_target_index(sdf_file, mols):
    preprocessor = NFPPreprocessor()
    parser = SDFFileParser(preprocessor)
    result = parser.parse(sdf_file, return_smiles=True, target_index=[0, 2],
                          return_is_successful=True)
    dataset = result['dataset']
    smiles = result['smiles']
    assert len(dataset) == 2
    is_successful = result['is_successful']
    assert numpy.all(is_successful)
    assert len(is_successful) == 2

    # As we want test SDFFileParser, we assume
    # NFPPreprocessor works as documented.
    expect = preprocessor.get_input_features(mols[0])
    check_input_features(dataset[0], expect)

    expect = preprocessor.get_input_features(mols[2])
    check_input_features(dataset[1], expect)

    # check smiles array
    assert type(smiles) == numpy.ndarray
    assert smiles.ndim == 1
    assert len(smiles) == len(dataset)
    assert smiles[0] == 'CN=C=O'
    assert smiles[1] == 'CC1=CC2CC(CC1)O2'
//  [python]
def test_conversion_with_2_classes(self):
        # given
        categories = np.array([0, 1, 1, 0, 1])
        expected_one_hot = np.array([
            [1, 0],
            [0, 1],
            [0, 1],
            [1, 0],
            [0, 1]
        ])

        # when
        result_one_hot = convert_categorical2one_hot(categories)

        # then
        assert np.alltrue(result_one_hot == expected_one_hot)
//  [python]
def test_conversion_with_2_classes(self):
        # given
        categories = np.array([0, 1, 1, 0, 1])
        expected_one_hot = np.array([
            [1, 0],
            [0, 1],
            [0, 1],
            [1, 0],
            [0, 1]
        ])

        # when
        result_one_hot = convert_categorical2one_hot(categories)

        # then
        assert np.all(result_one_hot == expected_one_hot)
//  [python]
def __init__(self, alphabet, predicates=None, scales=None, **kw):
        """
         - predicates: a dict of {name:predicate}. See cogent.evolve.predicate
         - scales: scale rules, dict with predicates
        """
        self._canned_predicates = None
        _ContinuousSubstitutionModel.__init__(self, alphabet, **kw)

        (predicate_masks, predicate_order) = self._adaptPredicates(predicates or [])

        # Check for redundancy in predicates, ie: 1 or more than combine
        # to be equivalent to 1 or more others, or the distance params.
        # Give a clearer error in simple cases like always false or true.
        for (name, matrix) in predicate_masks.items():
            if numpy.alltrue((matrix == 0).flat):
                raise ValueError("Predicate %s is always false." % name)
        predicates_plus_scale = predicate_masks.copy()
        predicates_plus_scale[None] = self._instantaneous_mask
        if self._do_scaling:
            for (name, matrix) in predicate_masks.items():
                if numpy.alltrue((matrix == self._instantaneous_mask).flat):
                    raise ValueError("Predicate %s is always true." % name)
            if redundancyInPredicateMasks(predicate_masks):
                raise ValueError("Redundancy in predicates.")
            if redundancyInPredicateMasks(predicates_plus_scale):
                raise ValueError("Some combination of predicates is"
                        " equivalent to the overall rate parameter.")
        else:
            if redundancyInPredicateMasks(predicate_masks):
                raise ValueError("Redundancy in predicates.")
            if redundancyInPredicateMasks(predicates_plus_scale):
                warnings.warn("do_scaling=True would be more efficient than"
                        " these overly general predicates")

        self.predicate_masks = predicate_masks
        self.parameter_order = []
        self.predicate_indices = []
        self.symmetric = _isSymmetrical(self._instantaneous_mask)
        for pred in predicate_order:
            mask = predicate_masks[pred]
            if not _isSymmetrical(mask):
                self.symmetric = False
            indices = numpy.nonzero(mask)
            assert numpy.alltrue(mask[indices] == 1)
            self.parameter_order.append(pred)
            self.predicate_indices.append(indices)
        if not self.symmetric:
            warnings.warn('Model not reversible')
        (self.scale_masks, scale_order) = self._adaptPredicates(scales or [])
        self.checkParamsExist()
//  [python]
def __init__(self, alphabet, predicates=None, scales=None, **kw):
        """
         - predicates: a dict of {name:predicate}. See cogent.evolve.predicate
         - scales: scale rules, dict with predicates
        """
        self._canned_predicates = None
        _ContinuousSubstitutionModel.__init__(self, alphabet, **kw)

        (predicate_masks, predicate_order) = self._adaptPredicates(predicates or [])

        # Check for redundancy in predicates, ie: 1 or more than combine
        # to be equivalent to 1 or more others, or the distance params.
        # Give a clearer error in simple cases like always false or true.
        for (name, matrix) in predicate_masks.items():
            if numpy.all((matrix == 0).flat):
                raise ValueError("Predicate %s is always false." % name)
        predicates_plus_scale = predicate_masks.copy()
        predicates_plus_scale[None] = self._instantaneous_mask
        if self._do_scaling:
            for (name, matrix) in predicate_masks.items():
                if numpy.all((matrix == self._instantaneous_mask).flat):
                    raise ValueError("Predicate %s is always true." % name)
            if redundancyInPredicateMasks(predicate_masks):
                raise ValueError("Redundancy in predicates.")
            if redundancyInPredicateMasks(predicates_plus_scale):
                raise ValueError("Some combination of predicates is"
                        " equivalent to the overall rate parameter.")
        else:
            if redundancyInPredicateMasks(predicate_masks):
                raise ValueError("Redundancy in predicates.")
            if redundancyInPredicateMasks(predicates_plus_scale):
                warnings.warn("do_scaling=True would be more efficient than"
                        " these overly general predicates")

        self.predicate_masks = predicate_masks
        self.parameter_order = []
        self.predicate_indices = []
        self.symmetric = _isSymmetrical(self._instantaneous_mask)
        for pred in predicate_order:
            mask = predicate_masks[pred]
            if not _isSymmetrical(mask):
                self.symmetric = False
            indices = numpy.nonzero(mask)
            assert numpy.all(mask[indices] == 1)
            self.parameter_order.append(pred)
            self.predicate_indices.append(indices)
        if not self.symmetric:
            warnings.warn('Model not reversible')
        (self.scale_masks, scale_order) = self._adaptPredicates(scales or [])
        self.checkParamsExist()
//  [python]
def test_conversion_with_3_classes(self):
        # given
        categories = np.array([0, 2, 1, 0, 1])
        expected_one_hot = np.array([
            [1, 0, 0],
            [0, 0, 1],
            [0, 1, 0],
            [1, 0, 0],
            [0, 1, 0]
        ])

        # when
        result_one_hot = convert_categorical2one_hot(categories)

        # then
        assert np.alltrue(result_one_hot == expected_one_hot)
//  [python]
def test_conversion_with_3_classes(self):
        # given
        categories = np.array([0, 2, 1, 0, 1])
        expected_one_hot = np.array([
            [1, 0, 0],
            [0, 0, 1],
            [0, 1, 0],
            [1, 0, 0],
            [0, 1, 0]
        ])

        # when
        result_one_hot = convert_categorical2one_hot(categories)

        # then
        assert np.all(result_one_hot == expected_one_hot)
//  [python]
def test_endian_bool_indexing(self):
        # Ticket #105
        a = np.arange(10., dtype='>f8')
        b = np.arange(10., dtype='<f8')
        xa = np.where((a > 2) & (a < 6))
        xb = np.where((b > 2) & (b < 6))
        ya = ((a > 2) & (a < 6))
        yb = ((b > 2) & (b < 6))
        assert_array_almost_equal(xa, ya.nonzero())
        assert_array_almost_equal(xb, yb.nonzero())
        assert_(np.alltrue(a[ya] > 0.5))
        assert_(np.alltrue(b[yb] > 0.5))
//  [python]
def test_endian_bool_indexing(self):
        # Ticket #105
        a = np.arange(10., dtype='>f8')
        b = np.arange(10., dtype='<f8')
        xa = np.where((a > 2) & (a < 6))
        xb = np.where((b > 2) & (b < 6))
        ya = ((a > 2) & (a < 6))
        yb = ((b > 2) & (b < 6))
        assert_array_almost_equal(xa, ya.nonzero())
        assert_array_almost_equal(xb, yb.nonzero())
        assert_(np.all(a[ya] > 0.5))
        assert_(np.all(b[yb] > 0.5))
//  [python]
def check_equal_numpy(x, y):
    """
    Return True iff x and y are equal.

    Checks the dtype and shape if x and y are numpy.ndarray instances.

    """
    if isinstance(x, np.ndarray) and isinstance(y, np.ndarray):
        return (x.dtype == y.dtype and x.shape == y.shape and
                n.all(abs(x - y) < 1e-10))
    elif (isinstance(x, np.random.RandomState) and
          isinstance(y, np.random.RandomState)):
        return python_all(n.all(a == b) for a, b in
                          izip(x.__getstate__(), y.__getstate__()))
    else:
        return x == y
//  [python]
def check_equal_numpy(x, y):
    """
    Return True iff x and y are equal.

    Checks the dtype and shape if x and y are numpy.ndarray instances.

    """
    if isinstance(x, np.ndarray) and isinstance(y, np.ndarray):
        return (x.dtype == y.dtype and x.shape == y.shape and
                np.all(abs(x - y) < 1e-10))
    elif (isinstance(x, np.random.RandomState) and
          isinstance(y, np.random.RandomState)):
        return python_all(np.all(a == b) for a, b in
                          izip(x.__getstate__(), y.__getstate__()))
    else:
        return x == y
//  [python]
def numpy_scalar(data):
    """ Return a scalar stored in a numpy ndarray.

    Raises
    ------
     NotScalarConstantError
        If the numpy ndarray is not a scalar.

    """

    # handle case where data is numpy.array([])
    if (data.ndim > 0 and
        (len(data.shape) == 0 or
         builtins.max(data.shape) == 0)):
        assert np.all(np.array([]) == data)
        raise EmptyConstantError()
    try:
        np.complex(data)  # works for all numeric scalars
        return data
    except Exception:
        raise NotScalarConstantError(
            'v.data is non-numeric, non-scalar, or has more than one'
            ' unique value', data)
//  [python]
def test_basic(self):
        y1 = [0, 1, 1, 0]
        y2 = [0, 0, 0, 0]
        y3 = [1, 1, 1, 1]
        assert_(not n.all(y1))
        assert_(n.all(y3))
        assert_(not n.all(y2))
        assert_(n.all(~n.array(y2)))
//  [python]
def test_basic(self):
        y1 = [0, 1, 1, 0]
        y2 = [0, 0, 0, 0]
        y3 = [1, 1, 1, 1]
        assert_(not np.all(y1))
        assert_(np.all(y3))
        assert_(not np.all(y2))
        assert_(np.all(~np.array(y2)))
//  [python]
def perform(self, node, axis_and_tensors, out_):
        out, = out_
        view = self.view
        axis, tensors = axis_and_tensors[0], axis_and_tensors[1:]
        # we check these tensors for being empty.
        if (view != -1) and reduce(lambda x, y: x and y,
                [tensor.shape[axis] == 0 for tensor in
                 tensors[0:view] + tensors[view + 1:]]):
            out[0] = tensors[view]

        else:
            ndim = tensors[0].ndim
            if axis < -ndim:
                raise IndexError("Join axis %d out of bounds [0, %d)" %
                                 (axis, ndim))

            out[0] = theano._asarray(np.concatenate(tensors, axis=axis),
                                     dtype=node.outputs[0].type.dtype)
//  [python]
def perform(self, node, axis_and_tensors, out_):
        out, = out_
        view = self.view
        axis, tensors = axis_and_tensors[0], axis_and_tensors[1:]
        # we check these tensors for being empty.
        if (view != -1) and np.all(
                [tensor.shape[axis] == 0 for tensor in
                 tensors[0:view] + tensors[view + 1:]]):
            out[0] = tensors[view]

        else:
            ndim = tensors[0].ndim
            if axis < -ndim:
                raise IndexError("Join axis %d out of bounds [0, %d)" %
                                 (axis, ndim))

            out[0] = theano._asarray(np.concatenate(tensors, axis=axis),
                                     dtype=node.outputs[0].type.dtype)
//  [python]
def test_blasdot_uninitialized_memory(self):
        # Ticket #950
        for m in [0, 1, 2]:
            for n in [0, 1, 2]:
                for k in range(3):
                    # Try to ensure that x->data contains non-zero floats
                    x = np.array([123456789e199], dtype=np.float64)
                    if IS_PYPY:
                        x.resize((m, 0), refcheck=False)
                    else:
                        x.resize((m, 0))
                    y = np.array([123456789e199], dtype=np.float64)
                    if IS_PYPY:
                        y.resize((0, n), refcheck=False)
                    else:
                        y.resize((0, n))

                    # `dot` should just return zero (m, n) matrix
                    z = np.dot(x, y)
                    assert_(np.all(z == 0))
                    assert_(z.shape == (m, n))
//  [python]
def test_random(self):
        x = rand(10)
        bin = np.linspace(x.min(), x.max(), 10)
        assert_(all(digitize(x, bin) != 0))
//  [python]
def test_random(self):
        x = rand(10)
        bin = np.linspace(x.min(), x.max(), 10)
        assert_(np.all(digitize(x, bin) != 0))
//  [python]
def test_right_open_random(self):
        x = rand(10)
        bins = np.linspace(x.min(), x.max(), 10)
        assert_(not np.any(digitize(x, bins, True) == 10))
//  [python]
def test_right_open_random(self):
        x = rand(10)
        bins = np.linspace(x.min(), x.max(), 10)
        assert_(np.all(digitize(x, bins, True) != 10))
//  [python]
def test_simple(self):
        # check that unwrap removes jumps greater that 2*pi
        assert_array_equal(unwrap([1, 1 + 2 * np.pi]), [1, 1])
        # check that unwrap maintains continuity
        assert_(all(diff(unwrap(rand(10) * 100)) < np.pi))
//  [python]
def test_simple(self):
        # check that unwrap removes jumps greater that 2*pi
        assert_array_equal(unwrap([1, 1 + 2 * np.pi]), [1, 1])
        # check that unwrap maintains continuity
        assert_(np.all(diff(unwrap(rand(10) * 100)) < np.pi))
//  [python]
def infer_shape(self, node, i_shapes):
        # Note start, stop and step can be float numbers.
        start, stop, step = node.inputs

        def is_constant_value(var, value):
            try:
                v = get_scalar_constant_value(var)
                return n.all(v == value)
            except NotScalarConstantError:
                pass
            return False

        def upcast(var):
            if (var.dtype in integer_dtypes and
                    # We do not want to cast uint64 to int64 as this can
                    # loose information. If we upcast uint64 with int64,
                    # this give float64. This is safer then checking for
                    # uint64 in case we support [u]int128 or other in the
                    # future.
                    scal.upcast(var.dtype, 'int64') == 'int64'):
                return cast(var, 'int64')
            return var

        if is_constant_value(step, 1):
            if is_constant_value(start, 0):
                return [(cast(stop, 'int64'),)]
            else:
                stop = upcast(stop)
                start = upcast(start)
                return [(maximum(cast(stop - start, 'int64'), 0),)]
        else:
            stop = upcast(stop)
            start = upcast(start)
            return [(maximum(cast(ceil(cast((stop - start), 'float64') / step),
                    'int64'), 0),)]
//  [python]
def infer_shape(self, node, i_shapes):
        # Note start, stop and step can be float numbers.
        start, stop, step = node.inputs

        def is_constant_value(var, value):
            try:
                v = get_scalar_constant_value(var)
                return np.all(v == value)
            except NotScalarConstantError:
                pass
            return False

        def upcast(var):
            if (var.dtype in integer_dtypes and
                    # We do not want to cast uint64 to int64 as this can
                    # loose information. If we upcast uint64 with int64,
                    # this give float64. This is safer then checking for
                    # uint64 in case we support [u]int128 or other in the
                    # future.
                    scal.upcast(var.dtype, 'int64') == 'int64'):
                return cast(var, 'int64')
            return var

        if is_constant_value(step, 1):
            if is_constant_value(start, 0):
                return [(cast(stop, 'int64'),)]
            else:
                stop = upcast(stop)
                start = upcast(start)
                return [(maximum(cast(stop - start, 'int64'), 0),)]
        else:
            stop = upcast(stop)
            start = upcast(start)
            return [(maximum(cast(ceil(cast((stop - start), 'float64') / step),
                    'int64'), 0),)]
//  [python]
def test_simple(self):
        tgt1 = corrcoef(self.A)
        assert_almost_equal(tgt1, self.res1)
        assert_(n.all(n.abs(tgt1) <= 1.0))

        tgt2 = corrcoef(self.A, self.B)
        assert_almost_equal(tgt2, self.res2)
        assert_(n.all(n.abs(tgt2) <= 1.0))
//  [python]
def test_simple(self):
        tgt1 = corrcoef(self.A)
        assert_almost_equal(tgt1, self.res1)
        assert_(np.all(np.abs(tgt1) <= 1.0))

        tgt2 = corrcoef(self.A, self.B)
        assert_almost_equal(tgt2, self.res2)
        assert_(np.all(np.abs(tgt2) <= 1.0))
//  [python]
def test_complex(self):
        x = np.array([[1, 2, 3], [1j, 2j, 3j]])
        res = corrcoef(x)
        tgt = np.array([[1., -1.j], [1.j, 1.]])
        assert_allclose(res, tgt)
        assert_(np.all(np.abs(res) <= 1.0))
//  [python]
def test_simple(self):
        x = np.array([4, 3, 2, 1, 1, 2, 3, 4, 0])
        assert_(n.all(unique(x) == [0, 1, 2, 3, 4]))
        assert_(unique(np.array([1, 1, 1, 1, 1])) == np.array([1]))
        x = ['widget', 'ham', 'foo', 'bar', 'foo', 'ham']
        assert_(n.all(unique(x) == ['bar', 'foo', 'ham', 'widget']))
        x = np.array([5 + 6j, 1 + 1j, 1 + 10j, 10, 5 + 6j])
        assert_(n.all(unique(x) == [1 + 1j, 1 + 10j, 5 + 6j, 10]))
//  [python]
def test_simple(self):
        x = np.array([4, 3, 2, 1, 1, 2, 3, 4, 0])
        assert_(np.all(unique(x) == [0, 1, 2, 3, 4]))
        assert_(unique(np.array([1, 1, 1, 1, 1])) == np.array([1]))
        x = ['widget', 'ham', 'foo', 'bar', 'foo', 'ham']
        assert_(np.all(unique(x) == ['bar', 'foo', 'ham', 'widget']))
        x = np.array([5 + 6j, 1 + 1j, 1 + 10j, 10, 5 + 6j])
        assert_(np.all(unique(x) == [1 + 1j, 1 + 10j, 5 + 6j, 10]))
//  [python]
def test_extreme(self):
        x = [[1e-100, 1e100], [1e100, 1e-100]]
        with np.errstate(all='raise'):
            c = corrcoef(x)
        assert_array_almost_equal(c, np.array([[1., -1.], [-1., 1.]]))
        assert_(np.all(np.abs(c) <= 1.0))
//  [python]
def test_period(self):
        # check that unwrap removes jumps greater that 255
        assert_array_equal(unwrap([1, 1 + 256], period=255), [1, 2])
        # check that unwrap maintains continuity
        assert_(np.all(diff(unwrap(rand(10) * 1000, period=255)) < 255))
        # check simple case
        simple_seq = np.array([0, 75, 150, 225, 300])
        wrap_seq = np.mod(simple_seq, 255)
        assert_array_equal(unwrap(wrap_seq, period=255), simple_seq)
        # check custom discont value
        uneven_seq = np.array([0, 75, 150, 225, 300, 430])
        wrap_uneven = np.mod(uneven_seq, 250)
        no_discont = unwrap(wrap_uneven, period=250)
        assert_array_equal(no_discont, [0, 75, 150, 225, 300, 180])
        sm_discont = unwrap(wrap_uneven, period=250, discont=140)
        assert_array_equal(sm_discont, [0, 75, 150, 225, 300, 430])
        assert sm_discont.dtype == wrap_uneven.dtype
//  [python]
def test_object_with_array(self):
        mx1 = masked_array([1.], mask=[True])
        mx2 = masked_array([1., 2.])
        mx = masked_array([mx1, mx2], mask=[False, True], dtype=object)
        assert_(mx[0] is mx1)
        assert_(mx[1] is not mx2)
        assert_(n.all(mx[1].data == mx2.data))
        assert_(n.all(mx[1].mask))
        # check that we return a view.
        mx[1].data[0] = 0.
        assert_(mx2[0] == 0.)
//  [python]
def test_object_with_array(self):
        mx1 = masked_array([1.], mask=[True])
        mx2 = masked_array([1., 2.])
        mx = masked_array([mx1, mx2], mask=[False, True], dtype=object)
        assert_(mx[0] is mx1)
        assert_(mx[1] is not mx2)
        assert_(np.all(mx[1].data == mx2.data))
        assert_(np.all(mx[1].mask))
        # check that we return a view.
        mx[1].data[0] = 0.
        assert_(mx2[0] == 0.)
//  [python]
def test_identity():
    def check(dtype):
        obj = rand_of_dtype((2,), dtype)
        sym = tensor.vector(dtype=dtype)
        f = function([sym], tensor_copy(sym))
        assert n.all(obj == f(obj))
        assert obj.dtype == f(obj).dtype
        topo = f.maker.fgraph.toposort()
        assert len(topo) == 1
        if theano.config.mode != 'FAST_COMPILE':
            assert isinstance(topo[0].op, DeepCopyOp)

    for dtype in ALL_DTYPES:
        yield check, dtype
//  [python]
def test_identity():
    def check(dtype):
        obj = rand_of_dtype((2,), dtype)
        sym = tensor.vector(dtype=dtype)
        f = function([sym], tensor_copy(sym))
        assert numpy.all(obj == f(obj))
        assert obj.dtype == f(obj).dtype
        topo = f.maker.fgraph.toposort()
        assert len(topo) == 1
        if theano.config.mode != 'FAST_COMPILE':
            assert isinstance(topo[0].op, DeepCopyOp)

    for dtype in ALL_DTYPES:
        yield check, dtype