def initCoords(self): size=self.vol_dim[0]/2 x, y=np.meshgrid(np.arange(-size+1,size+1),np.arange(-size+1,size+1)) #temp=np.arctan2(y, x) #temp=np.reshape(temp,(size*2,size*2,1)) #self.radmatrix=np.repeat(temp, size*2, axis=2) self.radmatrix=np.remainder(np.arctan2(x, y)+2*np.pi,2*np.pi)-2*np.pi self.zline=np.arange(-size+1,size+1)
def leave_trail(self, additive=False): """Each particle leaves it's pheromone trace on the trace array. If the trace is additive, it gets added, otherwise the trace array is set to the value of the trace.""" trace_strength = self.trace_strength if additive: organisms_pos = np.floor( np.remainder(self.population, np.array(self.trail_map.shape))).astype(int) vals, count = Physarum.cupy_unique_axis0(organisms_pos, return_counts=True) self.trail_map[vals[:, 0], vals[:, 1]] = self.trail_map[ vals[:, 0], vals[:, 1]] + (count * trace_strength) else: self.trail_map[np.remainder(self.population, self.trail_map.shape)] = trace_strength return self.trail_map
def optimized_update_positions(positions, angle, theta_sense, horizon_sense, theta_walk, horizon_walk, trace_array): """Returns the adapted physarum-positions, given initial coordinates and constants. This function is optimized by using Cupy (implementation of NumPy-compatible multi-dimensional array on CUDA)""" ### Get all possible positions to test # get the new 3 angles to test for each organism angles_to_test = np.hstack(( (angle - theta_sense) % (2 * np.pi), angle, (angle + theta_sense) % (2 * np.pi), )).reshape(-1, 3) # get positions to test based on current positions and angles pos_to_test = positions.reshape(-1, 1, 2) + np.stack( (horizon_sense * np.cos(angles_to_test), horizon_sense * np.sin(angles_to_test)), axis=-1) pos_to_test = np.remainder(pos_to_test, np.array(trace_array.shape)) ### Get all possible positions to walk to # get the new 3 angles to walk to for each organism angles_to_walk = np.hstack(( (angle - theta_walk) % (2 * np.pi), angle, (angle + theta_walk) % (2 * np.pi), )).reshape(-1, 3) # get positions to walk to based on current positions and angles pos_to_walk = positions.reshape(-1, 1, 2) + np.stack( (horizon_walk * np.cos(angles_to_walk), horizon_walk * np.sin(angles_to_walk)), axis=-1) pos_to_walk = np.remainder(pos_to_walk, np.array(trace_array.shape)) ### Get the positions to walk too based on the best positions out of the tested ones pos_to_test = np.floor(pos_to_test).astype(np.int64) - 1 # TODO notice argmax will always return first when multiple entries are equal best_indexes = trace_array[pos_to_test[:, :, 0], pos_to_test[:, :, 1]].argmax(axis=-1) new_positions = pos_to_walk[np.arange(len(pos_to_test)), best_indexes] new_angles = angles_to_walk[np.arange(len(pos_to_test)), best_indexes].reshape(-1, 1) return new_positions, new_angles
def remainder(x1: Array, x2: Array, /) -> Array: """ Array API compatible wrapper for :py:func:`np.remainder <numpy.remainder>`. See its docstring for more information. """ if x1.dtype not in _numeric_dtypes or x2.dtype not in _numeric_dtypes: raise TypeError("Only numeric dtypes are allowed in remainder") # Call result type here just to raise on disallowed type combinations _result_type(x1.dtype, x2.dtype) x1, x2 = Array._normalize_two_args(x1, x2) return Array._new(np.remainder(x1._array, x2._array))
def createWedge(self, subunit_num=0): ###Initialize the location of the protofilament to remove theta0=np.arctan2(self.com[0], self.com[1])+\ np.deg2rad(subunit_num*self.twist_per_subunit) z0=(self.com[2]+self.rise_per_subunit*subunit_num)/self.pixel_size ###Define the length along the protofilament in terms of subunits zsubunits=(self.zline.copy()-z0)*self.pixel_size/self.dimer_repeat_dist ###Define the angle of the center of the protofilament along the length of the segment theta=np.deg2rad((-self.helical_twist)*zsubunits)+theta0 ###Initialize the wedge mask wedge=np.zeros(self.vol_dim.tolist()) ###Define the size of the wedgemask fudge=np.deg2rad(360.0/(self.num_pfs*2)) ###Generate the wedge mask for i in range(len(theta)): temp1=np.remainder(theta[i]-fudge+2*np.pi,2*np.pi)-2*np.pi temp2=np.remainder(theta[i]+fudge+2*np.pi,2*np.pi)-2*np.pi angles=[temp1, temp2] if max(angles)-min(angles)>2*fudge+.2: above=max(angles) below=min(angles) inds=np.logical_or(self.radmatrix>above,self.radmatrix<below) else: above=min(angles) below=max(angles) inds=np.logical_and(self.radmatrix>above,self.radmatrix<below) wedge[i,:,:][inds]=1 return wedge
def map_coordinates(input, coordinates, output=None, order=None, mode='constant', cval=0.0, prefilter=True): """Map the input array to new coordinates by interpolation. The array of coordinates is used to find, for each point in the output, the corresponding coordinates in the input. The value of the input at those coordinates is determined by spline interpolation of the requested order. The shape of the output is derived from that of the coordinate array by dropping the first axis. The values of the array along the first axis are the coordinates in the input array at which the output value is found. Args: input (cupy.ndarray): The input array. coordinates (array_like): The coordinates at which ``input`` is evaluated. output (cupy.ndarray or ~cupy.dtype): The array in which to place the output, or the dtype of the returned array. order (int): The order of the spline interpolation. If it is not given, order 1 is used. It is different from :mod:`scipy.ndimage` and can change in the future. Currently it supports only order 0 and 1. mode (str): Points outside the boundaries of the input are filled according to the given mode (``'constant'``, ``'nearest'``, ``'mirror'`` or ``'opencv'``). Default is ``'constant'``. cval (scalar): Value used for points outside the boundaries of the input if ``mode='constant'`` or ``mode='opencv'``. Default is 0.0 prefilter (bool): It is not used yet. It just exists for compatibility with :mod:`scipy.ndimage`. Returns: cupy.ndarray: The result of transforming the input. The shape of the output is derived from that of ``coordinates`` by dropping the first axis. .. seealso:: :func:`scipy.ndimage.map_coordinates` """ _check_parameter('map_coordinates', order, mode) if mode == 'opencv' or mode == '_opencv_edge': input = cupy.pad(input, [(1, 1)] * input.ndim, 'constant', constant_values=cval) coordinates = cupy.add(coordinates, 1) mode = 'constant' ret = _get_output(output, input, coordinates.shape[1:]) if mode == 'nearest': for i in six.moves.range(input.ndim): coordinates[i] = coordinates[i].clip(0, input.shape[i] - 1) elif mode == 'mirror': for i in six.moves.range(input.ndim): length = input.shape[i] - 1 if length == 0: coordinates[i] = 0 else: coordinates[i] = cupy.remainder(coordinates[i], 2 * length) coordinates[i] = 2 * cupy.minimum(coordinates[i], length) - coordinates[i] if input.dtype.kind in 'iu': input = input.astype(cupy.float32) if order == 0: out = input[tuple(cupy.rint(coordinates).astype(cupy.int32))] else: coordinates_floor = cupy.floor(coordinates).astype(cupy.int32) coordinates_ceil = coordinates_floor + 1 sides = [] for i in six.moves.range(input.ndim): # TODO(mizuno): Use array_equal after it is implemented if cupy.all(coordinates[i] == coordinates_floor[i]): sides.append([0]) else: sides.append([0, 1]) out = cupy.zeros(coordinates.shape[1], dtype=input.dtype) if input.dtype in (cupy.float64, cupy.complex128): weight = cupy.empty(coordinates.shape[1], dtype=cupy.float64) else: weight = cupy.empty(coordinates.shape[1], dtype=cupy.float32) for side in itertools.product(*sides): weight.fill(1) ind = [] for i in six.moves.range(input.ndim): if side[i] == 0: ind.append(coordinates_floor[i]) weight *= coordinates_ceil[i] - coordinates[i] else: ind.append(coordinates_ceil[i]) weight *= coordinates[i] - coordinates_floor[i] out += input[ind] * weight del weight if mode == 'constant': mask = cupy.zeros(coordinates.shape[1], dtype=cupy.bool_) for i in six.moves.range(input.ndim): mask += coordinates[i] < 0 mask += coordinates[i] > input.shape[i] - 1 out[mask] = cval del mask if ret.dtype.kind in 'iu': out = cupy.rint(out) ret[:] = out return ret
def __rmod__(self, other): return cupy.remainder(other, self)
def __mod__(self, other): return cupy.remainder(self, other)