def test_singleclip(): ''' A module to test clipping a single polygon. ''' # define the size of the pixel grid nx, ny = 100, 100 naxis = np.array([nx, ny]) # initialize the clipper clip = polyclip.Polyclip(naxis) # create a polygon px = np.array([1.2, 1.5, 2.7, 2.5]) py = np.array([4.5, 3.2, 1.4, 1.9]) # call the clipper xc, yc, area = clip.single(px, py) # xc,yc are the coordinates in the grid # area is the relative pixel area in that grid cell # for the single, there is no notion of the polyindices # the correct values xc0 = np.array([1, 1, 1, 2, 2]) yc0 = np.array([2, 3, 4, 1, 2]) area0 = np.array([0.09833333, 0.26051286, 0.03365386, 0.0475, 0.135]) # a temporary variable diff = np.amax(np.abs(area - area0)) # apply assertion tests assert np.array_equal(xc, xc0) assert np.array_equal(yc, yc0) assert diff < __MAXDIFF__
def __init__(self, conffile, beam): valid = lambda s: (len(s) > 0 and not any( map(s.startswith, self.COMMENTS))) # record some stuff self.beam = beam self.conffile = os.path.join(os.environ['PYLINEAR_CONFIG'], conffile) print('[debug]Look into wedge offsets') # the three main functions self.dispx = Parametric('dispx') self.dispy = Parametric('dispy') self.displ = Parametric('displ') # define the local path path = os.path.dirname(self.conffile) # open the file with open(self.conffile, 'r') as fp: for line in fp: line = line.strip() if valid(line): tokens = line.split(' ') key = tokens[0].upper() val = tokens[1:] if key.startswith('FFNAME'): self.ffname = os.path.join(path, val[0]) if key.startswith('XRANGE_{}'.format(beam)): self.xr = np.array(val[0:2], dtype=np.float32) if key.startswith('YRANGE_{}'.format(beam)): self.yr = np.array(val[0:2], dtype=np.float32) if key.startswith('DISPX_{}'.format(beam)): val = [self.fix_type(v) for v in val if valid(v)] self.dispx.append(Spatial(val)) if key.startswith('DISPY_{}'.format(beam)): val = [self.fix_type(v) for v in val if valid(v)] self.dispy.append(Spatial(val)) if key.startswith('DISPL_{}'.format(beam)): val = [self.fix_type(v) for v in val if valid(v)] self.displ.append(Spatial(val)) if key.startswith('NAXIS'): self.naxis = np.array(val[0:2], dtype=np.uint16) if key.startswith('SENSITIVITY_{}'.format(beam)): sensfile = os.path.join(path, val[0]) self.sensitivity = Sensitivity(sensfile) # get a polygon clipper self.polyclip = polyclip.Polyclip(self.naxis)
def __init__(self,h5,beams='all'): # get the name of the detector self.detector=h5.name[1:] # get a few properties self.extver=h5Attr(h5,'extver') self.sciext=h5Attr(h5,'science_ext') self.uncext=h5Attr(h5,'errors_ext') self.dqaext=h5Attr(h5,'dq_ext') self.naxis=h5Attr(h5,'naxis') self.xr=h5Attr(h5,'xrange') self.yr=h5Attr(h5,'yrange') # define a polygon clipper self.clip=polyclip.Polyclip(self.naxis) # read the grism #try: #h5g=h5[grism] #self.lamb0=h5Attr(h5g,'lamb0') #self.lamb1=h5Attr(h5g,'lamb1') #self.dlamb=h5Attr(h5g,'dlamb') #except: # raise KeyError("Grism {} not found.".format(grism)) # read the beams self.beams={} if beams is not None: if beams == 'all': for bm in h5: self.beams[bm]=Beam(h5[bm],self.clip,xr=self.xr,yr=self.yr) else: if np.isscalar(beams): self.beams[beams]=Beam(h5[beams],self.clip,xr=self.xr,\ yr=self.yr) else: for bm in beams: self.beams[bm]=Beam(h5[bm],self.clip,xr=self.xr,\ yr=self.yr)
def test_multiclip(): ''' A module to test clipping multiple polygons in a single pass. ''' # define the size of the pixel grid nx, ny = 100, 100 naxis = np.array([nx, ny]) # initialize the clipper clip = polyclip.Polyclip(naxis) # print the clipping object print(clip) # create 2 polygons to clip... here they're an irregular quadralateral, but # this isn't a requirement px = np.array([[3.4, 3.4, 4.5, 4.5], [3.5, 3.5, 5.5, 5.5]]) py = np.array([[1.4, 2.0, 2.0, 1.4], [3.5, 4.3, 4.3, 3.5]]) # call the clipper xc, yc, area, polyindices = clip(px, py) # xc,yc are the coordinates in the grid # area is the relative pixel area in that grid cell # polyindices are the indices to relate the clipped pixels to the originals xc0 = np.array([3, 4, 3, 3, 4, 4, 5, 5]) yc0 = np.array([1, 1, 3, 4, 3, 4, 3, 4]) area0 = np.array([0.36, 0.3, 0.25, 0.15, 0.5, 0.3, 0.25, 0.15]) polyindices0 = np.array([0, 2, 8]) # a temporary variable diff = np.amax(np.abs(area - area0)) # apply assertion tests assert np.array_equal(xc, xc0) assert np.array_equal(yc, yc0) assert diff < __MAXDIFF__ assert np.array_equal(polyindices, polyindices0)
# import relevant modules import numpy as np import polyclip # define the size of the pixel grid nx,ny=100,100 naxis=np.array([nx,ny]) # initialize the clipper clipper=polyclip.Polyclip(naxis) # create 2 polygons to clip... here they're an irregular quadralateral, but # this isn't a requirement px=np.array([[3.4,3.4,4.5,4.5],[3.5,3.5,5.5,5.5]]) py=np.array([[1.4,2.0,2.0,1.4],[3.5,4.3,4.3,3.5]]) # call the clipper xc,yc,area,polyindices = clipper.multi(px,py) # xc,yc are the coordinates in the grid # area is the relative pixel area in that grid cell # polyindices are the indices to related the clipped pixels to the original # use these things like for j,(x,y) in enumerate(zip(px,py)): j0,j1=polyindices[j],polyindices[j+1] if j1 > j0: print(xc[j0:j1],yc[j0:j1],area[j0:j1])
def __init__(self,conffile,beam): valid=lambda s: (len(s)>0 and not any(map(s.startswith,self.COMMENTS))) # record some stuff self.beam=beam self.conffile=os.path.join(os.environ['PYLINEAR_CONFIG'],conffile) # the three main functions self.dispx=Parametric('dispx') self.dispy=Parametric('dispy') self.displ=Parametric('displ') # define the local path path=os.path.dirname(self.conffile) # add wedges self.wedge={} # open the file with open(self.conffile,'r') as fp: for line in fp: line=line.strip() if valid(line): tokens=line.split(' ') key=tokens[0].upper() #val=tokens[1:] # remove empty spaces val = list(filter(lambda x: x!='',tokens[1:])) if val: if key.startswith('FFNAME'): self.ffname=os.path.join(path,val[0]) if key.startswith('XRANGE_{}'.format(beam)): self.xr=np.array(val[0:2],dtype=np.float32) if key.startswith('YRANGE_{}'.format(beam)): self.yr=np.array(val[0:2],dtype=np.float32) if key.startswith('DISPX_{}'.format(beam)): val=[self.fix_type(v) for v in val if valid(v)] self.dispx.append(Spatial(val)) if key.startswith('DISPY_{}'.format(beam)): val=[self.fix_type(v) for v in val if valid(v)] self.dispy.append(Spatial(val)) if key.startswith('DISPL_{}'.format(beam)): val=[self.fix_type(v) for v in val if valid(v)] self.displ.append(Spatial(val)) if key.startswith('NAXIS'): self.naxis=np.array(val[0:2],dtype=np.uint16) if key.startswith('SENSITIVITY_{}'.format(beam)): sensfile=os.path.join(path,val[0]) self.sensitivity=Sensitivity(sensfile) if key.startswith('WEDGE'): filt=key.split('_')[1] val=tuple(self.fix_type(v) for v in val if valid(v)) self.wedge[filt]=val # get a polygon clipper self.polyclip=polyclip.Polyclip(self.naxis) # just some quick error checking okay=(len(self.dispx)>0 and len(self.dispy)>0 and len(self.displ)>0) assert okay,'{} order is not present in the config file.'.format(beam)