jiahuimbzuai/CoQuIR huggingface dataset:
//  [python]
def test_get_qm9_smiles():
    # test default behavior
    pp = AtomicNumberPreprocessor()
    dataset, smiles = qm9.get_qm9(preprocessor=pp, return_smiles=True)

    # --- Test dataset is correctly obtained ---
    index = numpy.random.choice(len(dataset), None)
    atoms, label = dataset[index]

    assert atoms.ndim == 1  # (atom, )
    assert atoms.dtype == numpy.int32
    # (atom from, atom to) or (edge_type, atom from, atom to)
    assert label.ndim == 1
    assert label.shape[0] == QM9_NUM_LABEL
    assert label.dtype == numpy.float32

    # --- Test number of dataset ---
    assert len(dataset) == QM9_NUM_DATASET
    assert len(smiles) == QM9_NUM_DATASET

    # --- Test order of dataset ---
    atoms0, labels0 = dataset[0]
    assert smiles[0] == 'C'
    assert numpy.all(atoms0 == numpy.array([6], dtype=numpy.int32))

    atoms7777, labels7777 = dataset[7777]
    assert smiles[7777] == 'CC1=NCCC(C)O1'
    assert numpy.all(
        atoms7777 == numpy.array([6, 6, 7, 6, 6, 6, 6, 8], dtype=numpy.int32))

    atoms133884, labels133884 = dataset[133884]
    assert smiles[133884] == 'C1N2C3C4C5OC13C2C54'
    assert numpy.all(
        atoms133884 == numpy.array([6, 7, 6, 6, 6, 8, 6, 6, 6],
                                   dtype=numpy.int32))
//  [python]
def trim(image, shape):
    """
    Trim image to a given shape

    Parameters
    ----------
    image: 2D `numpy.ndarray`
        Input image
    shape: tuple of int
        Desired output shape of the image

    Returns
    -------
    new_image: 2D `numpy.ndarray`
        Input image trimmed

    """
    shape = np.asarray(shape, dtype=int)
    imshape = np.asarray(image.shape, dtype=int)

    if np.alltrue(imshape == shape):
        return image

    if np.any(shape <= 0):
        raise ValueError("TRIM: null or negative shape given")

    dshape = imshape - shape
    if np.any(dshape < 0):
        raise ValueError("TRIM: target size bigger than source one")

    if np.any(dshape % 2 != 0):
        raise ValueError("TRIM: source and target shapes "
                         "have different parity")

    idx, idy = np.indices(shape)
    offx, offy = dshape // 2

    return image[idx + offx, idy + offy]
//  [python]
def trim(image, shape):
    """
    Trim image to a given shape

    Parameters
    ----------
    image: 2D `numpy.ndarray`
        Input image
    shape: tuple of int
        Desired output shape of the image

    Returns
    -------
    new_image: 2D `numpy.ndarray`
        Input image trimmed

    """
    shape = np.asarray(shape, dtype=int)
    imshape = np.asarray(image.shape, dtype=int)

    if np.all(imshape == shape):
        return image

    if np.any(shape <= 0):
        raise ValueError("TRIM: null or negative shape given")

    dshape = imshape - shape
    if np.any(dshape < 0):
        raise ValueError("TRIM: target size bigger than source one")

    if np.any(dshape % 2 != 0):
        raise ValueError("TRIM: source and target shapes "
                         "have different parity")

    idx, idy = np.indices(shape)
    offx, offy = dshape // 2

    return image[idx + offx, idy + offy]
//  [python]
def from_contours(cls, contours, required_variance_explained=0.95, return_positions=False):
        """This class method should be used to construct a PCAContour object from a
        set of contours. The proncipal components of the contours are caltulated, and
        enough retained to account for the 'required_variance_explained' fraction.

        The mean shape is stored in the instance variable 'mean' and the principal components
        in 'modes'. The square root of the variance explained by each mode is stored in
        'standard_deviations', while the total variance of all of the modes (even those
        not retained) is in 'total_variance'.

        If the 'return_positions' parameter is true, then a tuple is returned containing
        (pca_contour, positions, normalized_positions), where 'pca_contour' is the new
        PCAContour instance, 'positions' is the position of each of the input contours
        along each principal shape mode, and 'normalized_positions' is the position
        along each mode in terms of standard deviations along that mode.
        """
        import pca as pca

        data = [c.points for c in contours]
        if not utils.all_same_shape(data):
            raise ValueError("All contours must have the same number of points in order to perform PCA.")
        units = [c.units for c in contours]
        if not numpy.alltrue([u == units[0] for u in units]):
            raise ValueError("All contours must have the same units in order to produce a PCA shape model from them.")
        units = units[0]
        scales = [utils.decompose_homogenous_transform(c.to_world_transform)[1] for c in contours]
        if numpy.alltrue([numpy.allclose(scales[0], s) for s in scales[1:]]):
            transform = utils.make_homogenous_transform(transform=scales[0])
        else:
            transform = numpy.eye(3)
        vals = pca.pca_dimensionality_reduce(numpy.array(data, dtype=numpy.float32), required_variance_explained)
        mean, pcs, norm_pcs, variances, total_variance, positions, norm_positions = vals
        c = cls(
            points=mean,
            mean=mean,
            modes=pcs,
            standard_deviations=numpy.sqrt(variances),
            total_variance=total_variance,
            position=numpy.zeros(len(norm_pcs)),
            units=units,
            to_world_transform=transform,
        )
        if return_positions:
            return c, positions, norm_positions
        else:
            return c
//  [python]
def from_contours(cls, contours, required_variance_explained=0.95, return_positions=False):
        """This class method should be used to construct a PCAContour object from a
        set of contours. The proncipal components of the contours are caltulated, and
        enough retained to account for the 'required_variance_explained' fraction.

        The mean shape is stored in the instance variable 'mean' and the principal components
        in 'modes'. The square root of the variance explained by each mode is stored in
        'standard_deviations', while the total variance of all of the modes (even those
        not retained) is in 'total_variance'.

        If the 'return_positions' parameter is true, then a tuple is returned containing
        (pca_contour, positions, normalized_positions), where 'pca_contour' is the new
        PCAContour instance, 'positions' is the position of each of the input contours
        along each principal shape mode, and 'normalized_positions' is the position
        along each mode in terms of standard deviations along that mode.
        """
        import pca as pca

        data = [c.points for c in contours]
        if not utils.all_same_shape(data):
            raise ValueError("All contours must have the same number of points in order to perform PCA.")
        units = [c.units for c in contours]
        if not numpy.all([u == units[0] for u in units]):
            raise ValueError("All contours must have the same units in order to produce a PCA shape model from them.")
        units = units[0]
        scales = [utils.decompose_homogenous_transform(c.to_world_transform)[1] for c in contours]
        if numpy.all([numpy.allclose(scales[0], s) for s in scales[1:]]):
            transform = utils.make_homogenous_transform(transform=scales[0])
        else:
            transform = numpy.eye(3)
        vals = pca.pca_dimensionality_reduce(numpy.array(data, dtype=numpy.float32), required_variance_explained)
        mean, pcs, norm_pcs, variances, total_variance, positions, norm_positions = vals
        c = cls(
            points=mean,
            mean=mean,
            modes=pcs,
            standard_deviations=numpy.sqrt(variances),
            total_variance=total_variance,
            position=numpy.zeros(len(norm_pcs)),
            units=units,
            to_world_transform=transform,
        )
        if return_positions:
            return c, positions, norm_positions
        else:
            return c
//  [python]
def ufunc_find_matching_loop(ufunc, op_dtypes):
    assert(isinstance(ufunc, numpy.ufunc))
    assert(len(op_dtypes) == ufunc.nin)

    # In NumPy, the loops are evaluated from first to last. The first one that is viable
    # is the one used. One loop is viable if it is possible to cast every operand to the
    # one expected by the ufunc. Note that the output is not considered in this logic.
    for candidate in ufunc.types:
        if numpy.alltrue([numpy.can_cast(*x) for x in zip(op_dtypes, candidate[0:ufunc.nin])]):
            # found
            return candidate

    return None
//  [python]
def ufunc_find_matching_loop(ufunc, op_dtypes):
    assert(isinstance(ufunc, numpy.ufunc))
    assert(len(op_dtypes) == ufunc.nin)

    # In NumPy, the loops are evaluated from first to last. The first one that is viable
    # is the one used. One loop is viable if it is possible to cast every operand to the
    # one expected by the ufunc. Note that the output is not considered in this logic.
    for candidate in ufunc.types:
        if numpy.all([numpy.can_cast(*x) for x in zip(op_dtypes, candidate[0:ufunc.nin])]):
            # found
            return candidate

    return None
//  [python]
def zero_pad(image, shape, position='corner'):
    """
    Extends image to a certain size with zeros

    Parameters
    ----------
    image: real 2d `numpy.ndarray`
        Input image
    shape: tuple of int
        Desired output shape of the image
    position : str, optional
        The position of the input image in the output one:
            * 'corner'
                top-left corner (default)
            * 'center'
                centered

    Returns
    -------
    padded_img: real `numpy.ndarray`
        The zero-padded image

    """
    shape = np.asarray(shape, dtype=int)
    imshape = np.asarray(image.shape, dtype=int)

    if np.alltrue(imshape == shape):
        return image

    if np.any(shape <= 0):
        raise ValueError("ZERO_PAD: null or negative shape given")

    dshape = shape - imshape
    if np.any(dshape < 0):
        raise ValueError("ZERO_PAD: target size smaller than source one")

    pad_img = np.zeros(shape, dtype=image.dtype)

    idx, idy = np.indices(imshape)

    if position == 'center':
        if np.any(dshape % 2 != 0):
            raise ValueError("ZERO_PAD: source and target shapes "
                             "have different parity.")
        offx, offy = dshape // 2
    else:
        offx, offy = (0, 0)

    pad_img[idx + offx, idy + offy] = image

    return pad_img
//  [python]
def zero_pad(image, shape, position='corner'):
    """
    Extends image to a certain size with zeros

    Parameters
    ----------
    image: real 2d `numpy.ndarray`
        Input image
    shape: tuple of int
        Desired output shape of the image
    position : str, optional
        The position of the input image in the output one:
            * 'corner'
                top-left corner (default)
            * 'center'
                centered

    Returns
    -------
    padded_img: real `numpy.ndarray`
        The zero-padded image

    """
    shape = np.asarray(shape, dtype=int)
    imshape = np.asarray(image.shape, dtype=int)

    if np.all(imshape == shape):
        return image

    if np.any(shape <= 0):
        raise ValueError("ZERO_PAD: null or negative shape given")

    dshape = shape - imshape
    if np.any(dshape < 0):
        raise ValueError("ZERO_PAD: target size smaller than source one")

    pad_img = np.zeros(shape, dtype=image.dtype)

    idx, idy = np.indices(imshape)

    if position == 'center':
        if np.any(dshape % 2 != 0):
            raise ValueError("ZERO_PAD: source and target shapes "
                             "have different parity.")
        offx, offy = dshape // 2
    else:
        offx, offy = (0, 0)

    pad_img[idx + offx, idy + offy] = image

    return pad_img
//  [python]
def test_is_ref_by_default(self):
        # check that we're returning only many version of the same
        # object by default
        ps7 = self.ps7
        out = [x for x in ps7.iter_inner()]
        assert numpy.alltrue([x==out[0] for x in out])
//  [python]
def test_is_ref_by_default(self):
        # check that we're returning only many version of the same
        # object by default
        ps7 = self.ps7
        out = [x for x in ps7.iter_inner()]
        assert numpy.all([x==out[0] for x in out])
//  [python]
def test_copy(self):
        ps7 = self.ps7
        out = [x for x in ps7.iter_inner(copy=True)]
        # now check that there are no duplicate objects
        assert numpy.alltrue([out[x-1] not in out[x:] for x in range(1,len(out))])
//  [python]
def test_copy(self):
        ps7 = self.ps7
        out = [x for x in ps7.iter_inner(copy=True)]
        # now check that there are no duplicate objects
        assert numpy.all([out[x-1] not in out[x:] for x in range(1,len(out))])
//  [python]
def verify(self, obj, state):
        data = state["data"]
        self.assertEqual(state["class_name"], obj.__class__.__name__)
        data = data["data"]
        self.assertEqual(data["b"], obj.b)
        self.assertEqual(data["i"], obj.i)
        self.assertEqual(data["longi"], obj.longi)
        self.assertEqual(data["f"], obj.f)
        self.assertEqual(data["c"], obj.c)
        self.assertEqual(data["n"], obj.n)
        self.assertEqual(data["s"], obj.s)
        self.assertEqual(data["u"], obj.u)
        tup = data["tuple"]["data"]
        self.assertEqual(tup[:-1], obj.tuple[:-1])
        self.assertEqual(tup[-1]["data"]["data"]["a"], "t")
        lst = data["list"]["data"]
        self.assertEqual(lst[:-1], obj.list[:-1])

        pure_lst = data["pure_list"]["data"]
        self.assertEqual(pure_lst, obj.pure_list)
        dct = data["dict"]["data"]
        self.assertEqual(dct["a"], obj.dict["a"])
        self.assertEqual(dct["b"], obj.dict["b"])

        self._check_instance_and_references(obj, data)

        num_attr = "numeric" if data["numeric"]["type"] == "numeric" else "ref"
        junk = state_pickler.gunzip_string(
            base64.decodebytes(data[num_attr]["data"])
        )
        num = pickle.loads(junk)
        self.assertEqual(numpy.alltrue(numpy.ravel(num == obj.numeric)), 1)

        self.assertIn(data["ref"]["type"], ["reference", "numeric"])
        if data["ref"]["type"] == "numeric":
            self.assertEqual(data["numeric"]["type"], "reference")
        else:
            self.assertEqual(data["numeric"]["type"], "numeric")
        self.assertEqual(data["ref"]["id"], data["numeric"]["id"])
//  [python]
def verify(self, obj, state):
        data = state["data"]
        self.assertEqual(state["class_name"], obj.__class__.__name__)
        data = data["data"]
        self.assertEqual(data["b"], obj.b)
        self.assertEqual(data["i"], obj.i)
        self.assertEqual(data["longi"], obj.longi)
        self.assertEqual(data["f"], obj.f)
        self.assertEqual(data["c"], obj.c)
        self.assertEqual(data["n"], obj.n)
        self.assertEqual(data["s"], obj.s)
        self.assertEqual(data["u"], obj.u)
        tup = data["tuple"]["data"]
        self.assertEqual(tup[:-1], obj.tuple[:-1])
        self.assertEqual(tup[-1]["data"]["data"]["a"], "t")
        lst = data["list"]["data"]
        self.assertEqual(lst[:-1], obj.list[:-1])

        pure_lst = data["pure_list"]["data"]
        self.assertEqual(pure_lst, obj.pure_list)
        dct = data["dict"]["data"]
        self.assertEqual(dct["a"], obj.dict["a"])
        self.assertEqual(dct["b"], obj.dict["b"])

        self._check_instance_and_references(obj, data)

        num_attr = "numeric" if data["numeric"]["type"] == "numeric" else "ref"
        junk = state_pickler.gunzip_string(
            base64.decodebytes(data[num_attr]["data"])
        )
        num = pickle.loads(junk)
        self.assertEqual(numpy.all(numpy.ravel(num == obj.numeric)), True)

        self.assertIn(data["ref"]["type"], ["reference", "numeric"])
        if data["ref"]["type"] == "numeric":
            self.assertEqual(data["numeric"]["type"], "reference")
        else:
            self.assertEqual(data["numeric"]["type"], "numeric")
        self.assertEqual(data["ref"]["id"], data["numeric"]["id"])
//  [python]
def inv(self):
        """Inverse and return as new matrix."""
        if not numpy.alltrue(self.sizeList[:,0]==self.sizeList[:,1]):
            raise Exception("Cannont inverse a non-square block matrix - please use todense and do it manually")
        newBM=BlockMatrix()
        bl=[]
        for b in self.blockList:
            bl.append(numpy.linalg.inv(b))
        newBM.assign(bl)
        return newBM
//  [python]
def inv(self):
        """Inverse and return as new matrix."""
        if not numpy.all(self.sizeList[:,0]==self.sizeList[:,1]):
            raise Exception("Cannont inverse a non-square block matrix - please use todense and do it manually")
        newBM=BlockMatrix()
        bl=[]
        for b in self.blockList:
            bl.append(numpy.linalg.inv(b))
        newBM.assign(bl)
        return newBM
//  [python]
def pinv(self,rcond=1e-15):
        """Inverse and return as new matrix."""
        if not numpy.alltrue(self.sizeList[:,0]==self.sizeList[:,1]):
            raise Exception("Cannont inverse a non-square block matrix - please use todense and do it manually")
        newBM=BlockMatrix()
        bl=[]
        for b in self.blockList:
            bl.append(numpy.linalg.pinv(b,rcond))
        newBM.assign(bl)
        return newBM
//  [python]
def pinv(self,rcond=1e-15):
        """Inverse and return as new matrix."""
        if not numpy.all(self.sizeList[:,0]==self.sizeList[:,1]):
            raise Exception("Cannont inverse a non-square block matrix - please use todense and do it manually")
        newBM=BlockMatrix()
        bl=[]
        for b in self.blockList:
            bl.append(numpy.linalg.pinv(b,rcond))
        newBM.assign(bl)
        return newBM
//  [python]
def transpose(self):
        """Return the transpose of self."""
        if not numpy.alltrue(self.sizeList[:,0]==self.sizeList[:,1]):
            raise Exception("Not yet implemented - transpose of block matrix with non-square blocks - should be easy to do if you need it...")
        newBM=self.copy()
        for b in newBM.blockList:
            b[:]=b.copy().transpose()
//  [python]
def transpose(self):
        """Return the transpose of self."""
        if not numpy.all(self.sizeList[:,0]==self.sizeList[:,1]):
            raise Exception("Not yet implemented - transpose of block matrix with non-square blocks - should be easy to do if you need it...")
        newBM=self.copy()
        for b in newBM.blockList:
            b[:]=b.copy().transpose()
//  [python]
def verify_unpickled(self, obj, state):
        self.assertEqual(
            state.__metadata__["class_name"], obj.__class__.__name__
        )
        self.assertEqual(state.b, obj.b)
        self.assertEqual(state.i, obj.i)
        self.assertEqual(state.longi, obj.longi)
        self.assertEqual(state.f, obj.f)
        self.assertEqual(state.c, obj.c)
        self.assertEqual(state.n, obj.n)
        self.assertEqual(state.s, obj.s)
        self.assertEqual(state.u, obj.u)
        self.assertEqual(state.inst.__metadata__["type"], "instance")

        tup = state.tuple
        self.assertTrue(state.tuple.has_instance)
        self.assertEqual(tup[:-1], obj.tuple[:-1])
        self.assertEqual(tup[-1].a, "t")
        lst = state.list
        self.assertTrue(state.list.has_instance)
        self.assertEqual(lst[:-1], obj.list[:-1])
        # Make sure the reference is the same
        self.assertEqual(id(state.inst), id(lst[-1]))

        self.assertEqual(lst[-1].a, "b")

        pure_lst = state.pure_list
        self.assertEqual(pure_lst, obj.pure_list)
        self.assertFalse(state.pure_list.has_instance)

        dct = state.dict
        self.assertTrue(dct.has_instance)
        self.assertEqual(dct["a"], obj.dict["a"])
        self.assertEqual(dct["b"], obj.dict["b"])
        self.assertEqual(dct["ref"].__metadata__["type"], "instance")

        num = state.numeric
        self.assertEqual(numpy.alltrue(numpy.ravel(num == obj.numeric)), 1)
        self.assertEqual(id(state.ref), id(num))

        if TVTK_AVAILABLE:
            _tvtk = state._tvtk
            self.assertEqual(_tvtk.representation, obj._tvtk.representation)
            self.assertEqual(_tvtk.specular_color, obj._tvtk.specular_color)
            self.assertEqual(_tvtk.point_size, obj._tvtk.point_size)
//  [python]
def verify_unpickled(self, obj, state):
        self.assertEqual(
            state.__metadata__["class_name"], obj.__class__.__name__
        )
        self.assertEqual(state.b, obj.b)
        self.assertEqual(state.i, obj.i)
        self.assertEqual(state.longi, obj.longi)
        self.assertEqual(state.f, obj.f)
        self.assertEqual(state.c, obj.c)
        self.assertEqual(state.n, obj.n)
        self.assertEqual(state.s, obj.s)
        self.assertEqual(state.u, obj.u)
        self.assertEqual(state.inst.__metadata__["type"], "instance")

        tup = state.tuple
        self.assertTrue(state.tuple.has_instance)
        self.assertEqual(tup[:-1], obj.tuple[:-1])
        self.assertEqual(tup[-1].a, "t")
        lst = state.list
        self.assertTrue(state.list.has_instance)
        self.assertEqual(lst[:-1], obj.list[:-1])
        # Make sure the reference is the same
        self.assertEqual(id(state.inst), id(lst[-1]))

        self.assertEqual(lst[-1].a, "b")

        pure_lst = state.pure_list
        self.assertEqual(pure_lst, obj.pure_list)
        self.assertFalse(state.pure_list.has_instance)

        dct = state.dict
        self.assertTrue(dct.has_instance)
        self.assertEqual(dct["a"], obj.dict["a"])
        self.assertEqual(dct["b"], obj.dict["b"])
        self.assertEqual(dct["ref"].__metadata__["type"], "instance")

        num = state.numeric
        self.assertEqual(numpy.all(numpy.ravel(num == obj.numeric)), True)
        self.assertEqual(id(state.ref), id(num))

        if TVTK_AVAILABLE:
            _tvtk = state._tvtk
            self.assertEqual(_tvtk.representation, obj._tvtk.representation)
            self.assertEqual(_tvtk.specular_color, obj._tvtk.specular_color)
            self.assertEqual(_tvtk.point_size, obj._tvtk.point_size)
//  [python]
def test_graphtool_MouseButtonUp(self):
        """testing when mousebuttonup var set to 1"""
        root=Tk()
        app=GraphApp(root)
        app.master.update()
        pause()
        val1=app.caluculate_ramp()
        app.mousebuttonup=1
        points=[(100,150),(150,200)]
        app.setControlPoints(points)
        app.master.update()
        val2=app.caluculate_ramp()
        self.assertFalse(numpy.alltrue(val1 == val2) )
        #self.assertEqual(val1!=val2,True)
        app.master.destroy()
//  [python]
def test_graphtool_MouseButtonUp(self):
        """testing when mousebuttonup var set to 1"""
        root=Tk()
        app=GraphApp(root)
        app.master.update()
        pause()
        val1=app.caluculate_ramp()
        app.mousebuttonup=1
        points=[(100,150),(150,200)]
        app.setControlPoints(points)
        app.master.update()
        val2=app.caluculate_ramp()
        self.assertFalse(numpy.all(val1 == val2) )
        #self.assertEqual(val1!=val2,True)
        app.master.destroy()
//  [python]
def dotBlock(self,m):
        """dot self with another block matrix.
        """
        if m.shape[0]!=self.shape[1]:
            raise Exception("Dot product of wrong shape")
        if numpy.alltrue(self.sizeList==m.sizeList) and numpy.alltrue(self.sizeList[:,0]==self.sizeList[:,1]):#note the second test may be unneccessary - I just odn't have time to think it through at the moment...
            #all blocks same size - just dot the blocks...
            out=BlockMatrix(self.sizeList)
            for i in range(len(self.sizeList)):
                out.blockList[i]=quick.dot(self.blockList[i],m.blockList[i])
            return out
        else:
            #output could be any size... will probably be block, but maybe not square blocks.
            out=numpy.zeros((self.shape[0],m.shape[1]),max(self.dtype,m.dtype))
            x=0
            y=0
            for i in xrange(len(m.sizeList)):
                mblock=m.blockList[i]
                for j in xrange(mblock.shape[1]):
                    for y in xrange(self.shape[0]):
                        #print self.getSlice([slice(y,y+1),slice(int(m.sizeCumList[i]),int(m.sizeCumList[i+1]))]).shape
                        #print mblock[:,j].shape
                        out[y,x]=quick.dot(self.getSlice([slice(y,y+1),slice(int(m.sizeCumList[i,1]),int(m.sizeCumList[i+1,1]))]),mblock[:,j])
                    x+=1
            return out
//  [python]
def dotBlock(self,m):
        """dot self with another block matrix.
        """
        if m.shape[0]!=self.shape[1]:
            raise Exception("Dot product of wrong shape")
        if numpy.all(self.sizeList==m.sizeList) and numpy.all(self.sizeList[:,0]==self.sizeList[:,1]):#note the second test may be unneccessary - I just odn't have time to think it through at the moment...
            #all blocks same size - just dot the blocks...
            out=BlockMatrix(self.sizeList)
            for i in range(len(self.sizeList)):
                out.blockList[i]=quick.dot(self.blockList[i],m.blockList[i])
            return out
        else:
            #output could be any size... will probably be block, but maybe not square blocks.
            out=numpy.zeros((self.shape[0],m.shape[1]),max(self.dtype,m.dtype))
            x=0
            y=0
            for i in xrange(len(m.sizeList)):
                mblock=m.blockList[i]
                for j in xrange(mblock.shape[1]):
                    for y in xrange(self.shape[0]):
                        #print self.getSlice([slice(y,y+1),slice(int(m.sizeCumList[i]),int(m.sizeCumList[i+1]))]).shape
                        #print mblock[:,j].shape
                        out[y,x]=quick.dot(self.getSlice([slice(y,y+1),slice(int(m.sizeCumList[i,1]),int(m.sizeCumList[i+1,1]))]),mblock[:,j])
                    x+=1
            return out
//  [python]
def test_graphtool_Continuous(self):
        """testing when continuous var set to 1"""
        root=Tk()
        app=GraphApp(root)
        app.master.update()
        val1=app.caluculate_ramp()
        app.continuous=1
        points=[(100,150),(150,200)]
        app.setControlPoints(points)
        app.master.update()
        val2=app.caluculate_ramp()
        self.assertFalse(numpy.alltrue(val1 == val2) )
        app.master.destroy()
//  [python]
def test_graphtool_Continuous(self):
        """testing when continuous var set to 1"""
        root=Tk()
        app=GraphApp(root)
        app.master.update()
        val1=app.caluculate_ramp()
        app.continuous=1
        points=[(100,150),(150,200)]
        app.setControlPoints(points)
        app.master.update()
        val2=app.caluculate_ramp()
        self.assertFalse(numpy.all(val1 == val2) )
        app.master.destroy()
//  [python]
def test_torch_padding(default_configuration, check_circuit_has_no_tlu):
    """Test padding in PyTorch using ONNX pad operators."""
    net = PaddingNet()

    num_batch = 20
    inputset = numpy.random.uniform(size=(num_batch, 1, 2, 2))

    quant_model = compile_brevitas_qat_model(
        net,
        inputset,
        configuration=default_configuration,
        p_error=0.01,
    )

    test_input = numpy.ones((1, 1, 2, 2))

    torch_output = net(torch.tensor(test_input)).detach().numpy()

    cml_output = quant_model.forward(test_input, fhe="disable")

    # We only care about checking that zeros added with padding are in the same positions
    # between the torch output and the Concrete ML output
    torch_output = torch_output > 0
    cml_output = cml_output > 0

    assert numpy.alltrue(torch_output == cml_output)
    check_circuit_has_no_tlu(quant_model.fhe_circuit)
//  [python]
def test_torch_padding(default_configuration, check_circuit_has_no_tlu):
    """Test padding in PyTorch using ONNX pad operators."""
    net = PaddingNet()

    num_batch = 20
    inputset = numpy.random.uniform(size=(num_batch, 1, 2, 2))

    quant_model = compile_brevitas_qat_model(
        net,
        inputset,
        configuration=default_configuration,
        p_error=0.01,
    )

    test_input = numpy.ones((1, 1, 2, 2))

    torch_output = net(torch.tensor(test_input)).detach().numpy()

    cml_output = quant_model.forward(test_input, fhe="disable")

    # We only care about checking that zeros added with padding are in the same positions
    # between the torch output and the Concrete ML output
    torch_output = torch_output > 0
    cml_output = cml_output > 0

    assert numpy.all(torch_output == cml_output)
    check_circuit_has_no_tlu(quant_model.fhe_circuit)
//  [python]
def test_graphtool_Update(self):
        """testing when update var set to 1"""
        root=Tk()
        app=GraphApp(root)
        app.master.update()
        points=[(130,150)]
        app.setControlPoints(points)
        val1=app.caluculate_ramp()
        app.update=1
        points=[(100,150),(150,200)]
        app.setControlPoints(points)
        app.Update()
        app.master.update()        
        val2=app.caluculate_ramp()
        #self.assertEqual(val1!=val2,True)
        self.assertFalse(numpy.alltrue(val1 == val2) )
        app.master.destroy()
//  [python]
def test_graphtool_Update(self):
        """testing when update var set to 1"""
        root=Tk()
        app=GraphApp(root)
        app.master.update()
        points=[(130,150)]
        app.setControlPoints(points)
        val1=app.caluculate_ramp()
        app.update=1
        points=[(100,150),(150,200)]
        app.setControlPoints(points)
        app.Update()
        app.master.update()        
        val2=app.caluculate_ramp()
        #self.assertEqual(val1!=val2,True)
        self.assertFalse(numpy.all(val1 == val2) )
        app.master.destroy()
//  [python]
def match_in_viewport(
        prev_points: np.ndarray,
        new_points: np.ndarray,
        image_size: Tuple[int, int]
) -> Tuple[np.ndarray, np.ndarray, np.ndarray]:

    is_in_viewport = lambda p: \
        np.alltrue(p[:2] >= (0, 0)) \
        and np.alltrue(p[:2] < image_size)

    prev_points_matched = []
    new_points_matched = []
    indices = []
    for k, (pp, cp) in enumerate(zip(prev_points, new_points)):
        if is_in_viewport(pp) and is_in_viewport(cp):
            prev_points_matched += [pp[:2]]
            new_points_matched += [cp[:2]]
            indices.append(k)

    prev_points_matched = np.array(prev_points_matched)
    new_points_matched = np.array(new_points_matched)

    return np.array(indices), prev_points_matched, new_points_matched
//  [python]
def match_in_viewport(
        prev_points: np.ndarray,
        new_points: np.ndarray,
        image_size: Tuple[int, int]
) -> Tuple[np.ndarray, np.ndarray, np.ndarray]:

    is_in_viewport = lambda p: \
        np.all(p[:2] >= (0, 0)) \
        and np.all(p[:2] < image_size)

    prev_points_matched = []
    new_points_matched = []
    indices = []
    for k, (pp, cp) in enumerate(zip(prev_points, new_points)):
        if is_in_viewport(pp) and is_in_viewport(cp):
            prev_points_matched += [pp[:2]]
            new_points_matched += [cp[:2]]
            indices.append(k)

    prev_points_matched = np.array(prev_points_matched)
    new_points_matched = np.array(new_points_matched)

    return np.array(indices), prev_points_matched, new_points_matched
//  [python]
def principal_components(image):
    '''
    Calculate Principal Component eigenvalues & eigenvectors for an image.

    Usage::

        pc = principal_components(image)

    Arguments:

        `image` (ndarray, :class:`spectral.Image`, :class:`GaussianStats`):

            An `MxNxB` image

    Returns a :class:`~spectral.algorithms.algorithms.PrincipalComponents`
    object with the following members:

        `eigenvalues`:

            A length B array of eigenvalues

        `eigenvectors`:

            A `BxB` array of normalized eigenvectors

        `stats` (:class:`GaussianStats`):

            A statistics object containing `mean`, `cov`, and `nsamples`.

        `transform`:

            A callable function to transform data to the space of the
            principal components.

        `reduce`:

            A method to reduce the number of eigenvalues.
    '''
    from numpy import sqrt, sum

    if isinstance(image, GaussianStats):
        stats = image
    else:
        stats = calc_stats(image)

    (L, V) = numpy.linalg.eig(stats.cov)

    # numpy says eigenvalues may not be sorted so we'll sort them, if needed.
    if not np.alltrue(np.diff(L) <= 0):
        ii = list(reversed(np.argsort(L)))
        L = L[ii]
        V = V[:, ii]

    return PrincipalComponents(L, V, stats)
//  [python]
def principal_components(image):
    '''
    Calculate Principal Component eigenvalues & eigenvectors for an image.

    Usage::

        pc = principal_components(image)

    Arguments:

        `image` (ndarray, :class:`spectral.Image`, :class:`GaussianStats`):

            An `MxNxB` image

    Returns a :class:`~spectral.algorithms.algorithms.PrincipalComponents`
    object with the following members:

        `eigenvalues`:

            A length B array of eigenvalues

        `eigenvectors`:

            A `BxB` array of normalized eigenvectors

        `stats` (:class:`GaussianStats`):

            A statistics object containing `mean`, `cov`, and `nsamples`.

        `transform`:

            A callable function to transform data to the space of the
            principal components.

        `reduce`:

            A method to reduce the number of eigenvalues.
    '''
    from numpy import sqrt, sum

    if isinstance(image, GaussianStats):
        stats = image
    else:
        stats = calc_stats(image)

    (L, V) = numpy.linalg.eig(stats.cov)

    # numpy says eigenvalues may not be sorted so we'll sort them, if needed.
    if not np.all(np.diff(L) <= 0):
        ii = list(reversed(np.argsort(L)))
        L = L[ii]
        V = V[:, ii]

    return PrincipalComponents(L, V, stats)
//  [python]
def test_reshape_x(self):
      value = np.array([[1, 2], [3, 4]])
      a = xdl.reshape(value, [1, -1])
      a = xdl.execute(a)
      self.assertTrue(np.alltrue(a == [[1, 2, 3, 4]]))
//  [python]
def test_reshape_x(self):
      value = np.array([[1, 2], [3, 4]])
      a = xdl.reshape(value, [1, -1])
      a = xdl.execute(a)
      self.assertTrue(np.all(a == [[1, 2, 3, 4]]))
//  [python]
def test_reshape(self):
      value = np.array([[1, 2], [3, 4]])
      a = xdl.reshape(value, [4])
      a = xdl.execute(a)
      self.assertTrue(np.alltrue(a == [1, 2, 3, 4]))
//  [python]
def test_reshape(self):
      value = np.array([[1, 2], [3, 4]])
      a = xdl.reshape(value, [4])
      a = xdl.execute(a)
      self.assertTrue(np.all(a == [1, 2, 3, 4]))
//  [python]
def __init__(self, dir, dir_rule="*", excludes=[]):
        raw_classes = [os.path.basename(ii) for ii in glob2.glob(os.path.join(dir, dir_rule))]
        raw_classes = [ii for ii in raw_classes if ii not in excludes]
        is_all_numeric = np.alltrue([str.isnumeric(ii) for ii in raw_classes])

        self.raw_classes = sorted(raw_classes, key=lambda xx: int(xx)) if is_all_numeric else sorted(raw_classes)
        self.classes_2_indices = {ii: id for id, ii in enumerate(self.raw_classes)}
        self.indices_2_classes = {vv: kk for kk, vv in self.classes_2_indices.items()}
//  [python]
def __init__(self, dir, dir_rule="*", excludes=[]):
        raw_classes = [os.path.basename(ii) for ii in glob2.glob(os.path.join(dir, dir_rule))]
        raw_classes = [ii for ii in raw_classes if ii not in excludes]
        is_all_numeric = np.all([str.isnumeric(ii) for ii in raw_classes])

        self.raw_classes = sorted(raw_classes, key=lambda xx: int(xx)) if is_all_numeric else sorted(raw_classes)
        self.classes_2_indices = {ii: id for id, ii in enumerate(self.raw_classes)}
        self.indices_2_classes = {vv: kk for kk, vv in self.classes_2_indices.items()}
//  [python]
def interp(self, x):
        """ Average multiple values at edges of numpy.array to use for extrapolation.

            This method only works for extrapolation.
        """
        if numpy.alltrue(numpy.logical_and(x < self._x[0], x > self._x[-1])):
            msg = "end_average() only works for extrapolation.  Some of the "\
                  "in x fall between the endpoints (x[0], x[-1]) of the "\
                  "x numpy.array."
            raise ValueError(msg)

        # find the average y value within depth_interval at both the start and
        # end of the data set that is within depth_interval distance from the
        # ends.
        indices = (self._x[0] + self.index_interval,
                   self._x[-1] - self.index_interval)
        first, last = numpy.searchsorted(self._x, indices)
        y_low = numpy.mean(self._y[:first])
        y_hi = numpy.mean(self._y[last:])

        dist_low = abs(x - self._x[0])
        dist_hi = abs(x - self._x[-1])
        y = numpy.choose(dist_low > dist_hi, (y_low, y_hi))
        return y
//  [python]
def interp(self, x):
        """ Average multiple values at edges of numpy.array to use for extrapolation.

            This method only works for extrapolation.
        """
        if numpy.all(numpy.logical_and(x < self._x[0], x > self._x[-1])):
            msg = "end_average() only works for extrapolation.  Some of the "\
                  "in x fall between the endpoints (x[0], x[-1]) of the "\
                  "x numpy.array."
            raise ValueError(msg)

        # find the average y value within depth_interval at both the start and
        # end of the data set that is within depth_interval distance from the
        # ends.
        indices = (self._x[0] + self.index_interval,
                   self._x[-1] - self.index_interval)
        first, last = numpy.searchsorted(self._x, indices)
        y_low = numpy.mean(self._y[:first])
        y_hi = numpy.mean(self._y[last:])

        dist_low = abs(x - self._x[0])
        dist_hi = abs(x - self._x[-1])
        y = numpy.choose(dist_low > dist_hi, (y_low, y_hi))
        return y
//  [python]
def test_symbolic_obs_wrapper(env_id):
    env = gym.make(env_id)

    env = SymbolicObsWrapper(env)
    obs, _ = env.reset(seed=123)
    agent_pos = env.agent_pos
    goal_pos = env.goal_pos

    assert obs["image"].shape == (env.width, env.height, 3)
    assert np.alltrue(
        obs["image"][agent_pos[0], agent_pos[1], :]
        == np.array([agent_pos[0], agent_pos[1], OBJECT_TO_IDX["agent"]])
    )
    assert np.alltrue(
        obs["image"][goal_pos[0], goal_pos[1], :]
        == np.array([goal_pos[0], goal_pos[1], OBJECT_TO_IDX["goal"]])
    )

    obs, _, _, _, _ = env.step(2)
    agent_pos = env.agent_pos
    goal_pos = env.goal_pos

    assert obs["image"].shape == (env.width, env.height, 3)
    assert np.alltrue(
        obs["image"][agent_pos[0], agent_pos[1], :]
        == np.array([agent_pos[0], agent_pos[1], OBJECT_TO_IDX["agent"]])
    )
    assert np.alltrue(
        obs["image"][goal_pos[0], goal_pos[1], :]
        == np.array([goal_pos[0], goal_pos[1], OBJECT_TO_IDX["goal"]])
    )
    env.close()
//  [python]
def test_symbolic_obs_wrapper(env_id):
    env = gym.make(env_id)

    env = SymbolicObsWrapper(env)
    obs, _ = env.reset(seed=123)
    agent_pos = env.agent_pos
    goal_pos = env.goal_pos

    assert obs["image"].shape == (env.width, env.height, 3)
    assert np.all(
        obs["image"][agent_pos[0], agent_pos[1], :]
        == np.array([agent_pos[0], agent_pos[1], OBJECT_TO_IDX["agent"]])
    )
    assert np.all(
        obs["image"][goal_pos[0], goal_pos[1], :]
        == np.array([goal_pos[0], goal_pos[1], OBJECT_TO_IDX["goal"]])
    )

    obs, _, _, _, _ = env.step(2)
    agent_pos = env.agent_pos
    goal_pos = env.goal_pos

    assert obs["image"].shape == (env.width, env.height, 3)
    assert np.all(
        obs["image"][agent_pos[0], agent_pos[1], :]
        == np.array([agent_pos[0], agent_pos[1], OBJECT_TO_IDX["agent"]])
    )
    assert np.all(
        obs["image"][goal_pos[0], goal_pos[1], :]
        == np.array([goal_pos[0], goal_pos[1], OBJECT_TO_IDX["goal"]])
    )
    env.close()
//  [python]
def _load_test_frame(self):
        test_frame_ = self.test_frame_
        if test_frame_ is None:
            test_frame_ = self._load_idx(0)

            if self._detect_grayscale is True:
                self.grayscale = bool(
                    np.alltrue(test_frame_[..., 0] == test_frame_[..., -1])
                )

            if self.height_ is None:
                self.height_ = test_frame_.shape[0]
            if self.width_ is None:
                self.width_ = test_frame_.shape[1]
            if self.channels_ is None:
                self.channels_ = test_frame_.shape[2]
        if self.CACHING:  # Deprecated, but keeping functionality for now.
            self.test_frame_ = test_frame_
        return test_frame_
//  [python]
def _load_test_frame(self):
        test_frame_ = self.test_frame_
        if test_frame_ is None:
            test_frame_ = self._load_idx(0)

            if self._detect_grayscale is True:
                self.grayscale = bool(
                    np.all(test_frame_[..., 0] == test_frame_[..., -1])
                )

            if self.height_ is None:
                self.height_ = test_frame_.shape[0]
            if self.width_ is None:
                self.width_ = test_frame_.shape[1]
            if self.channels_ is None:
                self.channels_ = test_frame_.shape[2]
        if self.CACHING:  # Deprecated, but keeping functionality for now.
            self.test_frame_ = test_frame_
        return test_frame_
//  [python]
def test_csv_parser_return_is_successful(csv_file_invalid, mols, label_a):
    """test `labels` option and retain_smiles=True."""
    preprocessor = NFPPreprocessor()
    parser = CSVFileParser(preprocessor, labels='labelA',
                           smiles_col='smiles')
    result = parser.parse(csv_file_invalid, 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
    # print('is_successful', is_successful)
    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_features(dataset[i], expect, label_a[i])
//  [python]
def test_csv_parser_return_is_successful(csv_file_invalid, mols, label_a):
    """test `labels` option and retain_smiles=True."""
    preprocessor = NFPPreprocessor()
    parser = CSVFileParser(preprocessor, labels='labelA',
                           smiles_col='smiles')
    result = parser.parse(csv_file_invalid, 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
    # print('is_successful', is_successful)
    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_features(dataset[i], expect, label_a[i])
//  [python]
def test_get_zinc_smiles():
    # test smiles extraction and dataset order
    pp = AtomicNumberPreprocessor()
    target_index = [0, 7777, 249454]  # set target_index for fast testing...
    dataset, smiles = zinc.get_zinc250k(preprocessor=pp, return_smiles=True,
                                        target_index=target_index)

    # --- Test dataset is correctly obtained ---
    index = numpy.random.choice(len(dataset), None)
    atoms, label = dataset[index]

    assert atoms.ndim == 1  # (atom, )
    assert atoms.dtype == numpy.int32
    # (atom from, atom to) or (edge_type, atom from, atom to)
    assert label.ndim == 1
    assert label.shape[0] == ZINC250K_NUM_LABEL
    assert label.dtype == numpy.float32

    # --- Test number of dataset ---
    assert len(dataset) == len(target_index)
    assert len(smiles) == len(target_index)

    # --- Test order of dataset ---
    assert smiles[0] == 'CC(C)(C)c1ccc2occ(CC(=O)Nc3ccccc3F)c2c1'
    atoms0, labels0 = dataset[0]
    assert numpy.alltrue(atoms0 == numpy.array(
        [6, 6, 6, 6, 6, 6, 6, 6, 8, 6, 6, 6, 6, 8, 7, 6, 6, 6, 6, 6, 6, 9, 6,
         6], dtype=numpy.int32))
    assert numpy.alltrue(labels0 == numpy.array(
        [5.0506, 0.70201224, 2.0840945], dtype=numpy.float32))

    assert smiles[1] == 'CCCc1cc(NC(=O)Nc2ccc3c(c2)OCCO3)n(C)n1'
    atoms7777, labels7777 = dataset[1]
    assert numpy.alltrue(atoms7777 == numpy.array(
        [6, 6, 6, 6, 6, 6, 7, 6, 8, 7, 6, 6, 6, 6, 6, 6, 8, 6, 6, 8, 7, 6, 7],
        dtype=numpy.int32))
    assert numpy.alltrue(labels7777 == numpy.array(
        [2.7878, 0.9035222, 2.3195992], dtype=numpy.float32))

    assert smiles[2] == 'O=C(CC(c1ccccc1)c1ccccc1)N1CCN(S(=O)(=O)c2ccccc2[N+](=O)[O-])CC1'  # NOQA
    atoms249454, labels249454 = dataset[2]
    assert numpy.alltrue(atoms249454 == numpy.array(
        [8,  6,  6,  6,  6,  6,  6,  6,  6,  6,  6,  6,  6,  6,  6,  6,  7,
         6,  6,  7, 16,  8,  8,  6,  6,  6,  6,  6,  6,  7,  8,  8,  6,  6],
        dtype=numpy.int32))
    assert numpy.alltrue(labels249454 == numpy.array(
        [3.6499, 0.37028658, 2.2142494], dtype=numpy.float32))
//  [python]
def test_get_zinc_smiles():
    # test smiles extraction and dataset order
    pp = AtomicNumberPreprocessor()
    target_index = [0, 7777, 249454]  # set target_index for fast testing...
    dataset, smiles = zinc.get_zinc250k(preprocessor=pp, return_smiles=True,
                                        target_index=target_index)

    # --- Test dataset is correctly obtained ---
    index = numpy.random.choice(len(dataset), None)
    atoms, label = dataset[index]

    assert atoms.ndim == 1  # (atom, )
    assert atoms.dtype == numpy.int32
    # (atom from, atom to) or (edge_type, atom from, atom to)
    assert label.ndim == 1
    assert label.shape[0] == ZINC250K_NUM_LABEL
    assert label.dtype == numpy.float32

    # --- Test number of dataset ---
    assert len(dataset) == len(target_index)
    assert len(smiles) == len(target_index)

    # --- Test order of dataset ---
    assert smiles[0] == 'CC(C)(C)c1ccc2occ(CC(=O)Nc3ccccc3F)c2c1'
    atoms0, labels0 = dataset[0]
    assert numpy.all(atoms0 == numpy.array(
        [6, 6, 6, 6, 6, 6, 6, 6, 8, 6, 6, 6, 6, 8, 7, 6, 6, 6, 6, 6, 6, 9, 6,
         6], dtype=numpy.int32))
    assert numpy.all(labels0 == numpy.array(
        [5.0506, 0.70201224, 2.0840945], dtype=numpy.float32))

    assert smiles[1] == 'CCCc1cc(NC(=O)Nc2ccc3c(c2)OCCO3)n(C)n1'
    atoms7777, labels7777 = dataset[1]
    assert numpy.all(atoms7777 == numpy.array(
        [6, 6, 6, 6, 6, 6, 7, 6, 8, 7, 6, 6, 6, 6, 6, 6, 8, 6, 6, 8, 7, 6, 7],
        dtype=numpy.int32))
    assert numpy.all(labels7777 == numpy.array(
        [2.7878, 0.9035222, 2.3195992], dtype=numpy.float32))

    assert smiles[2] == 'O=C(CC(c1ccccc1)c1ccccc1)N1CCN(S(=O)(=O)c2ccccc2[N+](=O)[O-])CC1'  # NOQA
    atoms249454, labels249454 = dataset[2]
    assert numpy.all(atoms249454 == numpy.array(
        [8,  6,  6,  6,  6,  6,  6,  6,  6,  6,  6,  6,  6,  6,  6,  6,  7,
         6,  6,  7, 16,  8,  8,  6,  6,  6,  6,  6,  6,  7,  8,  8,  6,  6],
        dtype=numpy.int32))
    assert numpy.all(labels249454 == numpy.array(
        [3.6499, 0.37028658, 2.2142494], dtype=numpy.float32))