def circle(self) : self.BlocksToWrite = copy.deepcopy(self.Blocks) self.Writer = fitsGBT.Writer(self.BlocksToWrite, 0) self.Writer.write('temp.fits') self.newReader = fitsGBT.Reader('temp.fits', 0) self.newBlocks = self.newReader.read() self.assertEqual(len(self.Blocks), len(self.newBlocks)) for ii in range(len(self.newBlocks)) : OldDB = self.Blocks[ii] NewDB = self.newBlocks[ii] for jj in range(4) : self.assertEqual(OldDB.dims[ii], NewDB.dims[ii]) self.assertTrue(ma.allclose(OldDB.data, NewDB.data)) for field, axis in fitsGBT.fields_and_axes.iteritems() : self.assertEqual(axis, OldDB.field_axes[field]) self.assertEqual(axis, NewDB.field_axes[field]) for field in ['SCAN', 'OBJECT', 'TIMESTAMP', 'OBSERVER', 'CRPIX1', 'CDELT1'] : self.assertEqual(OldDB.field[field], NewDB.field[field]) for field in ['CRVAL1', 'BANDWID', 'RESTFREQ', 'DURATION'] : self.assertAlmostEqual(OldDB.field[field], NewDB.field[field]) for field in ['LST', 'ELEVATIO', 'AZIMUTH', 'RA', 'DEC', 'OBSFREQ', 'CRVAL2', 'CRVAL3', 'EXPOSURE'] : self.assertTrue(sp.allclose(OldDB.field[field], NewDB.field[field])) for field in ['DATE-OBS'] : self.assertTrue(sp.alltrue(sp.equal(OldDB.field[field], NewDB.field[field]))) for field in ['CRVAL4', 'CAL'] : self.assertTrue(all(OldDB.field[field] == NewDB.field[field]))
def numpyAssertEqual(self, a1, a2): """Test for equality of array fields a1 and a2.""" self.assertEqual(type(a1), type(a2)) self.assertEqual(a1.shape, a2.shape) self.assertEqual(a1.dtype, a2.dtype) self.assertTrue(alltrue(equal(a1.ravel(), a2.ravel())))
def get_usgs_n(self): if self.get_usgsrc() == 0: return self.get_values( ) # Fetch usgsq,usgsh,handq,handh,handarea,handrad,handslope, handstage # Find indices for integer stageheight values in usgsh, and apply to usgsq usgsidx = scipy.where(scipy.equal(scipy.mod( self.usgsh, 1), 0)) # Find indices of integer values in usgsh usgsh = self.usgsh[usgsidx] usgsq = self.usgsq[usgsidx] # Find indices where usgsh[usgsidx] occur in handstage, and apply to handarea and handrad handidx = scipy.where(scipy.in1d(self.handstage, usgsh)) area = self.handarea[handidx] hydrad = self.handrad[handidx] # Remove usgsq values for duplicate usgsh heights (keep first instance only) if usgsh.shape != area.shape: for i in range(usgsh.shape[0]): if i == 0: pass elif usgsh[i] == usgsh[i - 1]: usgsq = scipy.delete(usgsq, i) # Calculate average manning's n after converting discharge units disch = usgsq #*0.0283168 # Convert cfs to cms self.usgsroughness_array = self.mannings_n(area=area, hydrad=hydrad, slope=self.handslope, disch=disch) self.usgsroughness = scipy.average(self.usgsroughness_array) print 'Average roughness: {0:.2f}'.format(self.usgsroughness)
def hardmax(self, Z): """ Transform each row in array Z into another row with zeroes in the non-maximum values and 1/nmax on the maximum values, where nmax is the number of elements taking the maximum value """ D = sp.equal(Z, np.max(Z, axis=1, keepdims=True)) # In case more than one value is equal to the maximum, the output # of hardmax is nonzero for all of them, but normalized D = D / np.sum(D, axis=1, keepdims=True) return D
def __test_eigenpairs(vals, vecs, matrix): """Test correct computation of eigenpairs Parameters ---------- vals : vector of eigenvalues vecs : eigenvector matrix, vecs[:,i] corresponding to eigenvalue vals[i] matrix : matrix from where eigenpairs came from Returns ------- res : list of (bool, float) truth value of eigenpair corectness along with error in computation """ res = [] for i in range(len(vals)): A = matrix * vecs[:, i] B = vals[i] * vecs[:, i] res.append((any(sp.equal(A, B)), sp.sum(A - B))) print('-----------laplacian test:') print(res) return res
def circle(self): self.BlocksToWrite = copy.deepcopy(self.Blocks) self.Writer = fitsGBT.Writer(self.BlocksToWrite, 0) self.Writer.write('temp.fits') self.newReader = fitsGBT.Reader('temp.fits', 0) self.newBlocks = self.newReader.read() self.assertEqual(len(self.Blocks), len(self.newBlocks)) for ii in range(len(self.newBlocks)): OldDB = self.Blocks[ii] NewDB = self.newBlocks[ii] for jj in range(4): self.assertEqual(OldDB.dims[ii], NewDB.dims[ii]) self.assertTrue(ma.allclose(OldDB.data, NewDB.data)) for field, axis in fitsGBT.fields_and_axes.iteritems(): self.assertEqual(axis, OldDB.field_axes[field]) self.assertEqual(axis, NewDB.field_axes[field]) for field in [ 'SCAN', 'OBJECT', 'TIMESTAMP', 'OBSERVER', 'CRPIX1', 'CDELT1' ]: self.assertEqual(OldDB.field[field], NewDB.field[field]) for field in ['CRVAL1', 'BANDWID', 'RESTFREQ', 'DURATION']: self.assertAlmostEqual(OldDB.field[field], NewDB.field[field]) for field in [ 'LST', 'ELEVATIO', 'AZIMUTH', 'RA', 'DEC', 'OBSFREQ', 'CRVAL2', 'CRVAL3', 'EXPOSURE' ]: self.assertTrue( sp.allclose(OldDB.field[field], NewDB.field[field])) for field in ['DATE-OBS']: self.assertTrue( sp.alltrue(sp.equal(OldDB.field[field], NewDB.field[field]))) for field in ['CRVAL4', 'CAL']: self.assertTrue(all(OldDB.field[field] == NewDB.field[field]))
def get_in_common(self): # Find indices for integer stageheight values in usgsh, and apply to usgsq usgsidx = scipy.where(scipy.equal(scipy.mod(self.usgsh,1),0)) # Find indices of integer values in usgsh usgshint = self.usgsh[usgsidx] # Integers in usgsh usgsqint = self.usgsq[usgsidx] # Integers in usgsq # Find indices where usgshint[usgsidx] occur in handstage, and apply to handarea and handrad handidx = scipy.where(scipy.in1d(self.handstage,usgshint)) handareaint = self.handarea[handidx] handradint = self.handrad[handidx] # Remove usgsqint values for duplicate usgshint heights (keep first instance only) if usgshint.shape != handareaint.shape: for i in range(usgshint.shape[0]): if i == 0: pass elif usgshint[i] == usgshint[i-1]: usgsqint = scipy.delete(usgsqint,i) self.usgshint = usgshint self.usgsqint = usgsqint self.handareaint = handareaint self.handradint = handradint self.usgsidx = usgsidx self.handidx = handidx
def are_matrix_equal(m1, m2): return s.equal(m1, m2).all()
def minstates(v): "return the indices of v that equal the minimum" return scipy.nonzero(scipy.equal(v, min(v)))