jiahuimbzuai/CoQuIR huggingface dataset:
//  [python]
def test_zero(image, measurements, module, objects, workspace):
    """Make sure we can process a blank image"""
    image.pixel_data = numpy.zeros((10, 10))

    objects.segmented = numpy.zeros((10, 10))

    module.run(workspace)

    for category, features in (
        (
            cellprofiler.modules.measureobjectintensity.INTENSITY,
            cellprofiler.modules.measureobjectintensity.ALL_MEASUREMENTS,
        ),
        (
            C_LOCATION,
            cellprofiler.modules.measureobjectintensity.ALL_LOCATION_MEASUREMENTS,
        ),
    ):
        for meas_name in features:
            feature_name = "%s_%s_%s" % (category, meas_name, "MyImage")

            data = measurements.get_current_measurement("MyObjects", feature_name)

            assert numpy.product(data.shape) == 0, (
                "Got data for feature %s" % feature_name
            )

        assert_features_and_columns_match(measurements, module)
//  [python]
def test_zero(image, measurements, module, objects, workspace):
    """Make sure we can process a blank image"""
    image.pixel_data = numpy.zeros((10, 10))

    objects.segmented = numpy.zeros((10, 10))

    module.run(workspace)

    for category, features in (
        (
            cellprofiler.modules.measureobjectintensity.INTENSITY,
            cellprofiler.modules.measureobjectintensity.ALL_MEASUREMENTS,
        ),
        (
            C_LOCATION,
            cellprofiler.modules.measureobjectintensity.ALL_LOCATION_MEASUREMENTS,
        ),
    ):
        for meas_name in features:
            feature_name = "%s_%s_%s" % (category, meas_name, "MyImage")

            data = measurements.get_current_measurement("MyObjects", feature_name)

            assert numpy.prod(data.shape) == 0, (
                "Got data for feature %s" % feature_name
            )

        assert_features_and_columns_match(measurements, module)
//  [python]
def _convert_from_encoding(self, byte_reader, _should_freeze: bool = False) -> np.ndarray:
        shape = [byte_reader.read_int64() for i in range(self.ndim)]
        total_num_elements = np.product(shape, dtype=np.int64)

        if self.element_type in _numeric_types:
            element_byte_size = self.element_type._byte_size
            bytes_to_read = element_byte_size * total_num_elements
            buffer = byte_reader.read_bytes_view(bytes_to_read)
            return np.frombuffer(buffer, self.element_type.to_numpy, count=total_num_elements).reshape(shape)
        else:
            elements = [
                self.element_type._convert_from_encoding(byte_reader, _should_freeze) for i in range(total_num_elements)
            ]
            np_type = self.element_type.to_numpy()
            return np.ndarray(shape=shape, buffer=np.array(elements, dtype=np_type), dtype=np_type, order="F")
//  [python]
def _convert_from_encoding(self, byte_reader, _should_freeze: bool = False) -> np.ndarray:
        shape = [byte_reader.read_int64() for i in range(self.ndim)]
        total_num_elements = np.prod(shape, dtype=np.int64)

        if self.element_type in _numeric_types:
            element_byte_size = self.element_type._byte_size
            bytes_to_read = element_byte_size * total_num_elements
            buffer = byte_reader.read_bytes_view(bytes_to_read)
            return np.frombuffer(buffer, self.element_type.to_numpy, count=total_num_elements).reshape(shape)
        else:
            elements = [
                self.element_type._convert_from_encoding(byte_reader, _should_freeze) for i in range(total_num_elements)
            ]
            np_type = self.element_type.to_numpy()
            return np.ndarray(shape=shape, buffer=np.array(elements, dtype=np_type), dtype=np_type, order="F")
//  [python]
def _exchange(self, data):
        # first convert to array
        data = promote(data, self.comm)

        if any(self.comm.allgather(len(data) != self.sendlength)):
            raise ValueError(
            'the length of data does not match that used to build the layout')

        #build buffer
        # Watch out:
        # take produces C-contiguous array,
        # friendly to alltoallv.
        # fancy indexing does not always return C_contiguous
        # array (2 days to realize this!)

        buffer = data.take(self.indices, axis=0)

        # build a dtype for communication
        # this is to avoid 2GB limit from bytes.
        duplicity = numpy.product(numpy.array(data.shape[1:], 'intp'))
        itemsize = duplicity * data.dtype.itemsize

        dt = MPI.BYTE.Create_contiguous(itemsize)
        dt.Commit()
        dtype = numpy.dtype((data.dtype, data.shape[1:]))
        recvbuffer = numpy.empty(self.recvlength, dtype=dtype, order='C')
        self.comm.Barrier()

        # now fire
        rt = self.comm.Alltoallv((buffer, (self.sendcounts, self.sendoffsets), dt),
                            (recvbuffer, (self.recvcounts, self.recvoffsets), dt))
        dt.Free()
        self.comm.Barrier()
        return recvbuffer
//  [python]
def _exchange(self, data):
        # first convert to array
        data = promote(data, self.comm)

        if any(self.comm.allgather(len(data) != self.sendlength)):
            raise ValueError(
            'the length of data does not match that used to build the layout')

        #build buffer
        # Watch out:
        # take produces C-contiguous array,
        # friendly to alltoallv.
        # fancy indexing does not always return C_contiguous
        # array (2 days to realize this!)

        buffer = data.take(self.indices, axis=0)

        # build a dtype for communication
        # this is to avoid 2GB limit from bytes.
        duplicity = numpy.prod(numpy.array(data.shape[1:], 'intp'))
        itemsize = duplicity * data.dtype.itemsize

        dt = MPI.BYTE.Create_contiguous(itemsize)
        dt.Commit()
        dtype = numpy.dtype((data.dtype, data.shape[1:]))
        recvbuffer = numpy.empty(self.recvlength, dtype=dtype, order='C')
        self.comm.Barrier()

        # now fire
        rt = self.comm.Alltoallv((buffer, (self.sendcounts, self.sendoffsets), dt),
                            (recvbuffer, (self.recvcounts, self.recvoffsets), dt))
        dt.Free()
        self.comm.Barrier()
        return recvbuffer
//  [python]
def __init__(self,
            edges,
            comm=MPI.COMM_WORLD,
            periodic=True,
            DomainAssign=None):
        """
        DomainAssign records each domain is assigned to which rank
        """
        self.shape = numpy.array([len(g) - 1 for g in edges], dtype='int32')
        self.ndim = len(self.shape)
        self.edges = [numpy.asarray(g) for g in edges]
        self.periodic = periodic
        self.comm = comm
        self.size = numpy.product(self.shape)

        if DomainAssign is None:
            if comm.size >= self.size:
                DomainAssign = numpy.array(range(self.size), dtype='int32')
            else:
                DomainAssign = numpy.empty(self.size, dtype='int32')
                for i in range(comm.size):
                    start = i * self.size // comm.size
                    end = (i + 1) * self.size // comm.size
                    DomainAssign[start:end] = i

        self.DomainAssign = DomainAssign

        dd = numpy.zeros(self.shape, dtype='int16')

        for i, edge in enumerate(edges):
            edge = numpy.array(edge)
            dd1 = edge[1:] == edge[:-1]
            dd1 = dd1.reshape([-1 if ii == i else 1 for ii in range(self.ndim)])
            dd[...] |= dd1

        self.DomainDegenerate = dd.ravel()

        self._update_primary_regions()
//  [python]
def __init__(self,
            edges,
            comm=MPI.COMM_WORLD,
            periodic=True,
            DomainAssign=None):
        """
        DomainAssign records each domain is assigned to which rank
        """
        self.shape = numpy.array([len(g) - 1 for g in edges], dtype='int32')
        self.ndim = len(self.shape)
        self.edges = [numpy.asarray(g) for g in edges]
        self.periodic = periodic
        self.comm = comm
        self.size = numpy.prod(self.shape)

        if DomainAssign is None:
            if comm.size >= self.size:
                DomainAssign = numpy.array(range(self.size), dtype='int32')
            else:
                DomainAssign = numpy.empty(self.size, dtype='int32')
                for i in range(comm.size):
                    start = i * self.size // comm.size
                    end = (i + 1) * self.size // comm.size
                    DomainAssign[start:end] = i

        self.DomainAssign = DomainAssign

        dd = numpy.zeros(self.shape, dtype='int16')

        for i, edge in enumerate(edges):
            edge = numpy.array(edge)
            dd1 = edge[1:] == edge[:-1]
            dd1 = dd1.reshape([-1 if ii == i else 1 for ii in range(self.ndim)])
            dd[...] |= dd1

        self.DomainDegenerate = dd.ravel()

        self._update_primary_regions()
//  [python]
def square_domain(L=1.,N=10):
    dx=1./N
    
    x,y= numpy.mgrid[0:L+dx/2:dx,0:L+dx/2:dx]
    x1,y1= numpy.mgrid[dx/2:L+dx/2:dx,dx/2:L+dx/2:dx]
    
    s=x.shape
    s1=x1.shape
    
    x_=x.flatten()
    y_=y.flatten()
    x1_=x1.flatten()
    y1_=y1.flatten()
    
    index=numpy.arange(len(x_)+len(x1_))
    
    l1=numpy.product(x.shape)
    
    i=index[:l1].reshape(s)
    i1=index[l1:].reshape(s1)
    
    e1=numpy.zeros((N**2,3),dtype='i')
    e2=numpy.zeros((N**2,3),dtype='i')
    e3=numpy.zeros((N**2,3),dtype='i')
    e4=numpy.zeros((N**2,3),dtype='i')
    
    e1[:,0]=i[:-1,:-1].flatten()
    e1[:,1]=i[1:,:-1].flatten()
    e1[:,2]=i1[:,:].flatten()
    
    e2[:,0]=i[1:,:-1].flatten()
    e2[:,1]=i[1:,1:].flatten()
    e2[:,2]=i1[:,:].flatten()
    
    e3[:,0]=i[1:,1:].flatten()
    e3[:,1]=i[:-1,1:].flatten()
    e3[:,2]=i1[:,:].flatten()
    
    e4[:,0]=i[:-1,:-1].flatten()
    e4[:,1]=i1[:,:].flatten()
    e4[:,2]=i[:-1,1:].flatten()
    
    x=numpy.concatenate([x_,x1_])
    y=numpy.concatenate([y_,y1_])
    
    elements=numpy.zeros((4*N**2,3),dtype='i')
    elements[0::4,:]=e1
    elements[1::4,:]=e2
    elements[2::4,:]=e3
    elements[3::4,:]=e4
  
    boundaries=[xx.flatten() for xx in [i[:,0],i[-1,:],i[::-1,-1],i[0,::-1]] ]
  
    return x,y,elements,boundaries
//  [python]
def square_domain(L=1.,N=10):
    dx=1./N
    
    x,y= numpy.mgrid[0:L+dx/2:dx,0:L+dx/2:dx]
    x1,y1= numpy.mgrid[dx/2:L+dx/2:dx,dx/2:L+dx/2:dx]
    
    s=x.shape
    s1=x1.shape
    
    x_=x.flatten()
    y_=y.flatten()
    x1_=x1.flatten()
    y1_=y1.flatten()
    
    index=numpy.arange(len(x_)+len(x1_))
    
    l1=numpy.prod(x.shape)
    
    i=index[:l1].reshape(s)
    i1=index[l1:].reshape(s1)
    
    e1=numpy.zeros((N**2,3),dtype='i')
    e2=numpy.zeros((N**2,3),dtype='i')
    e3=numpy.zeros((N**2,3),dtype='i')
    e4=numpy.zeros((N**2,3),dtype='i')
    
    e1[:,0]=i[:-1,:-1].flatten()
    e1[:,1]=i[1:,:-1].flatten()
    e1[:,2]=i1[:,:].flatten()
    
    e2[:,0]=i[1:,:-1].flatten()
    e2[:,1]=i[1:,1:].flatten()
    e2[:,2]=i1[:,:].flatten()
    
    e3[:,0]=i[1:,1:].flatten()
    e3[:,1]=i[:-1,1:].flatten()
    e3[:,2]=i1[:,:].flatten()
    
    e4[:,0]=i[:-1,:-1].flatten()
    e4[:,1]=i1[:,:].flatten()
    e4[:,2]=i[:-1,1:].flatten()
    
    x=numpy.concatenate([x_,x1_])
    y=numpy.concatenate([y_,y1_])
    
    elements=numpy.zeros((4*N**2,3),dtype='i')
    elements[0::4,:]=e1
    elements[1::4,:]=e2
    elements[2::4,:]=e3
    elements[3::4,:]=e4
  
    boundaries=[xx.flatten() for xx in [i[:,0],i[-1,:],i[::-1,-1],i[0,::-1]] ]
  
    return x,y,elements,boundaries
//  [python]
def _read_field(raw_file, field_name):

    header_file = raw_file + field_name + "_H"
    boxes, file_names, offsets, header = _read_header(header_file)

    dom_lo, dom_hi = _combine_boxes(boxes)
    data_shape = dom_hi - dom_lo + 1
    if header.ncomp > 1:
        data_shape = np.append(data_shape, header.ncomp)
    data = np.zeros(data_shape)

    for box, fn, offset in zip(boxes, file_names, offsets):
        lo = box[0] - dom_lo
        hi = box[1] - dom_lo
        shape = hi - lo + 1
        if header.ncomp > 1:
            shape = np.append(shape, header.ncomp)
        with open(raw_file + fn, "rb") as f:
            f.seek(offset)
            if (header.version == 1):
                f.readline()  # skip the first line
            arr = np.fromfile(f, 'float64', np.product(shape))
            arr = arr.reshape(shape, order='F')
            box_shape = [slice(l,h+1) for l, h in zip(lo, hi)]
            if header.ncomp > 1:
                box_shape += [slice(None)]
            data[tuple(box_shape)] = arr

    return data
//  [python]
def _read_field(raw_file, field_name):

    header_file = raw_file + field_name + "_H"
    boxes, file_names, offsets, header = _read_header(header_file)

    dom_lo, dom_hi = _combine_boxes(boxes)
    data_shape = dom_hi - dom_lo + 1
    if header.ncomp > 1:
        data_shape = np.append(data_shape, header.ncomp)
    data = np.zeros(data_shape)

    for box, fn, offset in zip(boxes, file_names, offsets):
        lo = box[0] - dom_lo
        hi = box[1] - dom_lo
        shape = hi - lo + 1
        if header.ncomp > 1:
            shape = np.append(shape, header.ncomp)
        with open(raw_file + fn, "rb") as f:
            f.seek(offset)
            if (header.version == 1):
                f.readline()  # skip the first line
            arr = np.fromfile(f, 'float64', np.prod(shape))
            arr = arr.reshape(shape, order='F')
            box_shape = [slice(l,h+1) for l, h in zip(lo, hi)]
            if header.ncomp > 1:
                box_shape += [slice(None)]
            data[tuple(box_shape)] = arr

    return data
//  [python]
def test_addsumprod (self):
        "Tests add, sum, product."
        (x, y, a10, m1, m2, xm, ym, z, zm, xf) = self.d
        assert_equal(numpy.add.reduce(x), add.reduce(x))
        assert_equal(numpy.add.accumulate(x), add.accumulate(x))
        assert_equal(4, sum(array(4),axis=0))
        assert_equal(4, sum(array(4), axis=0))
        assert_equal(numpy.sum(x,axis=0), sum(x,axis=0))
        assert_equal(numpy.sum(filled(xm,0),axis=0), sum(xm,axis=0))
        assert_equal(numpy.sum(x,0), sum(x,0))
        assert_equal(numpy.product(x,axis=0), product(x,axis=0))
        assert_equal(numpy.product(x,0), product(x,0))
        assert_equal(numpy.product(filled(xm,1),axis=0), product(xm,axis=0))
        s = (3,4)
        x.shape = y.shape = xm.shape = ym.shape = s
        if len(s) > 1:
            assert_equal(numpy.concatenate((x,y),1), concatenate((xm,ym),1))
            assert_equal(numpy.add.reduce(x,1), add.reduce(x,1))
            assert_equal(numpy.sum(x,1), sum(x,1))
            assert_equal(numpy.product(x,1), product(x,1))
//  [python]
def test_addsumprod (self):
        "Tests add, sum, product."
        (x, y, a10, m1, m2, xm, ym, z, zm, xf) = self.d
        assert_equal(numpy.add.reduce(x), add.reduce(x))
        assert_equal(numpy.add.accumulate(x), add.accumulate(x))
        assert_equal(4, sum(array(4),axis=0))
        assert_equal(4, sum(array(4), axis=0))
        assert_equal(numpy.sum(x,axis=0), sum(x,axis=0))
        assert_equal(numpy.sum(filled(xm,0),axis=0), sum(xm,axis=0))
        assert_equal(numpy.sum(x,0), sum(x,0))
        assert_equal(numpy.prod(x,axis=0), product(x,axis=0))
        assert_equal(numpy.prod(x,0), product(x,0))
        assert_equal(numpy.prod(filled(xm,1),axis=0), product(xm,axis=0))
        s = (3,4)
        x.shape = y.shape = xm.shape = ym.shape = s
        if len(s) > 1:
            assert_equal(numpy.concatenate((x,y),1), concatenate((xm,ym),1))
            assert_equal(numpy.add.reduce(x,1), add.reduce(x,1))
            assert_equal(numpy.sum(x,1), sum(x,1))
            assert_equal(numpy.prod(x,1), product(x,1))
//  [python]
def coverage(self, bin_size=50):
        '''
        Compute the coverage of the blend space by the input tensors.

        Returns NumPy 2D arrays ``(fill, magnitude, src)``. ``fill``
        indicates how densely filled each "bin" is, from 0.0 (empty)
        to 1.0 (full). ``magnitude`` accumulates the absolute values
        of the items within the bin. ``src`` indicates which tensor
        each item comes from, specified by its index in the
        ``tensors`` array.  (If multiple tensors write in the same
        bin, the last one wins.)
        '''
        if not isinstance(bin_size, (list, tuple)):
            bin_size = [bin_size]*self.ndim

        import numpy
        src = numpy.zeros(tuple(numpy.ceil(float(items) / float(bins))
                                for items, bins in izip(self.shape, bin_size)),
                          dtype=numpy.uint8)
        magnitude = numpy.zeros(src.shape)
        fill = numpy.zeros(src.shape)
        inc = 1.0 / numpy.product(bin_size)

        # This loop should look a lot like the one in FakeTensor.
        labels = self._labels
        for tensor_idx, tensor in enumerate(self._tensors):
            for key, val in tensor.iteritems():
                idx = tuple(label_list.index(label) // bins for label_list, label, bins in izip(labels, key, bin_size))
                src[idx] = tensor_idx
                fill[idx] += inc
                magnitude[idx] += abs(val)
        return fill, magnitude, src
//  [python]
def coverage(self, bin_size=50):
        '''
        Compute the coverage of the blend space by the input tensors.

        Returns NumPy 2D arrays ``(fill, magnitude, src)``. ``fill``
        indicates how densely filled each "bin" is, from 0.0 (empty)
        to 1.0 (full). ``magnitude`` accumulates the absolute values
        of the items within the bin. ``src`` indicates which tensor
        each item comes from, specified by its index in the
        ``tensors`` array.  (If multiple tensors write in the same
        bin, the last one wins.)
        '''
        if not isinstance(bin_size, (list, tuple)):
            bin_size = [bin_size]*self.ndim

        import numpy
        src = numpy.zeros(tuple(numpy.ceil(float(items) / float(bins))
                                for items, bins in izip(self.shape, bin_size)),
                          dtype=numpy.uint8)
        magnitude = numpy.zeros(src.shape)
        fill = numpy.zeros(src.shape)
        inc = 1.0 / numpy.prod(bin_size)

        # This loop should look a lot like the one in FakeTensor.
        labels = self._labels
        for tensor_idx, tensor in enumerate(self._tensors):
            for key, val in tensor.iteritems():
                idx = tuple(label_list.index(label) // bins for label_list, label, bins in izip(labels, key, bin_size))
                src[idx] = tensor_idx
                fill[idx] += inc
                magnitude[idx] += abs(val)
        return fill, magnitude, src
//  [python]
def _read_buffer(snapshot, header_fn, _component_names):

    boxes, file_names, offsets, header = _read_header(header_fn)

    dom_lo, dom_hi = _combine_boxes(boxes)

    all_data = {}
    for i in range(header.ncomp):
        all_data[_component_names[i]] = np.zeros(dom_hi - dom_lo + 1)

    for box, fn, offset in zip(boxes, file_names, offsets):
        lo = box[0] - dom_lo
        hi = box[1] - dom_lo
        shape = hi - lo + 1
        size = np.product(shape)
        with open(snapshot + "/Level_0/" + fn, "rb") as f:
            f.seek(offset)
            if (header.version == 1):
                f.readline()  # skip the first line
            arr = np.fromfile(f, 'float64', header.ncomp*size)
            for i in range(header.ncomp):
                comp_data = arr[i*size:(i+1)*size].reshape(shape, order='F')
                data = all_data[_component_names[i]]
                data[tuple([slice(l,h+1) for l, h in zip(lo, hi)])] = comp_data
                all_data[_component_names[i]] = data
    return all_data
//  [python]
def _read_buffer(snapshot, header_fn, _component_names):

    boxes, file_names, offsets, header = _read_header(header_fn)

    dom_lo, dom_hi = _combine_boxes(boxes)

    all_data = {}
    for i in range(header.ncomp):
        all_data[_component_names[i]] = np.zeros(dom_hi - dom_lo + 1)

    for box, fn, offset in zip(boxes, file_names, offsets):
        lo = box[0] - dom_lo
        hi = box[1] - dom_lo
        shape = hi - lo + 1
        size = np.prod(shape)
        with open(snapshot + "/Level_0/" + fn, "rb") as f:
            f.seek(offset)
            if (header.version == 1):
                f.readline()  # skip the first line
            arr = np.fromfile(f, 'float64', header.ncomp*size)
            for i in range(header.ncomp):
                comp_data = arr[i*size:(i+1)*size].reshape(shape, order='F')
                data = all_data[_component_names[i]]
                data[tuple([slice(l,h+1) for l, h in zip(lo, hi)])] = comp_data
                all_data[_component_names[i]] = data
    return all_data
//  [python]
def test_zero(image, measurements, module, objects, workspace):
    """Make sure we can process a blank image"""
    image.pixel_data = numpy.zeros((10, 10, N_CHANNELS))

    objects.segmented = numpy.zeros((10, 10))

    module.nchannels.value = N_CHANNELS

    module.run(workspace)

    for category, features in (
        (
            momc.INTENSITY,
            momc.ALL_MEASUREMENTS,
        ),
        (
            C_LOCATION,
            momc.ALL_LOCATION_MEASUREMENTS,
        ),
    ):
        for meas_name in features:
            for c in range(N_CHANNELS):
                feature_name = "%s_%s_%s_c%s" % (category, meas_name, "MyImage", c + 1)

                data = measurements.get_current_measurement("MyObjects", feature_name)

                assert numpy.product(data.shape) == 0, (
                    "Got data for feature %s" % feature_name
                )

        assert_features_and_columns_match(measurements, module)
//  [python]
def test_zero(image, measurements, module, objects, workspace):
    """Make sure we can process a blank image"""
    image.pixel_data = numpy.zeros((10, 10, N_CHANNELS))

    objects.segmented = numpy.zeros((10, 10))

    module.nchannels.value = N_CHANNELS

    module.run(workspace)

    for category, features in (
        (
            momc.INTENSITY,
            momc.ALL_MEASUREMENTS,
        ),
        (
            C_LOCATION,
            momc.ALL_LOCATION_MEASUREMENTS,
        ),
    ):
        for meas_name in features:
            for c in range(N_CHANNELS):
                feature_name = "%s_%s_%s_c%s" % (category, meas_name, "MyImage", c + 1)

                data = measurements.get_current_measurement("MyObjects", feature_name)

                assert numpy.prod(data.shape) == 0, (
                    "Got data for feature %s" % feature_name
                )

        assert_features_and_columns_match(measurements, module)
//  [python]
def basis_element(self, x, i, d=0):
        """
        Evaluate a particular basis element of the BSpline,
        or its derivative.

        INPUTS:
           x  -- x values at which to evaluate the basis element
           i  -- which element of the BSpline to return
           d  -- the order of derivative

        OUTPUTS: y
           y  -- value of d-th derivative of the i-th basis element
                 of the BSpline at specified x values
        """

        x = np.asarray(x, np.float64)
        _shape = x.shape
        if _shape == ():
            x.shape = (1,)
        x.shape = (np.product(_shape,axis=0),)
        if i < self.tau.shape[0] - 1:
            # TODO: OWNDATA flags...
            v = _hbspline.evaluate(x, self.tau, self.m, d, i, i+1)
        else:
            return np.zeros(x.shape, np.float64)

        if (i == self.tau.shape[0] - self.m):
            v = np.where(np.equal(x, self.tau[-1]), 1, v)
        v.shape = _shape
        return v
//  [python]
def basis_element(self, x, i, d=0):
        """
        Evaluate a particular basis element of the BSpline,
        or its derivative.

        INPUTS:
           x  -- x values at which to evaluate the basis element
           i  -- which element of the BSpline to return
           d  -- the order of derivative

        OUTPUTS: y
           y  -- value of d-th derivative of the i-th basis element
                 of the BSpline at specified x values
        """

        x = np.asarray(x, np.float64)
        _shape = x.shape
        if _shape == ():
            x.shape = (1,)
        x.shape = (np.prod(_shape,axis=0),)
        if i < self.tau.shape[0] - 1:
            # TODO: OWNDATA flags...
            v = _hbspline.evaluate(x, self.tau, self.m, d, i, i+1)
        else:
            return np.zeros(x.shape, np.float64)

        if (i == self.tau.shape[0] - self.m):
            v = np.where(np.equal(x, self.tau[-1]), 1, v)
        v.shape = _shape
        return v
//  [python]
def handle_monocular(self, msg):

        (image, camera) = msg
        gray = self.mkgray(image)
        C, ids = self.image_corners(gray)
        if C is not None:
            linearity_rms = self.mc.linear_error(C, ids, self.board)

            # Add in reprojection check
            image_points = C
            object_points = self.mc.mk_object_points([self.board], use_board_size=True)[0]
            dist_coeffs = numpy.zeros((4, 1))
            camera_matrix = numpy.array( [ [ camera.P[0], camera.P[1], camera.P[2]  ],
                                           [ camera.P[4], camera.P[5], camera.P[6]  ],
                                           [ camera.P[8], camera.P[9], camera.P[10] ] ] )
            ok, rot, trans = cv2.solvePnP(object_points, image_points, camera_matrix, dist_coeffs)
            # Convert rotation into a 3x3 Rotation Matrix
            rot3x3, _ = cv2.Rodrigues(rot)
            # Reproject model points into image
            object_points_world = numpy.asmatrix(rot3x3) * numpy.asmatrix(object_points.squeeze().T) + numpy.asmatrix(trans)
            reprojected_h = camera_matrix * object_points_world
            reprojected   = (reprojected_h[0:2, :] / reprojected_h[2, :])
            reprojection_errors = image_points.squeeze().T - reprojected

            reprojection_rms = numpy.sqrt(numpy.sum(numpy.array(reprojection_errors) ** 2) / numpy.product(reprojection_errors.shape))

            # Print the results
            print("Linearity RMS Error: %.3f Pixels      Reprojection RMS Error: %.3f Pixels" % (linearity_rms, reprojection_rms))
        else:
            print('no chessboard')
//  [python]
def handle_monocular(self, msg):

        (image, camera) = msg
        gray = self.mkgray(image)
        C, ids = self.image_corners(gray)
        if C is not None:
            linearity_rms = self.mc.linear_error(C, ids, self.board)

            # Add in reprojection check
            image_points = C
            object_points = self.mc.mk_object_points([self.board], use_board_size=True)[0]
            dist_coeffs = numpy.zeros((4, 1))
            camera_matrix = numpy.array( [ [ camera.P[0], camera.P[1], camera.P[2]  ],
                                           [ camera.P[4], camera.P[5], camera.P[6]  ],
                                           [ camera.P[8], camera.P[9], camera.P[10] ] ] )
            ok, rot, trans = cv2.solvePnP(object_points, image_points, camera_matrix, dist_coeffs)
            # Convert rotation into a 3x3 Rotation Matrix
            rot3x3, _ = cv2.Rodrigues(rot)
            # Reproject model points into image
            object_points_world = numpy.asmatrix(rot3x3) * numpy.asmatrix(object_points.squeeze().T) + numpy.asmatrix(trans)
            reprojected_h = camera_matrix * object_points_world
            reprojected   = (reprojected_h[0:2, :] / reprojected_h[2, :])
            reprojection_errors = image_points.squeeze().T - reprojected

            reprojection_rms = numpy.sqrt(numpy.sum(numpy.array(reprojection_errors) ** 2) / numpy.prod(reprojection_errors.shape))

            # Print the results
            print("Linearity RMS Error: %.3f Pixels      Reprojection RMS Error: %.3f Pixels" % (linearity_rms, reprojection_rms))
        else:
            print('no chessboard')
//  [python]
def get_accessor_as_numpy(gltf_scene: GLTF, accessor_id: int) -> np.ndarray:
    """
    Get a numpy array of the data stored in a GLTF accessor.

    Args:
        gltf_scene (`gltflib.GLTF`):
            The GLTF scene.
        accessor_id (`int`):
            The id of the accessor to extract.

    Returns:
        data (`np.ndarray`):
            The data stored in the accessor.
    """
    gltf_model = gltf_scene.model
    accessor = gltf_model.accessors[accessor_id]

    dtype: np.dtype = gltf_to_numpy_dtypes_mapping[accessor.componentType]
    item_shape = gltf_to_numpy_shapes_mapping[accessor.type]
    total_shape = np.append(accessor.count, item_shape)
    total_size = np.abs(np.product(total_shape))

    if accessor.sparse is not None:
        raise NotImplementedError
    else:
        data = get_buffer_as_bytes(gltf_scene=gltf_scene, buffer_view_id=accessor.bufferView)
        array = np.frombuffer(buffer=data, offset=accessor.byteOffset, count=total_size, dtype=dtype)
        array = array.reshape(total_shape)

    return array
//  [python]
def get_accessor_as_numpy(gltf_scene: GLTF, accessor_id: int) -> np.ndarray:
    """
    Get a numpy array of the data stored in a GLTF accessor.

    Args:
        gltf_scene (`gltflib.GLTF`):
            The GLTF scene.
        accessor_id (`int`):
            The id of the accessor to extract.

    Returns:
        data (`np.ndarray`):
            The data stored in the accessor.
    """
    gltf_model = gltf_scene.model
    accessor = gltf_model.accessors[accessor_id]

    dtype: np.dtype = gltf_to_numpy_dtypes_mapping[accessor.componentType]
    item_shape = gltf_to_numpy_shapes_mapping[accessor.type]
    total_shape = np.append(accessor.count, item_shape)
    total_size = np.abs(np.prod(total_shape))

    if accessor.sparse is not None:
        raise NotImplementedError
    else:
        data = get_buffer_as_bytes(gltf_scene=gltf_scene, buffer_view_id=accessor.bufferView)
        array = np.frombuffer(buffer=data, offset=accessor.byteOffset, count=total_size, dtype=dtype)
        array = array.reshape(total_shape)

    return array
//  [python]
def basis(self, x, d=0, lower=None, upper=None):
        """
        Evaluate the basis of the BSpline or its derivative.
        If lower or upper is specified, then only
        the [lower:upper] elements of the basis are returned.

        INPUTS:
           x     -- x values at which to evaluate the basis element
           i     -- which element of the BSpline to return
           d     -- the order of derivative
           lower -- optional lower limit of the set of basis
                    elements
           upper -- optional upper limit of the set of basis
                    elements

        OUTPUTS: y
           y  -- value of d-th derivative of the basis elements
                 of the BSpline at specified x values
        """
        x = np.asarray(x)
        _shape = x.shape
        if _shape == ():
            x.shape = (1,)
        x.shape = (np.product(_shape,axis=0),)

        if upper is None:
            upper = self.tau.shape[0] - self.m
        if lower is None:
            lower = 0
        upper = min(upper, self.tau.shape[0] - self.m)
        lower = max(0, lower)

        d = np.asarray(d)
        if d.shape == ():
            v = _hbspline.evaluate(x, self.tau, self.m, int(d), lower, upper)
        else:
            if d.shape[0] != 2:
                raise ValueError("if d is not an integer, expecting a jx2 \
                   array with first row indicating order \
                   of derivative, second row coefficient in front.")
            v = 0
            for i in range(d.shape[1]):
                v += d[1,i] * _hbspline.evaluate(x, self.tau, self.m, d[0,i], lower, upper)

        v.shape = (upper-lower,) + _shape
        if upper == self.tau.shape[0] - self.m:
            v[-1] = np.where(np.equal(x, self.tau[-1]), 1, v[-1])
        return v
//  [python]
def basis(self, x, d=0, lower=None, upper=None):
        """
        Evaluate the basis of the BSpline or its derivative.
        If lower or upper is specified, then only
        the [lower:upper] elements of the basis are returned.

        INPUTS:
           x     -- x values at which to evaluate the basis element
           i     -- which element of the BSpline to return
           d     -- the order of derivative
           lower -- optional lower limit of the set of basis
                    elements
           upper -- optional upper limit of the set of basis
                    elements

        OUTPUTS: y
           y  -- value of d-th derivative of the basis elements
                 of the BSpline at specified x values
        """
        x = np.asarray(x)
        _shape = x.shape
        if _shape == ():
            x.shape = (1,)
        x.shape = (np.prod(_shape,axis=0),)

        if upper is None:
            upper = self.tau.shape[0] - self.m
        if lower is None:
            lower = 0
        upper = min(upper, self.tau.shape[0] - self.m)
        lower = max(0, lower)

        d = np.asarray(d)
        if d.shape == ():
            v = _hbspline.evaluate(x, self.tau, self.m, int(d), lower, upper)
        else:
            if d.shape[0] != 2:
                raise ValueError("if d is not an integer, expecting a jx2 \
                   array with first row indicating order \
                   of derivative, second row coefficient in front.")
            v = 0
            for i in range(d.shape[1]):
                v += d[1,i] * _hbspline.evaluate(x, self.tau, self.m, d[0,i], lower, upper)

        v.shape = (upper-lower,) + _shape
        if upper == self.tau.shape[0] - self.m:
            v[-1] = np.where(np.equal(x, self.tau[-1]), 1, v[-1])
        return v
//  [python]
def makeFit(self, images):
        xVals = map(numpy.mean, images)
        yVals = numpy.array(images)
        imageShape = yVals.shape[1:]
        yVals.shape = len(xVals), numpy.product(imageShape)
        slopes, intercepts = numpy.polyfit(xVals, yVals, 1)
        slopes.shape = imageShape

        return numpy.array([images[0], slopes])
//  [python]
def makeFit(self, images):
        xVals = map(numpy.mean, images)
        yVals = numpy.array(images)
        imageShape = yVals.shape[1:]
        yVals.shape = len(xVals), numpy.prod(imageShape)
        slopes, intercepts = numpy.polyfit(xVals, yVals, 1)
        slopes.shape = imageShape

        return numpy.array([images[0], slopes])
//  [python]
def calc_final_radiance_jacobian(self, meas_spec, out_hdf, fm_err_grp):
        log_info("Calculating FM + Jacobians for final state vector update (unperturbed)\n")
        skip_jacobian = False
        unpert_spec = self.lua_config.fm.config.forward_model.radiance_all(skip_jacobian)
        unpert_spec_range = unpert_spec.spectral_range.convert(meas_spec.spectral_range.units)

        unpert_grp = fm_err_grp.create_group("Unperturbed")
        create_sounding_dataset(unpert_grp, "modeled_radiance", unpert_spec_range.data, "SciColor", unpert_spec_range.units.name)

        jacobian_data = self.lua_config.conn_solver.jacobian
        if numpy.product(jacobian_data.shape) > 0:
            create_sounding_dataset(out_hdf, "Jacobian", jacobian_data, ("SciColor", "SVElem"))

        return unpert_spec_range
//  [python]
def calc_final_radiance_jacobian(self, meas_spec, out_hdf, fm_err_grp):
        log_info("Calculating FM + Jacobians for final state vector update (unperturbed)\n")
        skip_jacobian = False
        unpert_spec = self.lua_config.fm.config.forward_model.radiance_all(skip_jacobian)
        unpert_spec_range = unpert_spec.spectral_range.convert(meas_spec.spectral_range.units)

        unpert_grp = fm_err_grp.create_group("Unperturbed")
        create_sounding_dataset(unpert_grp, "modeled_radiance", unpert_spec_range.data, "SciColor", unpert_spec_range.units.name)

        jacobian_data = self.lua_config.conn_solver.jacobian
        if numpy.prod(jacobian_data.shape) > 0:
            create_sounding_dataset(out_hdf, "Jacobian", jacobian_data, ("SciColor", "SVElem"))

        return unpert_spec_range
//  [python]
def test_blockdiagonal(self):
        knots1 = [0,0,0.4,1,1]
        degree = 1
        basis1 = BSplineBasis(knots1,degree)

        knots2 = [0.,0.,0.5,1,1]
        degree = 1
        basis2 = BSplineBasis(knots2,degree)

        basis3 = MonomialBasis(3)

        mbasis1 = TensorBasis([basis1,basis2])
        mbasis2 = TensorBasis([basis2,basis3])

        shape1 = [3,3,1,1]
        shape2 = [3,4,1,1]
        coeff1 = DTensor(numpy.random.randn(numpy.product(shape1),1),shape1)
        coeff2 = DTensor(numpy.random.randn(numpy.product(shape2),1),shape2)
        func1 = Function(mbasis1,coeff1)
        func2 = Function(mbasis2,coeff2)

        func3 = blkdiag(func1, func2)
        self.assertEqual(func3.shape,[2,2])
        self.assertEqualT(func3(0.5,0.2),DTensor([func1(0.5,0.2),0,0,func2(0.5,0.2)],[2,2]))

        constant1 = Function(DTensor([],[2,0]))
        func4 = blkdiag(func1, constant1)
        self.assertEqual(func4.shape,[3,1])
        self.assertEqualTensor(func4(0.5,0.2),DTensor([func1(0.5,0.2),0,0],[3,1]))

        constant2 = Function(DTensor([2,3],[1,2]))
        func5 = blkdiag(func1, constant2)
        self.assertEqual(func5.shape,[2,3])
        self.assertEqualT(func5(0.5,0.2),DTensor([func1(0.5,0.2),0,0,2,0,3],[2,3]))

        # blkdiag of 3 functions
        mbasis3 = TensorBasis([basis1,basis3])
        shape3 = [3,4,1,1]
        coeff3 = DTensor(numpy.random.randn(numpy.product(shape3),1),shape3)
        func6 = Function(mbasis3, coeff3)
        func7 = blkdiag(func1, func2, func6)
        self.assertEqual(func7.shape,[3,3])
        self.assertEqualT(func7(0.5,0.2),DTensor([func1(0.5,0.2),0,0,0,func2(0.5,0.2),0,0,0,func6(0.5,0.2)],[3,3]))

        # blkdiag with SX
        import casadi as cas
        constant3 = Function(DTensor([1],[1,1]))
        func10 = blkdiag(constant3, cas.SX.sym('x',1))
        self.assertEqual(func10.shape,[2,2])
//  [python]
def test_blockdiagonal(self):
        knots1 = [0,0,0.4,1,1]
        degree = 1
        basis1 = BSplineBasis(knots1,degree)

        knots2 = [0.,0.,0.5,1,1]
        degree = 1
        basis2 = BSplineBasis(knots2,degree)

        basis3 = MonomialBasis(3)

        mbasis1 = TensorBasis([basis1,basis2])
        mbasis2 = TensorBasis([basis2,basis3])

        shape1 = [3,3,1,1]
        shape2 = [3,4,1,1]
        coeff1 = DTensor(numpy.random.randn(numpy.prod(shape1),1),shape1)
        coeff2 = DTensor(numpy.random.randn(numpy.prod(shape2),1),shape2)
        func1 = Function(mbasis1,coeff1)
        func2 = Function(mbasis2,coeff2)

        func3 = blkdiag(func1, func2)
        self.assertEqual(func3.shape,[2,2])
        self.assertEqualT(func3(0.5,0.2),DTensor([func1(0.5,0.2),0,0,func2(0.5,0.2)],[2,2]))

        constant1 = Function(DTensor([],[2,0]))
        func4 = blkdiag(func1, constant1)
        self.assertEqual(func4.shape,[3,1])
        self.assertEqualTensor(func4(0.5,0.2),DTensor([func1(0.5,0.2),0,0],[3,1]))

        constant2 = Function(DTensor([2,3],[1,2]))
        func5 = blkdiag(func1, constant2)
        self.assertEqual(func5.shape,[2,3])
        self.assertEqualT(func5(0.5,0.2),DTensor([func1(0.5,0.2),0,0,2,0,3],[2,3]))

        # blkdiag of 3 functions
        mbasis3 = TensorBasis([basis1,basis3])
        shape3 = [3,4,1,1]
        coeff3 = DTensor(numpy.random.randn(numpy.prod(shape3),1),shape3)
        func6 = Function(mbasis3, coeff3)
        func7 = blkdiag(func1, func2, func6)
        self.assertEqual(func7.shape,[3,3])
        self.assertEqualT(func7(0.5,0.2),DTensor([func1(0.5,0.2),0,0,0,func2(0.5,0.2),0,0,0,func6(0.5,0.2)],[3,3]))

        # blkdiag with SX
        import casadi as cas
        constant3 = Function(DTensor([1],[1,1]))
        func10 = blkdiag(constant3, cas.SX.sym('x',1))
        self.assertEqual(func10.shape,[2,2])
//  [python]
def calcWordProbs(self, monomer_probs):  
        result = numpy.product(monomer_probs.take(self.m2w), axis=-1)
        # maybe simpler but slower, works ok:
        #result = numpy.product(monomer_probs ** (w2m, axis=-1))
        result /= result.sum()
        return result
//  [python]
def calcWordProbs(self, monomer_probs):  
        result = numpy.prod(monomer_probs.take(self.m2w), axis=-1)
        # maybe simpler but slower, works ok:
        #result = numpy.prod(monomer_probs ** (w2m, axis=-1))
        result /= result.sum()
        return result
//  [python]
def getColorCounts(filename):
    log.debug(f"Finding color occurrence for {filename}")

    im = Image.open(filename)
    if im.mode not in ['RGBA', 'RGB']:
        log.warning(f"Unable to detect colors in {filename} because it is stored with {im.mode} pixel format.")
        return [], []

    NUM_CLUSTERS = 16

    ar = np.asarray(im)
    shape = ar.shape
    ar = ar.reshape(numpy.product(shape[:2]), shape[2]).astype(float)

    with warnings.catch_warnings():
        warnings.simplefilter('ignore')
        codes, dist = scipy.cluster.vq.kmeans2(ar, NUM_CLUSTERS, minit='++')
        vecs, dist = scipy.cluster.vq.vq(ar, codes)  # assign codes
        counts, bins = numpy.histogram(vecs, len(codes))  # count occurrences

    log.debug('colors:')
    colors = [codeToIntArray(code) for code in codes]
    log.debug(colors)

    log.debug('counts:')
    log.debug(counts)

    return colors, counts
//  [python]
def getColorCounts(filename):
    log.debug(f"Finding color occurrence for {filename}")

    im = Image.open(filename)
    if im.mode not in ['RGBA', 'RGB']:
        log.warning(f"Unable to detect colors in {filename} because it is stored with {im.mode} pixel format.")
        return [], []

    NUM_CLUSTERS = 16

    ar = np.asarray(im)
    shape = ar.shape
    ar = ar.reshape(numpy.prod(shape[:2]), shape[2]).astype(float)

    with warnings.catch_warnings():
        warnings.simplefilter('ignore')
        codes, dist = scipy.cluster.vq.kmeans2(ar, NUM_CLUSTERS, minit='++')
        vecs, dist = scipy.cluster.vq.vq(ar, codes)  # assign codes
        counts, bins = numpy.histogram(vecs, len(codes))  # count occurrences

    log.debug('colors:')
    colors = [codeToIntArray(code) for code in codes]
    log.debug(colors)

    log.debug('counts:')
    log.debug(counts)

    return colors, counts
//  [python]
def calcWordProbs(self, monomer_probs):
        positions = range(self.word_length)
        assert len(monomer_probs) == self.m2w.shape[1], (
            len(monomer_probs), type(monomer_probs), self.m2w.shape)
        result = numpy.product(
            [monomer_probs[i].take(self.m2w[:,i]) 
            for i in positions], axis=0)
        result /= result.sum()
        return result
//  [python]
def calcWordProbs(self, monomer_probs):
        positions = range(self.word_length)
        assert len(monomer_probs) == self.m2w.shape[1], (
            len(monomer_probs), type(monomer_probs), self.m2w.shape)
        result = numpy.prod(
            [monomer_probs[i].take(self.m2w[:,i]) 
            for i in positions], axis=0)
        result /= result.sum()
        return result
//  [python]
def size_from(self, n=1):
		return numpy.product(self.win) * n
//  [python]
def size_from(self, n=1):
		return numpy.prod(self.win) * n
//  [python]
def sampler_factory(sampler_class, machine, machine_input, num_of_samples):
    sampler_class_obj = sampler_class
    while isinstance(sampler_class_obj, functools.partial):
        sampler_class_obj = sampler_class.func
    if issubclass(sampler_class_obj, MetropolisHastingsSampler):
        input_size = numpy.product(list(K.int_shape(machine_input)[1:]))
        model = Model(inputs=machine_input, outputs=machine.predictions)
        return sampler_class(model, num_of_samples, mini_batch_size=BATCH_SIZE,
                             num_of_chains=num_of_samples // (2 ** 7),
                             unused_sampels=input_size * 2)
    else:
        model = Model(inputs=machine_input, outputs=machine.conditional_log_probs)
        return sampler_class(model, num_of_samples, mini_batch_size=BATCH_SIZE)
//  [python]
def sampler_factory(sampler_class, machine, machine_input, num_of_samples):
    sampler_class_obj = sampler_class
    while isinstance(sampler_class_obj, functools.partial):
        sampler_class_obj = sampler_class.func
    if issubclass(sampler_class_obj, MetropolisHastingsSampler):
        input_size = numpy.prod(list(K.int_shape(machine_input)[1:]))
        model = Model(inputs=machine_input, outputs=machine.predictions)
        return sampler_class(model, num_of_samples, mini_batch_size=BATCH_SIZE,
                             num_of_chains=num_of_samples // (2 ** 7),
                             unused_sampels=input_size * 2)
    else:
        model = Model(inputs=machine_input, outputs=machine.conditional_log_probs)
        return sampler_class(model, num_of_samples, mini_batch_size=BATCH_SIZE)
//  [python]
def modify(self, name, value):
        """ Change the value of an attribute while preserving its type.

        Differs from __setitem__ in that if the attribute already exists, its
        type is preserved.  This can be very useful for interacting with
        externally generated files.

        If the attribute doesn't exist, it will be automatically created.
        """
        with phil:
            if not name in self:
                self[name] = value
            else:
                value = numpy.asarray(value, order='C')

                attr = h5a.open(self._id, self._e(name))

                if attr.get_space().get_simple_extent_type() == h5s.NULL:
                    raise IOError("Empty attributes can't be modified")

                # Allow the case of () <-> (1,)
                if (value.shape != attr.shape) and not \
                   (numpy.product(value.shape) == 1 and numpy.product(attr.shape) == 1):
                    raise TypeError("Shape of data is incompatible with existing attribute")
                attr.write(value)
//  [python]
def modify(self, name, value):
        """ Change the value of an attribute while preserving its type.

        Differs from __setitem__ in that if the attribute already exists, its
        type is preserved.  This can be very useful for interacting with
        externally generated files.

        If the attribute doesn't exist, it will be automatically created.
        """
        with phil:
            if not name in self:
                self[name] = value
            else:
                value = numpy.asarray(value, order='C')

                attr = h5a.open(self._id, self._e(name))

                if attr.get_space().get_simple_extent_type() == h5s.NULL:
                    raise IOError("Empty attributes can't be modified")

                # Allow the case of () <-> (1,)
                if (value.shape != attr.shape) and not \
                   (numpy.prod(value.shape) == 1 and numpy.prod(attr.shape) == 1):
                    raise TypeError("Shape of data is incompatible with existing attribute")
                attr.write(value)
//  [python]
def lhsFromDensity(kde, siz=100):
    '''
    LHS sampling from a variable's Kernel density estimate.

    :Parameters:
        - `kde`: scipy.stats.kde.gaussian_kde object
        - `siz`: Number or shape tuple for the output sample
    '''
    if not isinstance(kde, scipy.stats.kde.gaussian_kde):
        raise TypeError("kde is not a density object")
    if isinstance(siz, (tuple, list)):
        n = numpy.product(siz)
    s = kde.resample(n)
    v = lhsFromSample(s, n)
    if isinstance(siz, (tuple, list)):
        v.shape = siz
    return v
//  [python]
def lhsFromDensity(kde, siz=100):
    '''
    LHS sampling from a variable's Kernel density estimate.

    :Parameters:
        - `kde`: scipy.stats.kde.gaussian_kde object
        - `siz`: Number or shape tuple for the output sample
    '''
    if not isinstance(kde, scipy.stats.kde.gaussian_kde):
        raise TypeError("kde is not a density object")
    if isinstance(siz, (tuple, list)):
        n = numpy.prod(siz)
    s = kde.resample(n)
    v = lhsFromSample(s, n)
    if isinstance(siz, (tuple, list)):
        v.shape = siz
    return v