jiahuimbzuai/CoQuIR huggingface dataset:
//  [python]
def _get_nan_decs(self):
        """Set all variables to nan for specializations of fused types for
        which don't have signatures.

        """
        # Set non fused-type variables to nan
        tab = " "*4
        fused_types, lines = [], [tab + "else:"]
        seen = set()
        for outvar, outtype, code in zip(self.outvars, self.outtypes, self.outcodes):
            if len(code) == 1:
                line = "{}[0] = {}".format(outvar, NAN_VALUE[code])
                lines.append(2*tab + line)
            else:
                fused_type = outtype
                name, _ = fused_type
                if name not in seen:
                    fused_types.append(fused_type)
                    seen.add(name)
        if not fused_types:
            return lines

        # Set fused-type variables to nan
        all_codes = []
        for fused_type in fused_types:
            _, codes = fused_type
            all_codes.append(codes)
        all_codes = tuple(all_codes)

        codelens = list(map(lambda x: len(x), all_codes))
        last = numpy.prod(codelens) - 1
        for m, codes in enumerate(itertools.product(*all_codes)):
            fused_codes, decs = [], []
            for n, fused_type in enumerate(fused_types):
                code = codes[n]
                fused_codes.append(underscore(CY_TYPES[code]))
                for nn, outvar in enumerate(self.outvars):
                    if self.outtypes[nn] == fused_type:
                        line = "{}[0] = {}".format(outvar, NAN_VALUE[code])
                        decs.append(line)
            if m == 0:
                adverb = "if"
            elif m == last:
                adverb = "else"
            else:
                adverb = "elif"
            cond = self._get_conditional(fused_types, codes, adverb)
            lines.append(2*tab + cond)
            lines.extend(map(lambda x: 3*tab + x, decs))
        return lines
//  [python]
def required_driver_memory(N):
    NN = N * N
    fine_shape = (int(np.sqrt(NN)),) * 2
    coarse_shape = fine_shape[0] // 2, fine_shape[1] // 2
    coarse_dim = numpy.product(coarse_shape)
    nnz = 9 * coarse_dim
    elements = nnz + coarse_dim + 1
    bytes = elements * 8
    mb = bytes / 10**6
    print("Max required driver memory for N=%d is %fMB" % (N, mb))
//  [python]
def required_driver_memory(N):
    NN = N * N
    fine_shape = (int(np.sqrt(NN)),) * 2
    coarse_shape = fine_shape[0] // 2, fine_shape[1] // 2
    coarse_dim = numpy.prod(coarse_shape)
    nnz = 9 * coarse_dim
    elements = nnz + coarse_dim + 1
    bytes = elements * 8
    mb = bytes / 10**6
    print("Max required driver memory for N=%d is %fMB" % (N, mb))
//  [python]
def _find_nans(self):
        """
        Find NaN elements, return their row indices and their number.
        """
        isfinite = numpy.isfinite(self.features)
        collapsed_rows = numpy.product(isfinite, axis=1, dtype=bool)
        num_nans = self.n_samples - numpy.sum(collapsed_rows)
        return collapsed_rows, num_nans
//  [python]
def _find_nans(self):
        """
        Find NaN elements, return their row indices and their number.
        """
        isfinite = numpy.isfinite(self.features)
        collapsed_rows = numpy.prod(isfinite, axis=1, dtype=bool)
        num_nans = self.n_samples - numpy.sum(collapsed_rows)
        return collapsed_rows, num_nans
//  [python]
def parameter_stats(parameters, algorithm):
    vars_ = []
    for name, param in parameters.items():
        num_elements = numpy.product(param.get_value().shape)
        norm = param.norm(2) / num_elements ** 0.5
        grad_norm = algorithm.gradients[param].norm(2) / num_elements ** 0.5
        step_norm = algorithm.steps[param].norm(2) / num_elements ** 0.5
        stats = tensor.stack(norm, grad_norm, step_norm, step_norm / grad_norm)
        stats.name = name + '_stats'
        vars_.append(stats)
    return vars_
//  [python]
def parameter_stats(parameters, algorithm):
    vars_ = []
    for name, param in parameters.items():
        num_elements = numpy.prod(param.get_value().shape)
        norm = param.norm(2) / num_elements ** 0.5
        grad_norm = algorithm.gradients[param].norm(2) / num_elements ** 0.5
        step_norm = algorithm.steps[param].norm(2) / num_elements ** 0.5
        stats = tensor.stack(norm, grad_norm, step_norm, step_norm / grad_norm)
        stats.name = name + '_stats'
        vars_.append(stats)
    return vars_
//  [python]
def _binary_erosion(input, structure, iterations, mask, output,
                    border_value, origin, invert, brute_force):
    input = numpy.asarray(input)
    if numpy.iscomplexobj(input):
        raise TypeError('Complex type not supported')
    if structure is None:
        structure = generate_binary_structure(input.ndim, 1)
    else:
        structure = numpy.asarray(structure, dtype=bool)
    if structure.ndim != input.ndim:
        raise RuntimeError('structure and input must have same dimensionality')
    if not structure.flags.contiguous:
        structure = structure.copy()
    if numpy.product(structure.shape, axis=0) < 1:
        raise RuntimeError('structure must not be empty')
    if mask is not None:
        mask = numpy.asarray(mask)
        if mask.shape != input.shape:
            raise RuntimeError('mask and input must have equal sizes')
    origin = _ni_support._normalize_sequence(origin, input.ndim)
    cit = _center_is_true(structure, origin)
    if isinstance(output, numpy.ndarray):
        if numpy.iscomplexobj(output):
            raise TypeError('Complex output type not supported')
    else:
        output = bool
    output = _ni_support._get_output(output, input)

    if iterations == 1:
        _nd_image.binary_erosion(input, structure, mask, output,
                                 border_value, origin, invert, cit, 0)
        return output
    elif cit and not brute_force:
        changed, coordinate_list = _nd_image.binary_erosion(
            input, structure, mask, output,
            border_value, origin, invert, cit, 1)
        structure = structure[tuple([slice(None, None, -1)] *
                                    structure.ndim)]
        for ii in range(len(origin)):
            origin[ii] = -origin[ii]
            if not structure.shape[ii] & 1:
                origin[ii] -= 1
        if mask is not None:
            mask = numpy.asarray(mask, dtype=numpy.int8)
        if not structure.flags.contiguous:
            structure = structure.copy()
        _nd_image.binary_erosion2(output, structure, mask, iterations - 1,
                                  origin, invert, coordinate_list)
        return output
    else:
        tmp_in = numpy.empty_like(input, dtype=bool)
        tmp_out = output
        if iterations >= 1 and not iterations & 1:
            tmp_in, tmp_out = tmp_out, tmp_in
        changed = _nd_image.binary_erosion(
            input, structure, mask, tmp_out,
            border_value, origin, invert, cit, 0)
        ii = 1
        while ii < iterations or (iterations < 1 and changed):
            tmp_in, tmp_out = tmp_out, tmp_in
            changed = _nd_image.binary_erosion(
                tmp_in, structure, mask, tmp_out,
                border_value, origin, invert, cit, 0)
            ii += 1
        return output
//  [python]
def _binary_erosion(input, structure, iterations, mask, output,
                    border_value, origin, invert, brute_force):
    input = numpy.asarray(input)
    if numpy.iscomplexobj(input):
        raise TypeError('Complex type not supported')
    if structure is None:
        structure = generate_binary_structure(input.ndim, 1)
    else:
        structure = numpy.asarray(structure, dtype=bool)
    if structure.ndim != input.ndim:
        raise RuntimeError('structure and input must have same dimensionality')
    if not structure.flags.contiguous:
        structure = structure.copy()
    if numpy.prod(structure.shape, axis=0) < 1:
        raise RuntimeError('structure must not be empty')
    if mask is not None:
        mask = numpy.asarray(mask)
        if mask.shape != input.shape:
            raise RuntimeError('mask and input must have equal sizes')
    origin = _ni_support._normalize_sequence(origin, input.ndim)
    cit = _center_is_true(structure, origin)
    if isinstance(output, numpy.ndarray):
        if numpy.iscomplexobj(output):
            raise TypeError('Complex output type not supported')
    else:
        output = bool
    output = _ni_support._get_output(output, input)

    if iterations == 1:
        _nd_image.binary_erosion(input, structure, mask, output,
                                 border_value, origin, invert, cit, 0)
        return output
    elif cit and not brute_force:
        changed, coordinate_list = _nd_image.binary_erosion(
            input, structure, mask, output,
            border_value, origin, invert, cit, 1)
        structure = structure[tuple([slice(None, None, -1)] *
                                    structure.ndim)]
        for ii in range(len(origin)):
            origin[ii] = -origin[ii]
            if not structure.shape[ii] & 1:
                origin[ii] -= 1
        if mask is not None:
            mask = numpy.asarray(mask, dtype=numpy.int8)
        if not structure.flags.contiguous:
            structure = structure.copy()
        _nd_image.binary_erosion2(output, structure, mask, iterations - 1,
                                  origin, invert, coordinate_list)
        return output
    else:
        tmp_in = numpy.empty_like(input, dtype=bool)
        tmp_out = output
        if iterations >= 1 and not iterations & 1:
            tmp_in, tmp_out = tmp_out, tmp_in
        changed = _nd_image.binary_erosion(
            input, structure, mask, tmp_out,
            border_value, origin, invert, cit, 0)
        ii = 1
        while ii < iterations or (iterations < 1 and changed):
            tmp_in, tmp_out = tmp_out, tmp_in
            changed = _nd_image.binary_erosion(
                tmp_in, structure, mask, tmp_out,
                border_value, origin, invert, cit, 0)
            ii += 1
        return output
//  [python]
def product(x, axis=0):
    return np.product(x, axis)
//  [python]
def product(x, axis=0):
    return np.prod(x, axis)
//  [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_surface(self, dest_surf = None):
        camera = self.camera

        im = highgui.cvQueryFrame(camera)
        #convert Ipl image to PIL image
        #print type(im)
        if im:
            xx = opencv.adaptors.Ipl2NumPy(im)
            #print type(xx)
            #print xx.iscontiguous()
            #print dir(xx)
            #print xx.shape
            xxx = numpy.reshape(xx, (numpy.product(xx.shape),))

            if xx.shape[2] != 3:
                raise ValueError("not sure what to do about this size")

            pg_img = pygame.image.frombuffer(xxx, (xx.shape[1],xx.shape[0]), "RGB")

            # if there is a destination surface given, we blit onto that.
            if dest_surf:
                dest_surf.blit(pg_img, (0,0))
            return dest_surf
//  [python]
def get_surface(self, dest_surf = None):
        camera = self.camera

        im = highgui.cvQueryFrame(camera)
        #convert Ipl image to PIL image
        #print type(im)
        if im:
            xx = opencv.adaptors.Ipl2NumPy(im)
            #print type(xx)
            #print xx.iscontiguous()
            #print dir(xx)
            #print xx.shape
            xxx = numpy.reshape(xx, (numpy.prod(xx.shape),))

            if xx.shape[2] != 3:
                raise ValueError("not sure what to do about this size")

            pg_img = pygame.image.frombuffer(xxx, (xx.shape[1],xx.shape[0]), "RGB")

            # if there is a destination surface given, we blit onto that.
            if dest_surf:
                dest_surf.blit(pg_img, (0,0))
            return dest_surf
//  [python]
def grid_init(is_vector=False, force=False):
  '''Sets up the regular x-, y-, z-grid 
  specified by the global lists: 

    :min\_: List of minimum grid values
    :max\_: List of maximum grid values
    :N\_: List of number of grid points

  **Parameters:**
  
    is_vector : bool, optional
      If True, converts the regular grid to a vector grid.

  '''
  
  # All grid related variables should be globals 
  global x, y, z, d3r, min_, max_, N_, delta_, grid, is_initialized, is_regular
  
  if is_initialized and not force:
    return 0
  
  # Initialize a list for the grid 
  grid = [[],[],[]]
    
  # Loop over the three dimensions 
  for ii in range(3):
    if max_[ii] == min_[ii]:
      # If min-value is equal to max-value, write only min-value to grid  
      grid[ii]   = numpy.array([min_[ii]],dtype=numpy.float64)
      delta_[ii] = 1
      N_[ii]     = 1
    else:
      # Calculate the grid using the input parameters 
      if delta_[ii]:
        grid[ii] = numpy.arange(min_[ii],max_[ii]+delta_[ii],delta_[ii],dtype=numpy.float64)
        N_[ii] = len(grid[ii])
      else:
        grid[ii] = numpy.array(numpy.linspace(min_[ii],max_[ii],N_[ii]),dtype=numpy.float64) # backward compatibility
        delta_[ii] = grid[ii][1]-grid[ii][0]
  
  # Write grid 
  x = grid[0]  
  y = grid[1]  
  z = grid[2]
  d3r = numpy.product(delta_)
  
  is_initialized = True
  is_regular = True
  
  if is_vector:
    grid2vector()
  else:
    setattr(sys.modules[__name__],'is_vector',False)
//  [python]
def grid_init(is_vector=False, force=False):
  '''Sets up the regular x-, y-, z-grid 
  specified by the global lists: 

    :min\_: List of minimum grid values
    :max\_: List of maximum grid values
    :N\_: List of number of grid points

  **Parameters:**
  
    is_vector : bool, optional
      If True, converts the regular grid to a vector grid.

  '''
  
  # All grid related variables should be globals 
  global x, y, z, d3r, min_, max_, N_, delta_, grid, is_initialized, is_regular
  
  if is_initialized and not force:
    return 0
  
  # Initialize a list for the grid 
  grid = [[],[],[]]
    
  # Loop over the three dimensions 
  for ii in range(3):
    if max_[ii] == min_[ii]:
      # If min-value is equal to max-value, write only min-value to grid  
      grid[ii]   = numpy.array([min_[ii]],dtype=numpy.float64)
      delta_[ii] = 1
      N_[ii]     = 1
    else:
      # Calculate the grid using the input parameters 
      if delta_[ii]:
        grid[ii] = numpy.arange(min_[ii],max_[ii]+delta_[ii],delta_[ii],dtype=numpy.float64)
        N_[ii] = len(grid[ii])
      else:
        grid[ii] = numpy.array(numpy.linspace(min_[ii],max_[ii],N_[ii]),dtype=numpy.float64) # backward compatibility
        delta_[ii] = grid[ii][1]-grid[ii][0]
  
  # Write grid 
  x = grid[0]  
  y = grid[1]  
  z = grid[2]
  d3r = numpy.prod(delta_)
  
  is_initialized = True
  is_regular = True
  
  if is_vector:
    grid2vector()
  else:
    setattr(sys.modules[__name__],'is_vector',False)
//  [python]
def mv2g(**kwargs):
  '''Converts all `numpy.ndarrays` given as the keyword arguments 
  (`**kwargs`) from a vector grid of `shape=(..., Nx*Ny*Nz, ...,)` to a regular 
  grid of `shape=(..., Nx, Ny, Nz, ...,)`, and, if more than one `**kwargs` is 
  given, returns it as a dictionary.
  
  Hint: The global values for the grid dimensionality, i.e., :mod:`grid.N_`,
  are used for reshaping.
  '''
  import itertools
  return_val = {}
  for i,j in kwargs.items():
    j = numpy.asarray(j,dtype=float)
    shape = numpy.shape(j)
    where = numpy.argwhere(shape==numpy.product(N_))[0,0]
    return_val[i] = numpy.zeros(shape[:where]+tuple(N_)+shape[where+1:])
    for key in itertools.product(*[range(k) for k in (shape[:where] + shape[where+1:])]):
      obj = [slice(k,k+1) for k in key]
      for r in range(3):
        obj.insert(where,slice(None,None))
      return_val[i][obj] = matrix_vector2grid(j[obj[:where]+obj[where+2:]].reshape((-1,)), 
                                          **dict(zip(['Nx','Ny','Nz'],N_)))

  return list(return_val.values())[0] if len(return_val.values()) == 1 else return_val
//  [python]
def mv2g(**kwargs):
  '''Converts all `numpy.ndarrays` given as the keyword arguments 
  (`**kwargs`) from a vector grid of `shape=(..., Nx*Ny*Nz, ...,)` to a regular 
  grid of `shape=(..., Nx, Ny, Nz, ...,)`, and, if more than one `**kwargs` is 
  given, returns it as a dictionary.
  
  Hint: The global values for the grid dimensionality, i.e., :mod:`grid.N_`,
  are used for reshaping.
  '''
  import itertools
  return_val = {}
  for i,j in kwargs.items():
    j = numpy.asarray(j,dtype=float)
    shape = numpy.shape(j)
    where = numpy.argwhere(shape==numpy.prod(N_))[0,0]
    return_val[i] = numpy.zeros(shape[:where]+tuple(N_)+shape[where+1:])
    for key in itertools.product(*[range(k) for k in (shape[:where] + shape[where+1:])]):
      obj = [slice(k,k+1) for k in key]
      for r in range(3):
        obj.insert(where,slice(None,None))
      return_val[i][obj] = matrix_vector2grid(j[obj[:where]+obj[where+2:]].reshape((-1,)), 
                                          **dict(zip(['Nx','Ny','Nz'],N_)))

  return list(return_val.values())[0] if len(return_val.values()) == 1 else return_val
//  [python]
def fromstring(datastring, type=None, shape=None, typecode=None, dtype=None):
    dtype = type2dtype(typecode, type, dtype, True)
    if shape is None:
        count = -1
    else:
        count = np.product(shape)
    res = np.fromstring(datastring, dtype=dtype, count=count)
    if shape is not None:
        res.shape = shape
    return res
//  [python]
def fromstring(datastring, type=None, shape=None, typecode=None, dtype=None):
    dtype = type2dtype(typecode, type, dtype, True)
    if shape is None:
        count = -1
    else:
        count = np.prod(shape)
    res = np.fromstring(datastring, dtype=dtype, count=count)
    if shape is not None:
        res.shape = shape
    return res
//  [python]
def set_grid(xnew,ynew,znew,is_vector):
  '''Sets the x-, y-, z-grid.
  '''
  global x, y, z, is_initialized, is_regular
  coord = ['x', 'y', 'z']  
  NumberTypes = (int, float) #: Contains the supported types.
  
  reset_grid() # Reset the current grid parameters

  length = []
  grid = [xnew,ynew,znew]
  for i,c in enumerate(grid):
    # Check the type of the grid
    if isinstance(c,NumberTypes):
      c = numpy.array([c],dtype=numpy.float64)      
    elif isinstance(c,(list,tuple)): 
      c = numpy.array(c,dtype=numpy.float64)    
    elif not isinstance(c,numpy.ndarray):
      raise TypeError('%s (dimension %d) is of inappropriate type. (%s)' %(coord[i],i,type(c)))
    # Reshape if necessary
    if c.ndim != 1:
      c = c.reshape((-1,))
    # Save new grid
    grid[i] = c
    length.append(len(c))
  
  # Produce some information about the grid.
  info_string = 'Grid has been set up...'
  info_string += ('\n\tIf the input coordinates will be used for a regular grid,' +
                  '\n\tit will contain %dx%dx%d=%d data points.' % 
                  (tuple(length) + (numpy.product(length),)) 
                  )
  if length[0] == length[1] == length[2]:
    info_string += ('\n\n\tIf the input coordinates will be used for a vector grid,' +
                    '\n\tit will contain %d data points.' % length[0]
                    )
  else:
    info_string += ('\n\n\tAttention: Due to their different length, the grid variables' +
                    '\n\tcannot be used for a computation using a vector grid!'
                    )
  
  # Write grid 
  x = grid[0]  
  y = grid[1]  
  z = grid[2]
  
  is_initialized = True
  if isinstance(is_vector,bool):
    setattr(sys.modules[__name__],'is_vector',is_vector)
    info_string += ('\n\nThe variable `grid.is_vector` has been set to %s.' % 
                    is_vector)

  is_regular = is_vector==False
  
  set_boundaries(is_regular)
  return info_string
//  [python]
def set_grid(xnew,ynew,znew,is_vector):
  '''Sets the x-, y-, z-grid.
  '''
  global x, y, z, is_initialized, is_regular
  coord = ['x', 'y', 'z']  
  NumberTypes = (int, float) #: Contains the supported types.
  
  reset_grid() # Reset the current grid parameters

  length = []
  grid = [xnew,ynew,znew]
  for i,c in enumerate(grid):
    # Check the type of the grid
    if isinstance(c,NumberTypes):
      c = numpy.array([c],dtype=numpy.float64)      
    elif isinstance(c,(list,tuple)): 
      c = numpy.array(c,dtype=numpy.float64)    
    elif not isinstance(c,numpy.ndarray):
      raise TypeError('%s (dimension %d) is of inappropriate type. (%s)' %(coord[i],i,type(c)))
    # Reshape if necessary
    if c.ndim != 1:
      c = c.reshape((-1,))
    # Save new grid
    grid[i] = c
    length.append(len(c))
  
  # Produce some information about the grid.
  info_string = 'Grid has been set up...'
  info_string += ('\n\tIf the input coordinates will be used for a regular grid,' +
                  '\n\tit will contain %dx%dx%d=%d data points.' % 
                  (tuple(length) + (numpy.prod(length),)) 
                  )
  if length[0] == length[1] == length[2]:
    info_string += ('\n\n\tIf the input coordinates will be used for a vector grid,' +
                    '\n\tit will contain %d data points.' % length[0]
                    )
  else:
    info_string += ('\n\n\tAttention: Due to their different length, the grid variables' +
                    '\n\tcannot be used for a computation using a vector grid!'
                    )
  
  # Write grid 
  x = grid[0]  
  y = grid[1]  
  z = grid[2]
  
  is_initialized = True
  if isinstance(is_vector,bool):
    setattr(sys.modules[__name__],'is_vector',is_vector)
    info_string += ('\n\nThe variable `grid.is_vector` has been set to %s.' % 
                    is_vector)

  is_regular = is_vector==False
  
  set_boundaries(is_regular)
  return info_string
//  [python]
def _reshape(self, X, y=None):
        if y is not None and y.ndim == 1:
            y = y.reshape((y.shape[0], 1))
        if self.is_convolution() and X.ndim == 3:
            X = X.reshape((X.shape[0], X.shape[1], X.shape[2], 1))
        if self.is_convolution() and X.ndim == 2:
            size = math.sqrt(X.shape[1])
            assert size.is_integer(),\
                "Input array is not in image shape, and could not assume a square."
            X = X.reshape((X.shape[0], int(size), int(size), 1))
        if not self.is_convolution() and X.ndim > 2:
            X = X.reshape((X.shape[0], numpy.product(X.shape[1:])))
        return X, y
//  [python]
def _reshape(self, X, y=None):
        if y is not None and y.ndim == 1:
            y = y.reshape((y.shape[0], 1))
        if self.is_convolution() and X.ndim == 3:
            X = X.reshape((X.shape[0], X.shape[1], X.shape[2], 1))
        if self.is_convolution() and X.ndim == 2:
            size = math.sqrt(X.shape[1])
            assert size.is_integer(),\
                "Input array is not in image shape, and could not assume a square."
            X = X.reshape((X.shape[0], int(size), int(size), 1))
        if not self.is_convolution() and X.ndim > 2:
            X = X.reshape((X.shape[0], numpy.prod(X.shape[1:])))
        return X, y
//  [python]
def align_zlp(interactive, api):
    # find the focused data item
    window = api.application.document_windows[0]
    src_data_item = window.target_data_item
    if src_data_item:

        src_xdata = src_data_item.xdata
        # check to make sure it is suitable for this algorithm
        if src_xdata.is_datum_1d and (src_xdata.is_sequence or src_xdata.is_collection):

            # get the numpy array and create the destination data
            src_data = src_data_item.data
            dst_data = numpy.zeros(src_data.shape)

            # set up the indexing. to make this algorithm work with any indexing,
            # we will iterate over all non-datum dimensions using numpy.unravel_index.
            d_rank = src_xdata.datum_dimension_count
            src_shape = tuple(src_xdata.data_shape)
            s_shape = src_shape[0:-d_rank]
            count = int(numpy.product(s_shape))

            # use this as the reference position. all other spectra will be aligned to this one.
            ref_pos = numpy.argmax(src_data[0, 0])

            # loop over all non-datum dimensions linearly
            for i in range(count):
                # generate the index for the non-datum dimensions using unravel_index
                ii = numpy.unravel_index(i, s_shape)

                # the algorithm in this early version is to find the max value
                mx_pos = numpy.argmax(src_data[ii])

                # determine the offset (an integer) and store the shifted data into the result
                offset = mx_pos - ref_pos
                if offset < 0:
                    dst_data[ii][-offset:] = src_data[ii][0:offset]
                elif offset > 0:
                    dst_data[ii][:-offset] = src_data[ii][offset:]
                else:
                    dst_data[ii][:] = src_data[ii][:]

                # if the last index is 0, report progress
                if ii[-1] == 0:
                    print(f"At index {ii}")

                # check to see if the user canceled
                if interactive.cancelled:
                    break

            dimensional_calibrations = src_xdata.dimensional_calibrations
            energy_calibration = dimensional_calibrations[-1]
            energy_calibration.offset = -(ref_pos + 0.5) * energy_calibration.scale
            dimensional_calibrations = dimensional_calibrations[0:-1] + [energy_calibration]

            # dst_data is complete. construct xdata with correct calibration and data descriptor.
            dst_xdata = DataAndMetadata.new_data_and_metadata(dst_data, src_xdata.intensity_calibration, dimensional_calibrations, data_descriptor=DataAndMetadata.DataDescriptor(False, 2, 1))

            # create a new data item in the library and set its title.
            data_item = api.library.create_data_item_from_data_and_metadata(dst_xdata)
            data_item.title = "Aligned " + src_data_item.title

            # display the data item.
            window.display_data_item(data_item)
        else:
            print("Failed: Data is not a sequence or collection of 1D spectra.")
    else:
        print("Failed: No data item selected.")
//  [python]
def align_zlp(interactive, api):
    # find the focused data item
    window = api.application.document_windows[0]
    src_data_item = window.target_data_item
    if src_data_item:

        src_xdata = src_data_item.xdata
        # check to make sure it is suitable for this algorithm
        if src_xdata.is_datum_1d and (src_xdata.is_sequence or src_xdata.is_collection):

            # get the numpy array and create the destination data
            src_data = src_data_item.data
            dst_data = numpy.zeros(src_data.shape)

            # set up the indexing. to make this algorithm work with any indexing,
            # we will iterate over all non-datum dimensions using numpy.unravel_index.
            d_rank = src_xdata.datum_dimension_count
            src_shape = tuple(src_xdata.data_shape)
            s_shape = src_shape[0:-d_rank]
            count = int(numpy.prod(s_shape))

            # use this as the reference position. all other spectra will be aligned to this one.
            ref_pos = numpy.argmax(src_data[0, 0])

            # loop over all non-datum dimensions linearly
            for i in range(count):
                # generate the index for the non-datum dimensions using unravel_index
                ii = numpy.unravel_index(i, s_shape)

                # the algorithm in this early version is to find the max value
                mx_pos = numpy.argmax(src_data[ii])

                # determine the offset (an integer) and store the shifted data into the result
                offset = mx_pos - ref_pos
                if offset < 0:
                    dst_data[ii][-offset:] = src_data[ii][0:offset]
                elif offset > 0:
                    dst_data[ii][:-offset] = src_data[ii][offset:]
                else:
                    dst_data[ii][:] = src_data[ii][:]

                # if the last index is 0, report progress
                if ii[-1] == 0:
                    print(f"At index {ii}")

                # check to see if the user canceled
                if interactive.cancelled:
                    break

            dimensional_calibrations = src_xdata.dimensional_calibrations
            energy_calibration = dimensional_calibrations[-1]
            energy_calibration.offset = -(ref_pos + 0.5) * energy_calibration.scale
            dimensional_calibrations = dimensional_calibrations[0:-1] + [energy_calibration]

            # dst_data is complete. construct xdata with correct calibration and data descriptor.
            dst_xdata = DataAndMetadata.new_data_and_metadata(dst_data, src_xdata.intensity_calibration, dimensional_calibrations, data_descriptor=DataAndMetadata.DataDescriptor(False, 2, 1))

            # create a new data item in the library and set its title.
            data_item = api.library.create_data_item_from_data_and_metadata(dst_xdata)
            data_item.title = "Aligned " + src_data_item.title

            # display the data item.
            window.display_data_item(data_item)
        else:
            print("Failed: Data is not a sequence or collection of 1D spectra.")
    else:
        print("Failed: No data item selected.")
//  [python]
def _create_specs(self, X, y=None):
        # Automatically work out the output unit count based on dataset.
        if y is not None and self.layers[-1].units is None:
            self.layers[-1].units = y.shape[1]
        else:
            assert y is None or self.layers[-1].units == y.shape[1],\
                "Mismatch between dataset size and units in output layer."

        # Then compute the number of units in each layer for initialization.
        self.unit_counts = [numpy.product(X.shape[1:]) if self.is_convolution() else X.shape[1]]
        res = X.shape[1:3] if self.is_convolution() else None

        for l in self.layers:
            if isinstance(l, Convolution):
                assert l.kernel_shape is not None,\
                    "Layer `%s` requires parameter `kernel_shape` to be set." % (l.name,)
                if l.border_mode == 'valid':
                    res = (int((res[0] - l.kernel_shape[0]) / l.pool_shape[0]) + 1,
                           int((res[1] - l.kernel_shape[1]) / l.pool_shape[1]) + 1)
                if l.border_mode == 'full':
                    res = (int((res[0] + l.kernel_shape[0]) / l.pool_shape[0]) - 1,
                           int((res[1] + l.kernel_shape[1]) / l.pool_shape[1]) - 1)
                           
                if l.scale_factor != (1, 1):
                    res = (int(l.scale_factor[0] * res[0]), int(l.scale_factor[1] * res[1]))
 
                unit_count = numpy.prod(res) * l.channels
            else:
                unit_count = l.units

            self.unit_counts.append(unit_count)
//  [python]
def _create_specs(self, X, y=None):
        # Automatically work out the output unit count based on dataset.
        if y is not None and self.layers[-1].units is None:
            self.layers[-1].units = y.shape[1]
        else:
            assert y is None or self.layers[-1].units == y.shape[1],\
                "Mismatch between dataset size and units in output layer."

        # Then compute the number of units in each layer for initialization.
        self.unit_counts = [numpy.prod(X.shape[1:]) if self.is_convolution() else X.shape[1]]
        res = X.shape[1:3] if self.is_convolution() else None

        for l in self.layers:
            if isinstance(l, Convolution):
                assert l.kernel_shape is not None,\
                    "Layer `%s` requires parameter `kernel_shape` to be set." % (l.name,)
                if l.border_mode == 'valid':
                    res = (int((res[0] - l.kernel_shape[0]) / l.pool_shape[0]) + 1,
                           int((res[1] - l.kernel_shape[1]) / l.pool_shape[1]) + 1)
                if l.border_mode == 'full':
                    res = (int((res[0] + l.kernel_shape[0]) / l.pool_shape[0]) - 1,
                           int((res[1] + l.kernel_shape[1]) / l.pool_shape[1]) - 1)
                           
                if l.scale_factor != (1, 1):
                    res = (int(l.scale_factor[0] * res[0]), int(l.scale_factor[1] * res[1]))
 
                unit_count = numpy.prod(res) * l.channels
            else:
                unit_count = l.units

            self.unit_counts.append(unit_count)
//  [python]
def measure_objects(self, object_set, workspace):
        objects = workspace.get_objects(object_set)

        label_image = objects.segmented

        if objects.has_parent_image:
            mask = objects.parent_image.mask

            label_image[~mask] = 0

            total_area = numpy.sum(mask)
        else:
            total_area = numpy.product(label_image.shape)

        region_properties = skimage.measure.regionprops(label_image)

        area_occupied = numpy.sum([region["area"] for region in region_properties])
        
        if area_occupied > 0:
            if objects.volumetric:
                spacing = None

                if objects.has_parent_image:
                    spacing = objects.parent_image.spacing

                labels = numpy.unique(label_image)

                if labels[0] == 0:
                    labels = labels[1:]

                perimeter = surface_area(label_image, spacing=spacing, index=labels)
            else:
                perimeter = numpy.sum(
                    [numpy.round(region["perimeter"]) for region in region_properties]
                )
        else:
            perimeter = 0

        measurements = workspace.measurements
        pipeline = workspace.pipeline

        self._add_image_measurement(
            object_set,
            F_VOLUME_OCCUPIED if pipeline.volumetric() else F_AREA_OCCUPIED,
            area_occupied,
            measurements,
        )

        self._add_image_measurement(
            object_set,
            F_SURFACE_AREA if pipeline.volumetric() else F_PERIMETER,
            perimeter,
            measurements,
        )

        self._add_image_measurement(
            object_set,
            F_TOTAL_VOLUME if pipeline.volumetric() else F_TOTAL_AREA,
            total_area,
            measurements,
        )

        return [[object_set, str(area_occupied), str(perimeter), str(total_area),]]
//  [python]
def measure_objects(self, object_set, workspace):
        objects = workspace.get_objects(object_set)

        label_image = objects.segmented

        if objects.has_parent_image:
            mask = objects.parent_image.mask

            label_image[~mask] = 0

            total_area = numpy.sum(mask)
        else:
            total_area = numpy.prod(label_image.shape)

        region_properties = skimage.measure.regionprops(label_image)

        area_occupied = numpy.sum([region["area"] for region in region_properties])
        
        if area_occupied > 0:
            if objects.volumetric:
                spacing = None

                if objects.has_parent_image:
                    spacing = objects.parent_image.spacing

                labels = numpy.unique(label_image)

                if labels[0] == 0:
                    labels = labels[1:]

                perimeter = surface_area(label_image, spacing=spacing, index=labels)
            else:
                perimeter = numpy.sum(
                    [numpy.round(region["perimeter"]) for region in region_properties]
                )
        else:
            perimeter = 0

        measurements = workspace.measurements
        pipeline = workspace.pipeline

        self._add_image_measurement(
            object_set,
            F_VOLUME_OCCUPIED if pipeline.volumetric() else F_AREA_OCCUPIED,
            area_occupied,
            measurements,
        )

        self._add_image_measurement(
            object_set,
            F_SURFACE_AREA if pipeline.volumetric() else F_PERIMETER,
            perimeter,
            measurements,
        )

        self._add_image_measurement(
            object_set,
            F_TOTAL_VOLUME if pipeline.volumetric() else F_TOTAL_AREA,
            total_area,
            measurements,
        )

        return [[object_set, str(area_occupied), str(perimeter), str(total_area),]]
//  [python]
def test_02_01_relate_zeros(self):
        """Relate a field of empty parents to empty children"""
        labels = numpy.zeros((10, 10), int)
        workspace, module = self.make_workspace(labels, labels)
        module.wants_per_parent_means.value = False
        module.run(workspace)
        m = workspace.measurements
        parents_of = m.get_current_measurement(
            CHILD_OBJECTS, "Parent_%s" % PARENT_OBJECTS
        )
        self.assertEqual(numpy.product(parents_of.shape), 0)
        child_count = m.get_current_measurement(
            PARENT_OBJECTS, "Children_%s_Count" % CHILD_OBJECTS
        )
        self.assertEqual(numpy.product(child_count.shape), 0)
        self.features_and_columns_match(workspace)
//  [python]
def test_02_01_relate_zeros(self):
        """Relate a field of empty parents to empty children"""
        labels = numpy.zeros((10, 10), int)
        workspace, module = self.make_workspace(labels, labels)
        module.wants_per_parent_means.value = False
        module.run(workspace)
        m = workspace.measurements
        parents_of = m.get_current_measurement(
            CHILD_OBJECTS, "Parent_%s" % PARENT_OBJECTS
        )
        self.assertEqual(numpy.prod(parents_of.shape), 0)
        child_count = m.get_current_measurement(
            PARENT_OBJECTS, "Children_%s_Count" % CHILD_OBJECTS
        )
        self.assertEqual(numpy.prod(child_count.shape), 0)
        self.features_and_columns_match(workspace)
//  [python]
def test_02_01_relate_one(self):
        """Relate one parent to one child"""
        parent_labels = numpy.ones((10, 10), int)
        child_labels = numpy.zeros((10, 10), int)
        child_labels[3:5, 4:7] = 1
        workspace, module = self.make_workspace(parent_labels, child_labels)
        module.wants_per_parent_means.value = False
        module.run(workspace)
        m = workspace.measurements
        parents_of = m.get_current_measurement(
            CHILD_OBJECTS, "Parent_%s" % PARENT_OBJECTS
        )
        self.assertEqual(numpy.product(parents_of.shape), 1)
        self.assertEqual(parents_of[0], 1)
        child_count = m.get_current_measurement(
            PARENT_OBJECTS, "Children_%s_Count" % CHILD_OBJECTS
        )
        self.assertEqual(numpy.product(child_count.shape), 1)
        self.assertEqual(child_count[0], 1)
        self.features_and_columns_match(workspace)
//  [python]
def test_02_01_relate_one(self):
        """Relate one parent to one child"""
        parent_labels = numpy.ones((10, 10), int)
        child_labels = numpy.zeros((10, 10), int)
        child_labels[3:5, 4:7] = 1
        workspace, module = self.make_workspace(parent_labels, child_labels)
        module.wants_per_parent_means.value = False
        module.run(workspace)
        m = workspace.measurements
        parents_of = m.get_current_measurement(
            CHILD_OBJECTS, "Parent_%s" % PARENT_OBJECTS
        )
        self.assertEqual(numpy.prod(parents_of.shape), 1)
        self.assertEqual(parents_of[0], 1)
        child_count = m.get_current_measurement(
            PARENT_OBJECTS, "Children_%s_Count" % CHILD_OBJECTS
        )
        self.assertEqual(numpy.prod(child_count.shape), 1)
        self.assertEqual(child_count[0], 1)
        self.features_and_columns_match(workspace)
//  [python]
def test_02_02_relate_wrong_size(self):
        """Regression test of IMG-961

        Perhaps someone is trying to relate cells to wells and the grid
        doesn't completely cover the labels matrix.
        """
        parent_labels = numpy.ones((20, 10), int)
        parent_labels[10:, :] = 0
        child_labels = numpy.zeros((10, 20), int)
        child_labels[3:5, 4:7] = 1
        workspace, module = self.make_workspace(parent_labels, child_labels)
        module.wants_per_parent_means.value = False
        module.run(workspace)
        m = workspace.measurements
        parents_of = m.get_current_measurement(
            CHILD_OBJECTS, "Parent_%s" % PARENT_OBJECTS
        )
        self.assertEqual(numpy.product(parents_of.shape), 1)
        self.assertEqual(parents_of[0], 1)
        child_count = m.get_current_measurement(
            PARENT_OBJECTS, "Children_%s_Count" % CHILD_OBJECTS
        )
        self.assertEqual(numpy.product(child_count.shape), 1)
        self.assertEqual(child_count[0], 1)
        self.features_and_columns_match(workspace)
//  [python]
def test_02_02_relate_wrong_size(self):
        """Regression test of IMG-961

        Perhaps someone is trying to relate cells to wells and the grid
        doesn't completely cover the labels matrix.
        """
        parent_labels = numpy.ones((20, 10), int)
        parent_labels[10:, :] = 0
        child_labels = numpy.zeros((10, 20), int)
        child_labels[3:5, 4:7] = 1
        workspace, module = self.make_workspace(parent_labels, child_labels)
        module.wants_per_parent_means.value = False
        module.run(workspace)
        m = workspace.measurements
        parents_of = m.get_current_measurement(
            CHILD_OBJECTS, "Parent_%s" % PARENT_OBJECTS
        )
        self.assertEqual(numpy.prod(parents_of.shape), 1)
        self.assertEqual(parents_of[0], 1)
        child_count = m.get_current_measurement(
            PARENT_OBJECTS, "Children_%s_Count" % CHILD_OBJECTS
        )
        self.assertEqual(numpy.prod(child_count.shape), 1)
        self.assertEqual(child_count[0], 1)
        self.features_and_columns_match(workspace)
//  [python]
def test_02_03_relate_ijv(self):
        """Regression test of IMG-1317: relating objects in ijv form"""

        child_ijv = numpy.array([[5, 5, 1], [5, 6, 2], [20, 15, 3]])
        parent_ijv = numpy.array([[5, 5, 1], [5, 6, 1], [20, 15, 2]])
        workspace, module = self.make_workspace(parent_ijv, child_ijv)
        module.wants_per_parent_means.value = False
        module.run(workspace)
        m = workspace.measurements
        parents_of = m.get_current_measurement(
            CHILD_OBJECTS, "Parent_%s" % PARENT_OBJECTS
        )
        self.assertEqual(numpy.product(parents_of.shape), 3)
        self.assertTrue(parents_of[0], 1)
        self.assertEqual(parents_of[1], 1)
        self.assertEqual(parents_of[2], 2)
        child_count = m.get_current_measurement(
            PARENT_OBJECTS, "Children_%s_Count" % CHILD_OBJECTS
        )
        self.assertEqual(numpy.product(child_count.shape), 2)
        self.assertEqual(child_count[0], 2)
        self.assertEqual(child_count[1], 1)
//  [python]
def test_02_03_relate_ijv(self):
        """Regression test of IMG-1317: relating objects in ijv form"""

        child_ijv = numpy.array([[5, 5, 1], [5, 6, 2], [20, 15, 3]])
        parent_ijv = numpy.array([[5, 5, 1], [5, 6, 1], [20, 15, 2]])
        workspace, module = self.make_workspace(parent_ijv, child_ijv)
        module.wants_per_parent_means.value = False
        module.run(workspace)
        m = workspace.measurements
        parents_of = m.get_current_measurement(
            CHILD_OBJECTS, "Parent_%s" % PARENT_OBJECTS
        )
        self.assertEqual(numpy.prod(parents_of.shape), 3)
        self.assertTrue(parents_of[0], 1)
        self.assertEqual(parents_of[1], 1)
        self.assertEqual(parents_of[2], 2)
        child_count = m.get_current_measurement(
            PARENT_OBJECTS, "Children_%s_Count" % CHILD_OBJECTS
        )
        self.assertEqual(numpy.prod(child_count.shape), 2)
        self.assertEqual(child_count[0], 2)
        self.assertEqual(child_count[1], 1)
//  [python]
def _logsum(matrix):
    """Implement logsum for a matrix object (PRIVATE)."""
    if len(matrix.shape) > 1:
        vec = numpy.reshape(matrix, (numpy.product(matrix.shape),))
    else:
        vec = matrix
    sum = LOG0
    for num in vec:
        sum = logaddexp(sum, num)
    return sum
//  [python]
def _logsum(matrix):
    """Implement logsum for a matrix object (PRIVATE)."""
    if len(matrix.shape) > 1:
        vec = numpy.reshape(matrix, (numpy.prod(matrix.shape),))
    else:
        vec = matrix
    sum = LOG0
    for num in vec:
        sum = logaddexp(sum, num)
    return sum
//  [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 is_empty_dataspace(attr):
                    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)