def __init__(self, xmin, xmax, ymin, ymax, binsize, regfilter, expomap, eventfile): self.xmin = xmin self.xmax = xmax self.ymin = ymin self.ymax = ymax assert self.ymax > self.ymin assert self.xmax > self.xmin # At least two bins! assert binsize < (self.xmax - self.xmin) / 2.0 assert binsize < (self.ymax - self.ymin) / 2.0 self.binsize = binsize self.xbins = numpy.arange(self.xmin, self.xmax, self.binsize) self.ybins = numpy.arange(self.ymin, self.ymax, self.binsize) # Prepare mask self.xcs = (self.xbins[:-1] + self.xbins[1:]) / 2.0 self.ycs = (self.ybins[:-1] + self.ybins[1:]) / 2.0 points = cartesian.cartesian([self.xcs, self.ycs]) # plt.imshow(exposures) self.mask = numpy.zeros((self.ycs.shape[0], self.xcs.shape[0]), dtype=bool) for i in range(self.mask.shape[0]): for j in range(self.mask.shape[1]): self.mask[i, j] = regfilter.inside1(self.xcs[j], self.ycs[i]) expo = ExposureMap.Exposure(expomap) wcs = XMMWCS.XMMWCS(eventfile) skypoints = wcs.xy2sky(points) ras = skypoints[:, 0] decs = skypoints[:, 1] exposures = expo.getExposureAtSky(ras, decs).reshape(self.mask.T.shape).T idx = exposures <= (exposures.max() / 2.0) self.mask[idx] = 0 self.empty = ~self.mask
def build_weights_matrix(Y): (n, m) = [Y.shape[0], Y.shape[1]] S = std_matrix(Y) size = n * m cart = cartesian.cartesian([range(n), range(m)]) cart_r = cart.reshape(n, m, 2) xy2idx = np.arange(size).reshape(n, m) # [x,y] -> index W = sparse.lil_matrix((size, size)) for i in range(Y.shape[0]): for j in range(Y.shape[1]): idx = xy2idx[i, j] N = neighborhood(cart_r, [i, j]).reshape(-1, 2) N = [tuple(neighbor) for neighbor in N] N.remove((i, j)) p_idx = [xy2idx[xy] for xy in N] weights = weight((i, j), N, Y, S) W[idx, p_idx] = -1 * np.asmatrix(weights) Wn = normalize(W, norm='l1', axis=1) Wn[np.arange(size), np.arange(size)] = 1 return Wn
def build_weights_matrix(Y): (n, m) = [Y.shape[0], Y.shape[1]] S = std_matrix(Y) size = n * m cart = cartesian.cartesian([xrange(n), xrange(m)]) cart_r = cart.reshape(n, m, 2) xy2idx = np.arange(size).reshape(n, m) # [x,y] -> index W = sparse.lil_matrix((size, size)) for i in xrange(Y.shape[0]): for j in xrange(Y.shape[1]): idx = xy2idx[i, j] N = neighborhood(cart_r, [i, j]).reshape(-1, 2) N = [tuple(neighbor) for neighbor in N] N.remove((i, j)) p_idx = [xy2idx[xy] for xy in N] weights = weight((i, j), N, Y, S) W[idx, p_idx] = -1 * np.asmatrix(weights) Wn = normalize(W, norm='l1', axis=1) Wn[np.arange(size), np.arange(size)] = 1 return Wn
def build_weights_matrix(y): (n, m) = [y.shape[0], y.shape[1]] s = std_matrix(y) size = n * m cart = cartesian.cartesian([xrange(n), xrange(m)]) cart_r = cart.reshape(n, m, 2) xy2idx = np.arange(size).reshape(n, m) # [x,y] -> index w = sparse.lil_matrix((size, size)) for i in xrange(y.shape[0]): for j in xrange(y.shape[1]): idx = xy2idx[i, j] n = neighborhood(cart_r, [i, j]).reshape(-1, 2) n = [tuple(neighbor) for neighbor in n] n.remove((i, j)) p_idx = [xy2idx[xy] for xy in n] weights = weight((i, j), n, y, s) w[idx, p_idx] = -1 * np.asmatrix(weights) Wn = normalize(w, norm='l1', axis=1) Wn[np.arange(size), np.arange(size)] = 1 return Wn
def plot(self, xbins, ybins, **kwargs): fig, sub = plt.subplots(1, 1) _ = sub.hist2d(self.X, self.Y, [xbins, ybins]) if len(self.centroids) > 0: cc = numpy.array(self.centroids) sub.scatter(cc[:, 0], cc[:, 1], s=80, alpha=0.8, facecolors='none', edgecolors='r', lw=2) if 'filter' in kwargs.keys(): xcs = (xbins[:-1] + xbins[1:]) / 2.0 ycs = (ybins[:-1] + ybins[1:]) / 2.0 points = cartesian.cartesian([xcs, ycs]) idx = kwargs['filter'].inside(points[:, 0], points[:, 1]) sub.plot(points[~idx, 0], points[~idx, 1], 'x', markersize=8, color='white') #for cell in self.cells.flatten(): # sub.add_patch( Rectangle((cell.x, cell.y), cell.w, cell.h, color='red',alpha=0.5, fill=False) ) return fig
def neighbourDirections(dimensions): arrays = [np.asarray([-1,0,1]) for i in range(dimensions)] return np.delete(cartesian(arrays), (3**dimensions)/2,0)
def regrid(sourceimage,targetimage,fillval = NAN,theader = 0,tpix = []): """ This takes sourceimage and puts it onto targetimage grid, using wcs solutions for both. Grid points with no info from sourceimage are set to fillval. Requires scipy0.14.0 sourceimage: path to image to regrid - should already be convolved to appropriate resolution using resconvolve targetimage: path to image whose grid is to be used in regridding fillval: value to give to empty grid positions (kwarg, default = NAN) theader: specify a header containing WCS solution to use (kwarg, default = 0) Returns array with targetimage dimensions. """ # Start timer start = time.time() # Load in source data and header information sdata,sheader = fits.getdata(sourceimage,header = True) # Create WCS object for the source image sourcewcs = wcs.WCS(sheader) # Create array of pixel indices in source image x = arange(sdata.shape[1]) y = arange(sdata.shape[0]) # Interpolate the image data over pixel indices interp = scipy.interpolate.RectBivariateSpline(y,x,sdata) # Load in target grid data if theader == 0 or tpix == []: tdata,theader = fits.getdata(targetimage,header=True) # Create all possible pairs of pixel coordinates in target grid coords = cartesian([arange(tdata.shape[1]),arange(tdata.shape[0])]) elif theader != 0 and tpix != []: assert len(tpix) == 4 dx,ux,dy,uy = tpix # Create all possible pairs of pixel coordinates in target grid tdata = zeros((uy-dy,ux-dx)) coords = cartesian([arange(dx,ux),arange(dy,uy)]) # Extract x and y columns of pixel pairs xpixs = coords[:,0] ypixs= coords[:,1] # Create WCS object for target grid targetwcs = wcs.WCS(theader) # Convert target grid pixels to ra/dec world = targetwcs.wcs_pix2world(coords,0) # Convert target grid ra/dec to source pixel coordinates dpix = sourcewcs.wcs_world2pix(world,0) # Extract x and y columns of converted pixel pairs xdpixs = dpix[:,0] ydpixs = dpix[:,1] # Find where target grid corresponds to actual source image data good = where((xdpixs >= min(x)) & (xdpixs <= max(x)) & (ydpixs >= min(y)) & (ydpixs <= max(y))) # Pick out only pixels with relevant image data xpixs = xpixs[good] ypixs = ypixs[good] xdpixs = xdpixs[good] ydpixs = ydpixs[good] # Create grid to fill up with source image regrid tofill = copy(tdata) tofill[:] = fillval # Choose indices of array positions to be changed inds = (ypixs,xpixs) tofill[inds] = interp(ydpixs,xdpixs,grid=False) # needs scipy 0.14.0 # End timer end = time.time() # Print time to run print 'Regridded in ',(end-start)/60.,' min' return tofill,tdata
def longest_path(self, spur_node_position=0, spur_node=None): """ Find the longest path in a multi-partite graph. This is an implementation of algorithm (1). spur_node_position : int Position of the spur node in the multi-partite graph, 0 means the true source node of the graph. The spur node is used in the k-longest path algorithm were we find the longest path starting at a specific node not only from the source node of the graph. spur_node : np.array Identifier of the spur node. ex: np.array([11,4,19]) for self.substring_length = 3. Recall that each node are identified using by a substring of length self.substring_length """ # The "n" of the n-partite graph n = self.peptide_length - self.substring_length + 1 # The spur node is just before the sink node: spur_node -> t # The longest path is trivial if spur_node_position == n: return spur_node[1:], self.Wt_weight(spur_node[1:], n) # Arrays for the dynamic programming length_to = np.zeros( (2, self.aminoacid_count**self.substring_length), dtype=PATH_LENGTH_DTYPE) - PATH_LENGTH_DTYPE(np.inf) predecessor = np.zeros( (n, self.aminoacid_count**self.substring_length), dtype=INT_AMINO_ACID_DTYPE) edge_index = 0 # Edges leaving the source node if spur_node_position == 0: a_index = 0 for a in cartesian( repeat(np.arange(self.aminoacid_count, dtype=INT_AMINO_ACID_DTYPE), times=self.substring_length)): if not self.is_blacklisted(edge_index): length_to[0, a_index] = self.W_weight(a, 0) a_index += 1 edge_index += 1 # The source is a spur node else: # Keep the count edge_index += self.aminoacid_count**self.substring_length # Visit each edges of the n-partite graph using a pre-defined topological order # Edges are always of the form: a -> s for i in xrange(1, n): # Initialize the length_to vector length_to[i % 2] = length_to[i % 2] - PATH_LENGTH_DTYPE(np.inf) a_index = 0 # Do not need anymore s_index = 0 # Edges are not attainable from the spur node. # i.e. edges on the left side of the spur node if i < spur_node_position: # Keep track of the edge index edge_index += self.aminoacid_count**(self.substring_length + 1) # Edges at the spur node position elif i == spur_node_position: for s in cartesian( repeat(np.arange(self.aminoacid_count, dtype=INT_AMINO_ACID_DTYPE), times=self.substring_length)): # Weight on the edge a -> s edge_weight = self.W_weight(s, i) a_index = (s_index - s[-1]) / self.aminoacid_count for a_prime in xrange(self.aminoacid_count): # a is obtained partly from s a = np.roll(s, 1) a[0] = a_prime # Only edges of the form: spur_node -> s if np.alltrue( a == spur_node ) and not self.is_blacklisted(edge_index): # Weight on the edge spur_node -> s length_to[i % 2, s_index] = edge_weight predecessor[i, s_index] = a_prime edge_index += 1 a_index += self.aminoacid_count**( self.substring_length - 1) s_index += 1 # Edges on the right side of the spur node else: for s in cartesian( repeat(np.arange(self.aminoacid_count, dtype=INT_AMINO_ACID_DTYPE), times=self.substring_length)): # Weight on the edge a -> s edge_weight = self.W_weight(s, i) a_index = (s_index - s[-1]) / self.aminoacid_count for a_prime in xrange(self.aminoacid_count): if not self.is_blacklisted(edge_index): # Edge a -> s if length_to[i % 2, s_index] < length_to[ (i - 1) % 2, a_index] + edge_weight: length_to[i % 2, s_index] = length_to[ (i - 1) % 2, a_index] + edge_weight predecessor[i, s_index] = a_prime edge_index += 1 a_index += self.aminoacid_count**( self.substring_length - 1) s_index += 1 length_to_t = PATH_LENGTH_DTYPE(-np.inf) predecessor_of_t = None a_index = 0 # Edges heading to the sink node for a in cartesian( repeat(np.arange(self.aminoacid_count, dtype=INT_AMINO_ACID_DTYPE), times=self.substring_length)): # Their is no point at blacklisting an edge heading # to the sink node but it's possible! if not self.is_blacklisted(edge_index): edge_weight = self.Wt_weight(a[1:], n) if length_to_t < length_to[(n - 1) % 2, a_index] + edge_weight: length_to_t = length_to[(n - 1) % 2, a_index] + edge_weight predecessor_of_t = np.array(a, dtype=INT_AMINO_ACID_DTYPE) a_index += 1 edge_index += 1 # There is no path between the source and the sink if predecessor_of_t == None: return np.zeros(0, dtype=INT_AMINO_ACID_DTYPE), -np.Inf # Initialise the peptide sequence peptide = np.zeros(self.peptide_length, dtype=INT_AMINO_ACID_DTYPE) peptide[-self.substring_length:] = predecessor_of_t # Revert the path using the predecessors to find the peptide for i in range(self.peptide_length - self.substring_length)[::-1]: # Compute the prececessor index predecessor_index = 0 for j in xrange(self.substring_length): predecessor_index += peptide[i + j + 1] * self.aminoacid_count**( self.substring_length - j - 1) # Keep track of the peptide sequence peptide[i] = predecessor[i + 1, predecessor_index] # Return the encoded peptide sequence and the binding affinity (path length) return peptide[spur_node_position:], length_to_t
import cartesian import argparse import itertools import numpy as np parser = argparse.ArgumentParser() parser.add_argument('-m', '--min', help='List of minimum investment amounts for each asset in the portfolio.', nargs='+', type=float) parser.add_argument('-s', '--step', help='List of step sizes for each asset in the portfolio.', nargs='+', type=float) parser.add_argument('-x', '--max', help='List of maximum investment amounts for each asset in the portfolio.', nargs='+', type=float) args = parser.parse_args() num_assets = len(args.min) possible_portfolio_values = [None]*num_assets for i in range(num_assets): possible_portfolio_values[i] = np.arange(args.min[i], args.max[i]+args.step[i], args.step[i], dtype=float) possible_portfolios = cartesian.cartesian(possible_portfolio_values) np.savetxt(sys.stdout,possible_portfolios, delimiter=' ')
def regrid(sourceimage, targetimage, fillval=NAN, theader=0, tpix=[]): """ This takes sourceimage and puts it onto targetimage grid, using wcs solutions for both. Grid points with no info from sourceimage are set to fillval. Requires scipy0.14.0 sourceimage: path to image to regrid - should already be convolved to appropriate resolution using resconvolve targetimage: path to image whose grid is to be used in regridding fillval: value to give to empty grid positions (kwarg, default = NAN) theader: specify a header containing WCS solution to use (kwarg, default = 0) Returns array with targetimage dimensions. """ # Start timer start = time.time() # Load in source data and header information sdata, sheader = fits.getdata(sourceimage, header=True) # Create WCS object for the source image sourcewcs = wcs.WCS(sheader) # Create array of pixel indices in source image x = arange(sdata.shape[1]) y = arange(sdata.shape[0]) # Interpolate the image data over pixel indices interp = scipy.interpolate.RectBivariateSpline(y, x, sdata) # Load in target grid data if theader == 0 or tpix == []: tdata, theader = fits.getdata(targetimage, header=True) # Create all possible pairs of pixel coordinates in target grid coords = cartesian([arange(tdata.shape[1]), arange(tdata.shape[0])]) elif theader != 0 and tpix != []: assert len(tpix) == 4 dx, ux, dy, uy = tpix # Create all possible pairs of pixel coordinates in target grid tdata = zeros((uy - dy, ux - dx)) coords = cartesian([arange(dx, ux), arange(dy, uy)]) # Extract x and y columns of pixel pairs xpixs = coords[:, 0] ypixs = coords[:, 1] # Create WCS object for target grid targetwcs = wcs.WCS(theader) # Convert target grid pixels to ra/dec world = targetwcs.wcs_pix2world(coords, 0) # Convert target grid ra/dec to source pixel coordinates dpix = sourcewcs.wcs_world2pix(world, 0) # Extract x and y columns of converted pixel pairs xdpixs = dpix[:, 0] ydpixs = dpix[:, 1] # Find where target grid corresponds to actual source image data good = where((xdpixs >= min(x)) & (xdpixs <= max(x)) & (ydpixs >= min(y)) & (ydpixs <= max(y))) # Pick out only pixels with relevant image data xpixs = xpixs[good] ypixs = ypixs[good] xdpixs = xdpixs[good] ydpixs = ydpixs[good] # Create grid to fill up with source image regrid tofill = copy(tdata) tofill[:] = fillval # Choose indices of array positions to be changed inds = (ypixs, xpixs) tofill[inds] = interp(ydpixs, xdpixs, grid=False) # needs scipy 0.14.0 # End timer end = time.time() # Print time to run print 'Regridded in ', (end - start) / 60., ' min' return tofill, tdata