jiahuimbzuai/CoQuIR huggingface dataset:
//  [python]
def from_vector_static(self, vector, shapes):
        self._static_parameters = dict()
        start = 0
        for key, shape in shapes:
            end = start + numpy.product(shape)
            self._static_parameters[key] = numpy.reshape(vector[start:end], shape)
            start = end
        if(end != len(vector)):
            raise Exception("Mismatch shape info and vector. Len of shape: %d; Len of vector: %d"%(end, len(vector)))
//  [python]
def from_vector_static(self, vector, shapes):
        self._static_parameters = dict()
        start = 0
        for key, shape in shapes:
            end = start + numpy.prod(shape)
            self._static_parameters[key] = numpy.reshape(vector[start:end], shape)
            start = end
        if(end != len(vector)):
            raise Exception("Mismatch shape info and vector. Len of shape: %d; Len of vector: %d"%(end, len(vector)))
//  [python]
def prod(self, __axis: int) -> 'Array':
        '''Return the product of array elements over the given axes.

        Parameters
        ----------
        axis : :class:`int`, a sequence of :class:`int`, or ``None``
            The axis or axes along which the product is performed. ``None``, the
            default, implies all axes.

        Returns
        -------
        :class:`Array`
        '''

        return numpy.product(self, __axis)
//  [python]
def prod(self, __axis: int) -> 'Array':
        '''Return the product of array elements over the given axes.

        Parameters
        ----------
        axis : :class:`int`, a sequence of :class:`int`, or ``None``
            The axis or axes along which the product is performed. ``None``, the
            default, implies all axes.

        Returns
        -------
        :class:`Array`
        '''

        return numpy.prod(self, __axis)
//  [python]
def _compute_probability_of_success(self, failure_components):
    assert isinstance(failure_components, FailureListProductComponents)
    return numpy.product(failure_components.poss, axis=0)
//  [python]
def _compute_probability_of_success(self, failure_components):
    assert isinstance(failure_components, FailureListProductComponents)
    return numpy.prod(failure_components.poss, axis=0)
//  [python]
def __len__(self):
        length = 0
        for subspace in self.space.subspaces():
            if not subspace:
                continue
            sizes = [len(v_list) if isinstance(v_list, Distribution) else 1 for v_list in subspace]
            length += numpy.product(sizes)

        return length
//  [python]
def __len__(self):
        length = 0
        for subspace in self.space.subspaces():
            if not subspace:
                continue
            sizes = [len(v_list) if isinstance(v_list, Distribution) else 1 for v_list in subspace]
            length += numpy.prod(sizes)

        return length
//  [python]
def _compute_grad_probability_of_success(self, failure_components):
    assert isinstance(failure_components, FailureListProductComponents)
    poss = failure_components.poss
    assert failure_components.grad_poss is not None
    grad_poss = failure_components.grad_poss
    grad = numpy.zeros(grad_poss[0].shape)
    for i in range(self.num_pfs):
      mask_array = numpy.ones(self.num_pfs, dtype=bool)
      mask_array[i] = False
      grad += grad_poss[i] * numpy.product(poss[mask_array], axis=0)[:, None]
    return grad
//  [python]
def _compute_grad_probability_of_success(self, failure_components):
    assert isinstance(failure_components, FailureListProductComponents)
    poss = failure_components.poss
    assert failure_components.grad_poss is not None
    grad_poss = failure_components.grad_poss
    grad = numpy.zeros(grad_poss[0].shape)
    for i in range(self.num_pfs):
      mask_array = numpy.ones(self.num_pfs, dtype=bool)
      mask_array[i] = False
      grad += grad_poss[i] * numpy.prod(poss[mask_array], axis=0)[:, None]
    return grad
//  [python]
def __getitem__(self, i):
        # This is a reimplementation of scikit-learn ParameterGrid __getitem__
        l = len(self)
        if -l <= i < 0:
            i += l
        elif i < -l or i >= l:
            raise IndexError("list index out of range")

        for subspace in self.space.subspaces():
            # XXX: could memoize information used here
            sizes = [len(v_list) if isinstance(v_list, Distribution) else 1 for v_list in subspace]
            total = numpy.product(sizes)

            if i >= total:
                # Try the next grid
                i -= total
            else:
                out = list()
                for v_list, n in zip(subspace, sizes):
                    i, offset = divmod(i, n)
                    if isinstance(v_list, Distribution):
                        out.append(v_list[offset])
                    else:
                        out.append(v_list)

                return out
//  [python]
def __getitem__(self, i):
        # This is a reimplementation of scikit-learn ParameterGrid __getitem__
        l = len(self)
        if -l <= i < 0:
            i += l
        elif i < -l or i >= l:
            raise IndexError("list index out of range")

        for subspace in self.space.subspaces():
            # XXX: could memoize information used here
            sizes = [len(v_list) if isinstance(v_list, Distribution) else 1 for v_list in subspace]
            total = numpy.prod(sizes)

            if i >= total:
                # Try the next grid
                i -= total
            else:
                out = list()
                for v_list, n in zip(subspace, sizes):
                    i, offset = divmod(i, n)
                    if isinstance(v_list, Distribution):
                        out.append(v_list[offset])
                    else:
                        out.append(v_list)

                return out
//  [python]
def test_transpose():
    """
    Test for transpose()
    :return: Nothing
    """
    repeat_for = 5

    for repeat in range(repeat_for):
        for i in range(1, 5):
            permutation = np.random.permutation(i + 1)
            permutation = [int(p) for p in permutation]

            shape = [np.random.randint(2, 5) for _ in range(i + 1)]
            entries = np.product(shape)

            data = np.arange(entries)
            data.shape = shape

            np_transposed = np.transpose(np.copy(data), np.copy(permutation))
            by_transposeCNTK = transpose(np.ascontiguousarray(data), permutation).eval()

            assert np.alltrue(np_transposed == by_transposeCNTK)
//  [python]
def test_transpose():
    """
    Test for transpose()
    :return: Nothing
    """
    repeat_for = 5

    for repeat in range(repeat_for):
        for i in range(1, 5):
            permutation = np.random.permutation(i + 1)
            permutation = [int(p) for p in permutation]

            shape = [np.random.randint(2, 5) for _ in range(i + 1)]
            entries = np.prod(shape)

            data = np.arange(entries)
            data.shape = shape

            np_transposed = np.transpose(np.copy(data), np.copy(permutation))
            by_transposeCNTK = transpose(np.ascontiguousarray(data), permutation).eval()

            assert np.alltrue(np_transposed == by_transposeCNTK)
//  [python]
def _is_finished(self):
        masks = self.masks[self.indexes]
        masks = np.sum(masks, axis=0)
        masks[masks > 0] = 1
        area_coverage = np.sum(masks) / np.product(masks.shape)
        print(area_coverage)
        if area_coverage < self.stop_criteria:
            return False
        else:
            return True
//  [python]
def _is_finished(self):
        masks = self.masks[self.indexes]
        masks = np.sum(masks, axis=0)
        masks[masks > 0] = 1
        area_coverage = np.sum(masks) / np.prod(masks.shape)
        print(area_coverage)
        if area_coverage < self.stop_criteria:
            return False
        else:
            return True
//  [python]
def _zb_lrp(self,R,lower_upper):
        '''
        zB decomposition for input layers, from http://iphome.hhi.de/samek/pdf/MonXAI19.pdf
        '''

        N,Hout,Wout,NF = R.shape
        hf,wf,df,NF = self.W.shape
        hstride, wstride = self.stride

        lower, upper = lower_upper

        x_shape = self.X.shape
        lower_shape = lower.shape
        upper_shape = upper.shape

        # ensure shape of bounds is matching, try and signal attempted reshaping if not but conditions are promising.
        assert lower_shape == upper_shape, "array shape of lower and upper bound differ: lower.shape = {} and upper.shape = {}".format(lower_shape, upper_shape)
        assert lower_shape[0] == upper_shape[0] == 1, "batch-axis shape of lower and upper need to be 1, but have been {} and {}".format(lower_shape[0], upper_shape[0])

        if not x_shape[1::] == lower_shape[1::]:
            print('Linear._zb_lrp detects differently shaped features in inputs X and lower/upper bounds:')
            print(x_shape[1::] , lower_shape[1::], upper_shape[1::])
            if numpy.product(x_shape[1::]) == numpy.product(lower_shape[1::]):
                print('   Number of features match. Attempting reshape.')
                lower = lower.reshape([1, *x_shape[1::]])
                upper = upper.reshape([1, *x_shape[1::]])
                lower_shape = lower.shape
                upper_shape = upper.shape

        assert x_shape[1::] == lower_shape[1::] == upper_shape[1::], "Different feature shape detected between bounds and inputs! Axes 1:: differ!: X: {}, lower: {}, upper: {}".format(x_shape, lower_shape, upper_shape)

        # now for the actual computations
        Rx = np.zeros_like(self.X,dtype=np.float)
        W = self.W[na,:,:]
        Wp = np.maximum(0,W)
        Wm = np.minimum(0,W)

        for i in range(Hout):
            for j in range(Wout):
                Zx = W * self.X[:, i*hstride:i*hstride+hf , j*wstride:j*wstride+wf , : , na]
                Zl = Wp * lower[:, i*hstride:i*hstride+hf , j*wstride:j*wstride+wf , : , na]
                Zu = Wm * upper[:, i*hstride:i*hstride+hf , j*wstride:j*wstride+wf , : , na]


                Zs = (Zx - Zl - Zu).sum(axis=(1,2,3),keepdims=True)
                Zs += 1e-16*((Zs >= 0)*2 - 1.) # add a weak numerical stabilizer to cushion division by zero
                Rs = R[:,i:i+1,j:j+1,na,:]/Zs
                Rx[:,i*hstride:i*hstride+hf: , j*wstride:j*wstride+wf: , : ] += ((Zx - Zl - Zu)*Rs).sum(axis=4)
        return Rx
//  [python]
def _zb_lrp(self,R,lower_upper):
        '''
        zB decomposition for input layers, from http://iphome.hhi.de/samek/pdf/MonXAI19.pdf
        '''

        N,Hout,Wout,NF = R.shape
        hf,wf,df,NF = self.W.shape
        hstride, wstride = self.stride

        lower, upper = lower_upper

        x_shape = self.X.shape
        lower_shape = lower.shape
        upper_shape = upper.shape

        # ensure shape of bounds is matching, try and signal attempted reshaping if not but conditions are promising.
        assert lower_shape == upper_shape, "array shape of lower and upper bound differ: lower.shape = {} and upper.shape = {}".format(lower_shape, upper_shape)
        assert lower_shape[0] == upper_shape[0] == 1, "batch-axis shape of lower and upper need to be 1, but have been {} and {}".format(lower_shape[0], upper_shape[0])

        if not x_shape[1::] == lower_shape[1::]:
            print('Linear._zb_lrp detects differently shaped features in inputs X and lower/upper bounds:')
            print(x_shape[1::] , lower_shape[1::], upper_shape[1::])
            if numpy.prod(x_shape[1::]) == numpy.prod(lower_shape[1::]):
                print('   Number of features match. Attempting reshape.')
                lower = lower.reshape([1, *x_shape[1::]])
                upper = upper.reshape([1, *x_shape[1::]])
                lower_shape = lower.shape
                upper_shape = upper.shape

        assert x_shape[1::] == lower_shape[1::] == upper_shape[1::], "Different feature shape detected between bounds and inputs! Axes 1:: differ!: X: {}, lower: {}, upper: {}".format(x_shape, lower_shape, upper_shape)

        # now for the actual computations
        Rx = np.zeros_like(self.X,dtype=np.float)
        W = self.W[na,:,:]
        Wp = np.maximum(0,W)
        Wm = np.minimum(0,W)

        for i in range(Hout):
            for j in range(Wout):
                Zx = W * self.X[:, i*hstride:i*hstride+hf , j*wstride:j*wstride+wf , : , na]
                Zl = Wp * lower[:, i*hstride:i*hstride+hf , j*wstride:j*wstride+wf , : , na]
                Zu = Wm * upper[:, i*hstride:i*hstride+hf , j*wstride:j*wstride+wf , : , na]


                Zs = (Zx - Zl - Zu).sum(axis=(1,2,3),keepdims=True)
                Zs += 1e-16*((Zs >= 0)*2 - 1.) # add a weak numerical stabilizer to cushion division by zero
                Rs = R[:,i:i+1,j:j+1,na,:]/Zs
                Rx[:,i*hstride:i*hstride+hf: , j*wstride:j*wstride+wf: , : ] += ((Zx - Zl - Zu)*Rs).sum(axis=4)
        return Rx
//  [python]
def graph_lbfgs(model, loss):
    """A factory to create a function required by tfp.optimizer.lbfgs_minimize.
    Args:
        model [in]: an instance of `tf.keras.Model` or its subclasses.
        loss [in]: a function with signature loss_value = loss(pred_y, true_y).
    Returns:
        A function that has a signature of:
            loss_value, gradients = f(model_parameters).
    """

    # obtain the shapes of all trainable parameters in the model
    shapes = tf.shape_n(model.trainable_variables)
    n_tensors = len(shapes)

    # we'll use tf.dynamic_stitch and tf.dynamic_partition later, so we need to
    # prepare required information first
    count = 0
    idx = []  # stitch indices
    part = []  # partition indices
    start_time = time.time()

    for i, shape in enumerate(shapes):
        n = numpy.product(shape)
        idx.append(tf.reshape(tf.range(count, count + n, dtype=tf.int32), shape))
        part.extend([i] * n)
        count += n

    part = tf.constant(part)

    @tf.function
    def assign_new_model_parameters(params_1d):
        """A function updating the model's parameters with a 1D tf.Tensor.
        Args:
            params_1d [in]: a 1D tf.Tensor representing the model's trainable parameters.
        """

        params = tf.dynamic_partition(params_1d, part, n_tensors)
        for i, (shape, param) in enumerate(zip(shapes, params)):
            model.trainable_variables[i].assign(tf.reshape(param, shape))

    # now create a function that will be returned by this factory
    @tf.function
    def f(params_1d):
        """A function that can be used by tfp.optimizer.lbfgs_minimize.
        This function is created by function_factory.
        Args:
           params_1d [in]: a 1D tf.Tensor.
        Returns:
            A scalar loss and the gradients w.r.t. the `params_1d`.
        """
        # use GradientTape so that we can calculate the gradient of loss w.r.t. parameters
        with tf.GradientTape() as tape:
            # update the parameters in the model
            assign_new_model_parameters(params_1d)
            # calculate the loss
            loss_value = loss()

        # calculate gradients and convert to 1D tf.Tensor
        grads = tape.gradient(loss_value, model.trainable_variables)
        grads = tf.dynamic_stitch(idx, grads)

        # print out iteration & loss
        f.iter.assign_add(1)

        if f.iter % 300 == 0:
            elapsed = tf.timestamp() - f.start_time

            tf.print("Iter:", f.iter // 3, "loss:", loss_value, "time:", elapsed)
            f.start_time.assign(tf.timestamp())

        # store loss value so we can retrieve later
        tf.py_function(f.history.append, inp=[loss_value], Tout=[])

        return loss_value, grads

    # store these information as members so we can use them outside the scope
    f.iter = tf.Variable(0)
    f.idx = idx
    f.part = part
    f.shapes = shapes
    f.assign_new_model_parameters = assign_new_model_parameters
    f.history = []
    f.start_time = tf.Variable(tf.timestamp())

    return f
//  [python]
def graph_lbfgs(model, loss):
    """A factory to create a function required by tfp.optimizer.lbfgs_minimize.
    Args:
        model [in]: an instance of `tf.keras.Model` or its subclasses.
        loss [in]: a function with signature loss_value = loss(pred_y, true_y).
    Returns:
        A function that has a signature of:
            loss_value, gradients = f(model_parameters).
    """

    # obtain the shapes of all trainable parameters in the model
    shapes = tf.shape_n(model.trainable_variables)
    n_tensors = len(shapes)

    # we'll use tf.dynamic_stitch and tf.dynamic_partition later, so we need to
    # prepare required information first
    count = 0
    idx = []  # stitch indices
    part = []  # partition indices
    start_time = time.time()

    for i, shape in enumerate(shapes):
        n = numpy.prod(shape)
        idx.append(tf.reshape(tf.range(count, count + n, dtype=tf.int32), shape))
        part.extend([i] * n)
        count += n

    part = tf.constant(part)

    @tf.function
    def assign_new_model_parameters(params_1d):
        """A function updating the model's parameters with a 1D tf.Tensor.
        Args:
            params_1d [in]: a 1D tf.Tensor representing the model's trainable parameters.
        """

        params = tf.dynamic_partition(params_1d, part, n_tensors)
        for i, (shape, param) in enumerate(zip(shapes, params)):
            model.trainable_variables[i].assign(tf.reshape(param, shape))

    # now create a function that will be returned by this factory
    @tf.function
    def f(params_1d):
        """A function that can be used by tfp.optimizer.lbfgs_minimize.
        This function is created by function_factory.
        Args:
           params_1d [in]: a 1D tf.Tensor.
        Returns:
            A scalar loss and the gradients w.r.t. the `params_1d`.
        """
        # use GradientTape so that we can calculate the gradient of loss w.r.t. parameters
        with tf.GradientTape() as tape:
            # update the parameters in the model
            assign_new_model_parameters(params_1d)
            # calculate the loss
            loss_value = loss()

        # calculate gradients and convert to 1D tf.Tensor
        grads = tape.gradient(loss_value, model.trainable_variables)
        grads = tf.dynamic_stitch(idx, grads)

        # print out iteration & loss
        f.iter.assign_add(1)

        if f.iter % 300 == 0:
            elapsed = tf.timestamp() - f.start_time

            tf.print("Iter:", f.iter // 3, "loss:", loss_value, "time:", elapsed)
            f.start_time.assign(tf.timestamp())

        # store loss value so we can retrieve later
        tf.py_function(f.history.append, inp=[loss_value], Tout=[])

        return loss_value, grads

    # store these information as members so we can use them outside the scope
    f.iter = tf.Variable(0)
    f.idx = idx
    f.part = part
    f.shapes = shapes
    f.assign_new_model_parameters = assign_new_model_parameters
    f.history = []
    f.start_time = tf.Variable(tf.timestamp())

    return f
//  [python]
def get_optimum_size(self, size):
        """
        Gets the nearest optimum image size

        Taken from the casa source code (cleanhelper.py)

        Parameters
        ----------
        size : int
            Target image size in pixels

        Returns
        -------
        optimum_size : int
            Optimum image size nearest to target size

        """
        import numpy

        def prime_factors(n, douniq=True):
            """ Return the prime factors of the given number. """
            factors = []
            lastresult = n
            sqlast=int(numpy.sqrt(n))+1
            if n == 1:
                return [1]
            c=2
            while 1:
                 if (lastresult == 1) or (c > sqlast):
                     break
                 sqlast=int(numpy.sqrt(lastresult))+1
                 while 1:
                     if(c > sqlast):
                         c=lastresult
                         break
                     if lastresult % c == 0:
                         break
                     c += 1

                 factors.append(c)
                 lastresult /= c

            if (factors==[]): factors=[n]
            return  numpy.unique(factors).tolist() if douniq else factors

        n = int(size)
        if (n%2 != 0):
            n+=1
        fac=prime_factors(n, False)
        for k in range(len(fac)):
            if (fac[k] > 7):
                val=fac[k]
                while (numpy.max(prime_factors(val)) > 7):
                    val +=1
                fac[k]=val
        newlarge=numpy.product(fac)
        for k in range(n, newlarge, 2):
            if ((numpy.max(prime_factors(k)) < 8)):
                return k
        return newlarge
//  [python]
def get_optimum_size(self, size):
        """
        Gets the nearest optimum image size

        Taken from the casa source code (cleanhelper.py)

        Parameters
        ----------
        size : int
            Target image size in pixels

        Returns
        -------
        optimum_size : int
            Optimum image size nearest to target size

        """
        import numpy

        def prime_factors(n, douniq=True):
            """ Return the prime factors of the given number. """
            factors = []
            lastresult = n
            sqlast=int(numpy.sqrt(n))+1
            if n == 1:
                return [1]
            c=2
            while 1:
                 if (lastresult == 1) or (c > sqlast):
                     break
                 sqlast=int(numpy.sqrt(lastresult))+1
                 while 1:
                     if(c > sqlast):
                         c=lastresult
                         break
                     if lastresult % c == 0:
                         break
                     c += 1

                 factors.append(c)
                 lastresult /= c

            if (factors==[]): factors=[n]
            return  numpy.unique(factors).tolist() if douniq else factors

        n = int(size)
        if (n%2 != 0):
            n+=1
        fac=prime_factors(n, False)
        for k in range(len(fac)):
            if (fac[k] > 7):
                val=fac[k]
                while (numpy.max(prime_factors(val)) > 7):
                    val +=1
                fac[k]=val
        newlarge=numpy.prod(fac)
        for k in range(n, newlarge, 2):
            if ((numpy.max(prime_factors(k)) < 8)):
                return k
        return newlarge
//  [python]
def calc(self, *args):
        return numpy.product(args)
//  [python]
def calc(self, *args):
        return numpy.prod(args)
//  [python]
def get_optimum_size(self, size):
        """
        Gets the nearest optimum image size

        Taken from the casa source code (cleanhelper.py)

        Parameters
        ----------
        size : int
            Target image size in pixels

        Returns
        -------
        optimum_size : int
            Optimum image size nearest to target size

        """
        import numpy

        def prime_factors(n, douniq=True):
            """ Return the prime factors of the given number. """
            factors = []
            lastresult = n
            sqlast=int(numpy.sqrt(n))+1
            if n == 1:
                return [1]
            c=2
            while 1:
                 if (lastresult == 1) or (c > sqlast):
                     break
                 sqlast=int(numpy.sqrt(lastresult))+1
                 while 1:
                     if(c > sqlast):
                         c=lastresult
                         break
                     if lastresult % c == 0:
                         break
                     c += 1
                 factors.append(c)
                 lastresult = lastresult // c
            if (factors==[]): factors=[n]
            return  numpy.unique(factors).tolist() if douniq else factors

        n = int(size)
        if (n%2 != 0):
            n+=1
        fac=prime_factors(n, False)
        for k in range(len(fac)):
            if (fac[k] > 7):
                val=fac[k]
                while (numpy.max(prime_factors(val)) > 7):
                    val +=1
                fac[k]=val
        newlarge=numpy.product(fac)
        for k in range(n, newlarge, 2):
            if ((numpy.max(prime_factors(k)) < 8)):
                return k
        return newlarge
//  [python]
def get_optimum_size(self, size):
        """
        Gets the nearest optimum image size

        Taken from the casa source code (cleanhelper.py)

        Parameters
        ----------
        size : int
            Target image size in pixels

        Returns
        -------
        optimum_size : int
            Optimum image size nearest to target size

        """
        import numpy

        def prime_factors(n, douniq=True):
            """ Return the prime factors of the given number. """
            factors = []
            lastresult = n
            sqlast=int(numpy.sqrt(n))+1
            if n == 1:
                return [1]
            c=2
            while 1:
                 if (lastresult == 1) or (c > sqlast):
                     break
                 sqlast=int(numpy.sqrt(lastresult))+1
                 while 1:
                     if(c > sqlast):
                         c=lastresult
                         break
                     if lastresult % c == 0:
                         break
                     c += 1
                 factors.append(c)
                 lastresult = lastresult // c
            if (factors==[]): factors=[n]
            return  numpy.unique(factors).tolist() if douniq else factors

        n = int(size)
        if (n%2 != 0):
            n+=1
        fac=prime_factors(n, False)
        for k in range(len(fac)):
            if (fac[k] > 7):
                val=fac[k]
                while (numpy.max(prime_factors(val)) > 7):
                    val +=1
                fac[k]=val
        newlarge=numpy.prod(fac)
        for k in range(n, newlarge, 2):
            if ((numpy.max(prime_factors(k)) < 8)):
                return k
        return newlarge
//  [python]
def reshape(self, shape):
		if not isinstance(shape, tuple):
			raise TypeError('reshape must be tuple')
		if len(shape) != 2:
			raise TypeError('reshape must be 2-tuple')
		if shape[0] == -1 and shape[1] > 0:
			shape = (int(numpy.product(self.shape) / shape[1]), shape[1])
		if shape[1] == -1 and shape[0] > 0:
			shape = (shape[0], int(numpy.product(self.shape) / shape[0]))
		if shape[0] * shape[1] != numpy.product(self.shape):
			raise ValueError(f'incompatible reshape {shape} for current shape {self.shape}')
		for k in self._groupnode._v_children.keys():
			self._groupnode._v_children[k].shape = shape
		self.shape = shape
//  [python]
def reshape(self, shape):
		if not isinstance(shape, tuple):
			raise TypeError('reshape must be tuple')
		if len(shape) != 2:
			raise TypeError('reshape must be 2-tuple')
		if shape[0] == -1 and shape[1] > 0:
			shape = (int(numpy.prod(self.shape) / shape[1]), shape[1])
		if shape[1] == -1 and shape[0] > 0:
			shape = (shape[0], int(numpy.prod(self.shape) / shape[0]))
		if shape[0] * shape[1] != numpy.prod(self.shape):
			raise ValueError(f'incompatible reshape {shape} for current shape {self.shape}')
		for k in self._groupnode._v_children.keys():
			self._groupnode._v_children[k].shape = shape
		self.shape = shape
//  [python]
def _arc_jac_sn(w, m):
    """Inverse Jacobian elliptic sn

    Solve for z in w = sn(z, m)

    Parameters
    ----------
    w - complex scalar
        argument

    m - scalar
        modulus; in interval [0, 1]


    See [1], Eq. (56)

    References
    ----------
    .. [1] Orfanidis, "Lecture Notes on Elliptic Filter Design",
           https://www.ece.rutgers.edu/~orfanidi/ece521/notes.pdf

    """

    def _complement(kx):
        # (1-k**2) ** 0.5; the expression below
        # works for small kx
        return ((1 - kx) * (1 + kx)) ** 0.5

    k = m ** 0.5

    if k > 1:
        return np.nan
    elif k == 1:
        return np.arctanh(w)

    ks = [k]
    niter = 0
    while ks[-1] != 0:
        k_ = ks[-1]
        k_p = _complement(k_)
        ks.append((1 - k_p) / (1 + k_p))
        niter += 1
        if niter > _ARC_JAC_SN_MAXITER:
            raise ValueError('Landen transformation not converging')

    K = np.product(1 + np.array(ks[1:])) * np.pi/2

    wns = [w]

    for kn, knext in zip(ks[:-1], ks[1:]):
        wn = wns[-1]
        wnext = (2 * wn /
                 ((1 + knext) * (1 + _complement(kn * wn))))
        wns.append(wnext)

    u = 2 / np.pi * np.arcsin(wns[-1])

    z = K * u
    return z
//  [python]
def _arc_jac_sn(w, m):
    """Inverse Jacobian elliptic sn

    Solve for z in w = sn(z, m)

    Parameters
    ----------
    w - complex scalar
        argument

    m - scalar
        modulus; in interval [0, 1]


    See [1], Eq. (56)

    References
    ----------
    .. [1] Orfanidis, "Lecture Notes on Elliptic Filter Design",
           https://www.ece.rutgers.edu/~orfanidi/ece521/notes.pdf

    """

    def _complement(kx):
        # (1-k**2) ** 0.5; the expression below
        # works for small kx
        return ((1 - kx) * (1 + kx)) ** 0.5

    k = m ** 0.5

    if k > 1:
        return np.nan
    elif k == 1:
        return np.arctanh(w)

    ks = [k]
    niter = 0
    while ks[-1] != 0:
        k_ = ks[-1]
        k_p = _complement(k_)
        ks.append((1 - k_p) / (1 + k_p))
        niter += 1
        if niter > _ARC_JAC_SN_MAXITER:
            raise ValueError('Landen transformation not converging')

    K = np.prod(1 + np.array(ks[1:])) * np.pi/2

    wns = [w]

    for kn, knext in zip(ks[:-1], ks[1:]):
        wn = wns[-1]
        wnext = (2 * wn /
                 ((1 + knext) * (1 + _complement(kn * wn))))
        wns.append(wnext)

    u = 2 / np.pi * np.arcsin(wns[-1])

    z = K * u
    return z
//  [python]
def get_sounding_ids(self):
        # Look for the first dataset in the first group that is not a group itself
        if len(list(self.values())) > 0 and len(list(self.values())[0].values()) > 0 and hasattr(list(list(self.values())[0].values())[0], "shape"):
            indexes = numpy.zeros(list(list(self.values())[0].values())[0].shape, dtype=int)
            indexes.ravel()[:] = list(range(numpy.product(indexes.shape))) 
        else:
            indexes = []
        return indexes
//  [python]
def get_sounding_ids(self):
        # Look for the first dataset in the first group that is not a group itself
        if len(list(self.values())) > 0 and len(list(self.values())[0].values()) > 0 and hasattr(list(list(self.values())[0].values())[0], "shape"):
            indexes = numpy.zeros(list(list(self.values())[0].values())[0].shape, dtype=int)
            indexes.ravel()[:] = list(range(numpy.prod(indexes.shape))) 
        else:
            indexes = []
        return indexes
//  [python]
def check_all_conditional_values_satisfied(experiment_meta):
  num_conditional_values = numpy.product([len(c.values) for c in experiment_meta.conditionals])
  satisfied_parameter_configurations = set()
  for parameter in experiment_meta.all_parameters_unsorted:
    conditional_values = []
    for conditional in experiment_meta.conditionals:
      parameter_conditions = {x.name: x.values for x in parameter.conditions}
      if conditional.name in parameter_conditions:
        conditional_values.append(parameter_conditions[conditional.name])
      else:  # If that conditional is not present for a parameter, then add all values
        conditional_values.append([x.enum_index for x in conditional.values])
    for selected_conditionals in itertools.product(*conditional_values):
      satisfied_parameter_configurations.add(selected_conditionals)

  if len(satisfied_parameter_configurations) != num_conditional_values:
    raise SigoptValidationError("Need at least one parameter that satisfies each conditional value")
//  [python]
def check_all_conditional_values_satisfied(experiment_meta):
  num_conditional_values = numpy.prod([len(c.values) for c in experiment_meta.conditionals])
  satisfied_parameter_configurations = set()
  for parameter in experiment_meta.all_parameters_unsorted:
    conditional_values = []
    for conditional in experiment_meta.conditionals:
      parameter_conditions = {x.name: x.values for x in parameter.conditions}
      if conditional.name in parameter_conditions:
        conditional_values.append(parameter_conditions[conditional.name])
      else:  # If that conditional is not present for a parameter, then add all values
        conditional_values.append([x.enum_index for x in conditional.values])
    for selected_conditionals in itertools.product(*conditional_values):
      satisfied_parameter_configurations.add(selected_conditionals)

  if len(satisfied_parameter_configurations) != num_conditional_values:
    raise SigoptValidationError("Need at least one parameter that satisfies each conditional value")
//  [python]
def create_from_handles(self, handles, module_num):
        """Fill a module with the information stored in the handles structure for module # ModuleNum

        Returns a module with the settings decanted from the handles.
        If the revision is old, a different and compatible module can be returned.
        """
        self.__module_num = module_num
        idx = module_num - 1
        settings = handles[SETTINGS][0, 0]
        setting_values = []
        self.__notes = []
        if (
                MODULE_NOTES in settings.dtype.fields
                and settings[MODULE_NOTES].shape[1] > idx
        ):
            n = settings[MODULE_NOTES][0, idx].flatten()
            for x in n:
                if isinstance(x, numpy.ndarray):
                    if len(x) == 0:
                        x = ""
                    else:
                        x = x[0]
                self.__notes.append(x)
        if SHOW_WINDOW in settings.dtype.fields:
            self.__show_window = settings[SHOW_WINDOW][0, idx] != 0
        if BATCH_STATE in settings.dtype.fields:
            # convert from uint8 to array of one string to avoid long
            # arrays, which get truncated by numpy repr()
            self.batch_state = numpy.array(settings[BATCH_STATE][0, idx].tostring())
        setting_count = settings[NUMBERS_OF_VARIABLES][0, idx]
        variable_revision_number = settings[VARIABLE_REVISION_NUMBERS][0, idx]
        module_name = settings[MODULE_NAMES][0, idx][0]
        for i in range(0, setting_count):
            value_cell = settings[VARIABLE_VALUES][idx, i]
            if isinstance(value_cell, numpy.ndarray):
                if numpy.product(value_cell.shape) == 0:
                    setting_values.append("")
                else:
                    setting_values.append(str(value_cell[0]))
            else:
                setting_values.append(value_cell)
        self.set_settings_from_values(
            setting_values, variable_revision_number, module_name
        )
//  [python]
def create_from_handles(self, handles, module_num):
        """Fill a module with the information stored in the handles structure for module # ModuleNum

        Returns a module with the settings decanted from the handles.
        If the revision is old, a different and compatible module can be returned.
        """
        self.__module_num = module_num
        idx = module_num - 1
        settings = handles[SETTINGS][0, 0]
        setting_values = []
        self.__notes = []
        if (
                MODULE_NOTES in settings.dtype.fields
                and settings[MODULE_NOTES].shape[1] > idx
        ):
            n = settings[MODULE_NOTES][0, idx].flatten()
            for x in n:
                if isinstance(x, numpy.ndarray):
                    if len(x) == 0:
                        x = ""
                    else:
                        x = x[0]
                self.__notes.append(x)
        if SHOW_WINDOW in settings.dtype.fields:
            self.__show_window = settings[SHOW_WINDOW][0, idx] != 0
        if BATCH_STATE in settings.dtype.fields:
            # convert from uint8 to array of one string to avoid long
            # arrays, which get truncated by numpy repr()
            self.batch_state = numpy.array(settings[BATCH_STATE][0, idx].tostring())
        setting_count = settings[NUMBERS_OF_VARIABLES][0, idx]
        variable_revision_number = settings[VARIABLE_REVISION_NUMBERS][0, idx]
        module_name = settings[MODULE_NAMES][0, idx][0]
        for i in range(0, setting_count):
            value_cell = settings[VARIABLE_VALUES][idx, i]
            if isinstance(value_cell, numpy.ndarray):
                if numpy.prod(value_cell.shape) == 0:
                    setting_values.append("")
                else:
                    setting_values.append(str(value_cell[0]))
            else:
                setting_values.append(value_cell)
        self.set_settings_from_values(
            setting_values, variable_revision_number, module_name
        )
//  [python]
def get_optimum_size(self, size):
        """
        Gets the nearest optimum image size

        Taken from the casa source code (cleanhelper.py)

        Parameters
        ----------
        size : int
            Target image size in pixels

        Returns
        -------
        optimum_size : int
            Optimum image size nearest to target size

        """
        import numpy

        def prime_factors(n, douniq=True):
            """ Return the prime factors of the given number. """
            factors = []
            lastresult = n
            sqlast=int(numpy.sqrt(n))+1
            if n == 1:
                return([1])
            c=2
            while 1:
                 if (lastresult == 1) or (c > sqlast):
                     break
                 sqlast=int(numpy.sqrt(lastresult))+1
                 while 1:
                     if(c > sqlast):
                         c=lastresult
                         break
                     if lastresult % c == 0:
                         break
                     c += 1
                 factors.append(c)
                 lastresult /= c
            if (factors==[]): factors=[n]
            return(numpy.unique(factors).tolist() if douniq else factors)

        n = int(size)
        if (n%2 != 0):
            n+=1
        fac=prime_factors(n, False)
        for k in range(len(fac)):
            if (fac[k] > 7):
                val=fac[k]
                while (numpy.max(prime_factors(val)) > 7):
                    val +=1
                fac[k]=val
        newlarge=numpy.product(fac)
        for k in range(n, newlarge, 2):
            if ((numpy.max(prime_factors(k)) < 8)):
                return(k)
        return(newlarge)
//  [python]
def get_optimum_size(self, size):
        """
        Gets the nearest optimum image size

        Taken from the casa source code (cleanhelper.py)

        Parameters
        ----------
        size : int
            Target image size in pixels

        Returns
        -------
        optimum_size : int
            Optimum image size nearest to target size

        """
        import numpy

        def prime_factors(n, douniq=True):
            """ Return the prime factors of the given number. """
            factors = []
            lastresult = n
            sqlast=int(numpy.sqrt(n))+1
            if n == 1:
                return([1])
            c=2
            while 1:
                 if (lastresult == 1) or (c > sqlast):
                     break
                 sqlast=int(numpy.sqrt(lastresult))+1
                 while 1:
                     if(c > sqlast):
                         c=lastresult
                         break
                     if lastresult % c == 0:
                         break
                     c += 1
                 factors.append(c)
                 lastresult /= c
            if (factors==[]): factors=[n]
            return(numpy.unique(factors).tolist() if douniq else factors)

        n = int(size)
        if (n%2 != 0):
            n+=1
        fac=prime_factors(n, False)
        for k in range(len(fac)):
            if (fac[k] > 7):
                val=fac[k]
                while (numpy.max(prime_factors(val)) > 7):
                    val +=1
                fac[k]=val
        newlarge=numpy.prod(fac)
        for k in range(n, newlarge, 2):
            if ((numpy.max(prime_factors(k)) < 8)):
                return(k)
        return(newlarge)
//  [python]
def dominant_colors(self, hashable_image: HashableImage):
        """
        Input: PIL image
        Output: A list of 10 colors in the image from most dominant to least dominant
        Adaptation of https://stackoverflow.com/a/3244061/7274182
        """
        ar = numpy.asarray(hashable_image.image.resize((150, 150), 0))
        shape = ar.shape
        ar = ar.reshape(numpy.product(shape[:2]), shape[2]).astype(
            float
        )  # flatten to shape (width*height, 3)

        kmeans = sklearn.cluster.MiniBatchKMeans(
            n_clusters=10, init="k-means++", max_iter=20, random_state=1000
        ).fit(ar)
        codes = kmeans.cluster_centers_

        vecs, _dist = scipy.cluster.vq.vq(ar, codes)  # assign codes
        counts, _bins = numpy.histogram(vecs, len(codes))  # count occurrences

        Color = collections.namedtuple("Color", ["r", "g", "b"])
        colors = []
        for index in numpy.argsort(counts)[::-1]:
            color_tuple = tuple([int(code) for code in codes[index]])
            colors.append(Color(*color_tuple))
        return colors
//  [python]
def dominant_colors(self, hashable_image: HashableImage):
        """
        Input: PIL image
        Output: A list of 10 colors in the image from most dominant to least dominant
        Adaptation of https://stackoverflow.com/a/3244061/7274182
        """
        ar = numpy.asarray(hashable_image.image.resize((150, 150), 0))
        shape = ar.shape
        ar = ar.reshape(numpy.prod(shape[:2]), shape[2]).astype(
            float
        )  # flatten to shape (width*height, 3)

        kmeans = sklearn.cluster.MiniBatchKMeans(
            n_clusters=10, init="k-means++", max_iter=20, random_state=1000
        ).fit(ar)
        codes = kmeans.cluster_centers_

        vecs, _dist = scipy.cluster.vq.vq(ar, codes)  # assign codes
        counts, _bins = numpy.histogram(vecs, len(codes))  # count occurrences

        Color = collections.namedtuple("Color", ["r", "g", "b"])
        colors = []
        for index in numpy.argsort(counts)[::-1]:
            color_tuple = tuple([int(code) for code in codes[index]])
            colors.append(Color(*color_tuple))
        return colors
//  [python]
def conv(self, block):
        '''在读取darknet的yolov3.weights文件时，顺序是
          1 - bias；
          2 - 如果有bn，则接着读取三个scale，mean，var
          3 - 读取权重
        '''
        # Darknet serializes convolutional weights as:
        # [bias/beta, [gamma, mean, variance], conv_weights]
        self.count[0] += 1
        # read conv block
        filters = int(block['filters'])
        size = int(block['size'])
        stride = int(block['stride'])
        pad = int(block['pad'])
        activation = block['activation']
        
        padding = 'same' if pad == 1 and stride == 1 else 'valid'
        batch_normalize = 'batch_normalize' in block
        
        prev_layer_shape = K.int_shape(self.prev_layer)
        weights_shape = (size, size, prev_layer_shape[-1], filters)
        darknet_w_shape = (filters, weights_shape[2], size, size)
        weights_size = np.product(weights_shape)

        print('+',self.count[0],'conv2d', 
              'bn' if batch_normalize else ' ',
              activation,
              weights_shape)

        
 
        # 如果有bn，则接着读取滤波器个scale，mean，var

        # 读取权重
        
        # DarkNet conv_weights are serialized Caffe-style:
        # (out_dim, in_dim, height, width)
        # We would like to set these to Tensorflow order:
        # (height, width, in_dim, out_dim)


        act_fn = None
        if activation == 'leaky':
            pass
        elif activation != 'linear':
            raise

        if stride > 1:
            self.prev_layer = ZeroPadding2D(((1,0),(1,0)))(self.prev_layer)

        conv_layer = (Conv2D(
                filters, (size, size),
                strides=(stride, stride),
                kernel_regularizer=l2(self.weight_decay),
                use_bias=not batch_normalize,
                             # weights=conv_weights,
                activation=act_fn,
                padding=padding))(self.prev_layer)

        if batch_normalize:
            #conv_layer = BatchNormalization(weights=bn_weight_list)(conv_layer)
             conv_layer = BatchNormalization()(conv_layer)
        self.prev_layer = conv_layer

        if activation == 'linear':
            self.all_layers.append(self.prev_layer)
        elif activation == 'leaky':
            act_layer = LeakyReLU(alpha=0.1)(self.prev_layer)
            self.prev_layer = act_layer
            self.all_layers.append(act_layer)
//  [python]
def conv(self, block):
        '''在读取darknet的yolov3.weights文件时，顺序是
          1 - bias；
          2 - 如果有bn，则接着读取三个scale，mean，var
          3 - 读取权重
        '''
        # Darknet serializes convolutional weights as:
        # [bias/beta, [gamma, mean, variance], conv_weights]
        self.count[0] += 1
        # read conv block
        filters = int(block['filters'])
        size = int(block['size'])
        stride = int(block['stride'])
        pad = int(block['pad'])
        activation = block['activation']
        
        padding = 'same' if pad == 1 and stride == 1 else 'valid'
        batch_normalize = 'batch_normalize' in block
        
        prev_layer_shape = K.int_shape(self.prev_layer)
        weights_shape = (size, size, prev_layer_shape[-1], filters)
        darknet_w_shape = (filters, weights_shape[2], size, size)
        weights_size = np.prod(weights_shape)

        print('+',self.count[0],'conv2d', 
              'bn' if batch_normalize else ' ',
              activation,
              weights_shape)

        
 
        # 如果有bn，则接着读取滤波器个scale，mean，var

        # 读取权重
        
        # DarkNet conv_weights are serialized Caffe-style:
        # (out_dim, in_dim, height, width)
        # We would like to set these to Tensorflow order:
        # (height, width, in_dim, out_dim)


        act_fn = None
        if activation == 'leaky':
            pass
        elif activation != 'linear':
            raise

        if stride > 1:
            self.prev_layer = ZeroPadding2D(((1,0),(1,0)))(self.prev_layer)

        conv_layer = (Conv2D(
                filters, (size, size),
                strides=(stride, stride),
                kernel_regularizer=l2(self.weight_decay),
                use_bias=not batch_normalize,
                             # weights=conv_weights,
                activation=act_fn,
                padding=padding))(self.prev_layer)

        if batch_normalize:
            #conv_layer = BatchNormalization(weights=bn_weight_list)(conv_layer)
             conv_layer = BatchNormalization()(conv_layer)
        self.prev_layer = conv_layer

        if activation == 'linear':
            self.all_layers.append(self.prev_layer)
        elif activation == 'leaky':
            act_layer = LeakyReLU(alpha=0.1)(self.prev_layer)
            self.prev_layer = act_layer
            self.all_layers.append(act_layer)
//  [python]
def test_keywords5_ticket_2100(self):
        # Test vectorizing function with no kwargs args.
        @vectorize
        def f(*v):
            return n.prod(v)

        assert_array_equal(f([1, 2], [3, 4]), [3, 8])
//  [python]
def test_keywords5_ticket_2100(self):
        # Test vectorizing function with no kwargs args.
        @vectorize
        def f(*v):
            return np.prod(v)

        assert_array_equal(f([1, 2], [3, 4]), [3, 8])
//  [python]
def perform(self, node, inp, out_):
        x, = inp
        out, = out_
        outdim = self.outdim
        if outdim == 1:
            try:
                out[0] = x.reshape(x.size)
            except AttributeError:
                out[0] = x.reshape((n.prod(x.shape),))
        elif outdim == len(x.shape):
            out[0] = x
        else:
            newshape = (x.shape[:outdim - 1] +
                        (n.prod(x.shape[outdim - 1:]),))
            out[0] = x.reshape(newshape)