def save(self): dialog = QFileDialog(self) dialog.setFileMode(QFileDialog.AnyFile) dialog.setAcceptMode(QFileDialog.AcceptSave) dialog.setDefaultSuffix("avi") dialog.setDirectory(os.getcwd()) dialog.setNameFilter(self.tr("Video Files [.avi] (*.avi)")) if dialog.exec_(): output_file_name = dialog.selectedFiles()[0] correlate(self.clip_filename, self.audio_filename, output_file_name)
def correlate_image(image_COMs, char_COMs, intensity_weight): # # The user specifies a weighting on Intensity, where: # # intensity_weight == 0: 100% positional # intensity_weight == 1: 100% intensity # # "Naturally", the intensity_weight is 0.5, meaning equal weighting between position and intesity. # # The constant weighting factors A, B, and C above can be calculated as follows: # # A = 0.5 * (1-intensity_weight) # B = 0.5 * (1-intensity_weight) # C = intensity_weight # # Since 0 <= intensity_weight <= 1, we know that (A + B + C) == 1 # # (COM_x, COM_y) as a whole represent one quantity, and Intensity represents a second quantity, so the # "natural" weightings for these are such that each quantity accounts for half of the net effect. Since # A and B both modify the positional quantity, these give equal weighting to position and intensity when # intensity_weight == 0.5 WEIGHTS = { 'x': 0.5 * (1 - intensity_weight), 'y': 0.5 * (1 - intensity_weight), 'i': intensity_weight } # A division naturally contributes N elements to the Grand Vector Comparison, where N = 3*(division^2) # For a [3,5,7] split, the "natural" weights would be [27, 75, 147], equivalent to [10.9%, 30.1%, 59.0%]. # For equal weightings, we multiply by the inverses: [9.22, 3.32, 1.69], or, equivalently, [0.65, 0.23, 0.12] # # Finally, we may not want equal weighting, so we can finally multiply these items by another weighting. For example, # we could use [1.3, 1.2, 1.1] if we wanted division 3 to have higher value than 5, which has higher value than 7 # # It is completely unnecessary for the elements to add to 1, so don't worry about that DIVISION_WEIGHTS = { 3: (0.647805394 * 2.25), 5: (0.233209942 * 1.50), 7: (0.118984664 * 1.00) } sliceCharCodes = {} for sliceCoord, sliceCOM in sorted(image_COMs.items()): slice_vector, weights = [], [] for numSlices, COM in sorted(sliceCOM.items()): for coord, subCOM in sorted(COM.items()): slice_vector.extend( (subCOM['COM_x'], subCOM['COM_y'], subCOM['intensity'])) weights.extend((DIVISION_WEIGHTS[numSlices] * WEIGHTS['x'], DIVISION_WEIGHTS[numSlices] * WEIGHTS['y'], DIVISION_WEIGHTS[numSlices] * WEIGHTS['i'])) charCorrelations = {} for charCode, charCOM in char_COMs.items(): char_vector = [] for numSlices, COM in sorted(charCOM.items()): for coord, subCOM in sorted(COM.items()): char_vector.extend((subCOM['COM_x'], subCOM['COM_y'], subCOM['intensity'])) charCorrelations[charCode] = correlate.correlate( slice_vector, char_vector, weights) sliceCharCodes[sliceCoord] = sorted([(charCorrelations[k], k) for k in charCorrelations])[-1] return sliceCharCodes
# gr.Eeff(el, ns_cal[ii], set_id_cal[ii], step, dir_cal[ii], wrt_file) # gr.Eeff(el, ns_val[ii], set_id_val[ii], step, dir_val[ii], wrt_file) gr.FIP(el, ns_cal[ii], set_id_cal[ii], step, dir_cal[ii], wrt_file) gr.FIP(el, ns_val[ii], set_id_val[ii], step, dir_val[ii], wrt_file) """Compute GSH coefficients to create microstructure function in real and fourier space""" for ii in xrange(len(set_id_cal)): get_M.get_M(el, H, ns_cal[ii], set_id_cal[ii], step, wrt_file) for ii in xrange(len(set_id_val)): get_M.get_M(el, H, ns_val[ii], set_id_val[ii], step, wrt_file) """Compute the periodic statistics for the microstructures""" for ii in xrange(len(set_id_cal)): corr.correlate(el, ns_cal[ii], H, set_id_cal[ii], step, wrt_file) for ii in xrange(len(set_id_val)): corr.correlate(el, ns_val[ii], H, set_id_val[ii], step, wrt_file) """Perform PCA on correlations""" pca = gns.new_space(el, ns_cal, H, set_id_cal, step, n_pc_tot, wrt_file) """transform statistics to reduced dimensionality space""" f = h5py.File("sve_reduced.hdf5", 'w') f.close() for ii in xrange(len(set_id_cal)): tf.transform(el, ns_cal[ii], H, set_id_cal[ii], step, pca, wrt_file) for ii in xrange(len(set_id_val)): tf.transform(el, ns_val[ii], H, set_id_val[ii], step, pca, wrt_file)
f = h5py.File("spatial.hdf5", 'w') f.close() """Gather data from vtk files""" for ii in xrange(len(set_id_cal)): vtk.read_euler(strt_cal[ii], ns_cal[ii], set_id_cal[ii], dir_cal[ii], 1) for ii in xrange(len(set_id_val)): vtk.read_euler(strt_val[ii], ns_val[ii], set_id_val[ii], dir_val[ii], 1) """Compute GSH coefficients to create microstructure function in real and fourier space""" for ii in xrange(len(set_id_cal)): get_M.get_M(ns_cal[ii], set_id_cal[ii]) for ii in xrange(len(set_id_val)): get_M.get_M(ns_val[ii], set_id_val[ii]) """Compute the periodic statistics for the microstructures""" for ii in xrange(len(set_id_cal)): corr.correlate(ns_cal[ii], set_id_cal[ii]) for ii in xrange(len(set_id_val)): corr.correlate(ns_val[ii], set_id_val[ii]) """Perform PCA on correlations""" pca = gns.new_space(ns_cal, set_id_cal) """transform statistics to reduced dimensionality space""" f = h5py.File("spatial_reduced.hdf5", 'w') f.close() for ii in xrange(len(set_id_cal)): tf.transform(ns_cal[ii], set_id_cal[ii], pca) for ii in xrange(len(set_id_val)): tf.transform(ns_val[ii], set_id_val[ii], pca)
if __name__ == "__main__": parser = argparse.ArgumentParser(description="Sync better Audio files.") parser.add_argument('-c', '--clip', help="Video or audio with low quality", metavar='low_quality_clip', type=str, default='', required=False) parser.add_argument('-a', '--audio', help="Audio with better quality", metavar='high_quality_audio', type=str, default='', required=False) parser.add_argument('-o', '--out', help="Filename for the mixed output", metavar='output_filename', type=str, default='', required=False) args = parser.parse_args() if not args.clip and not args.audio and not args.out: create_interface() elif args.clip and args.audio and args.out: correlate(args.clip, args.audio, args.out) else: parser.print_help()
def main( vis, closure_tels='DE601;DE602;DE603;DE604;DE605;FR606;SE607;UK608;DE609;PL610;PL611;PL612;IE613', npix=128, fov_arcsec=6., zbl=0., prior_fwhm_arcsec=1., doplots=False, imfile='myim', remove_tels='', niter=300, cfloor=0.001, conv_criteria=0.0001, use_bs=False, use_first=False): ## for the generic pipeline, force the data types npix = int(npix) fov_arcsec = float(fov_arcsec) zbl = float(zbl) prior_fwhm_arcsec = float(prior_fwhm_arcsec) niter = int(niter) cfloor = float(cfloor) conv_criteria = float(conv_criteria) vis1 = vis.rstrip( '/') + '.ms' ## so the next line will work if it doesn't end in ms fitsout = vis1.replace('.MS', '.fits').replace('.ms', '.fits') ## remove any telescopes from the default list r_tels = remove_tels.split(';') for r_tel in r_tels: closure_tels = closure_tels.replace(r_tel, '') tmp_tel = closure_tels.split(';') tel = [t for t in tmp_tel if t != ''] ## starting from a measurement set, get a smaller ms with just the list of antennas ## use taql to get a list of telescopes ss = 'taql \'select NAME from %s/ANTENNA\' > closure_txt' % (vis) os.system(ss) os.system('grep -v select closure_txt > closure_which') idxtel = np.loadtxt('closure_which', dtype='S') atel = np.unique(np.ravel(tel)) notfound = [] aidx = np.array([], dtype='int') for a in atel: found_this = False for i in range(len(idxtel)): if a == idxtel[i][:len(a)]: aidx = np.append(aidx, i) found_this = True if not found_this: notfound.append(a) if len(notfound): print 'The following telescopes were not found:', notfound os.system('rm closure_*') aidx_s = np.sort(aidx) if os.path.exists('cl_temp.ms'): os.system('rm -fr cl_temp.ms') ss = 'taql \'select from %s where ' % vis for i in range(len(aidx_s)): for j in range(i + 1, len(aidx_s)): ss += ('ANTENNA1==%d and ANTENNA2==%d' % (aidx_s[i], aidx_s[j])) if i == len(aidx_s) - 2 and j == len(aidx_s) - 1: ss += (' giving cl_temp.ms as plain\'') else: ss += (' or ') print 'Selecting smaller MS cl_temp.ms, this will take about 4s/Gb:' print ss os.system(ss) ## check if weights are appropriate print 'Checking if weights are sensible values, updating them if not' ss = "taql 'select WEIGHT_SPECTRUM from cl_temp.ms limit 1' > weight_check.txt" os.system(ss) with open('weight_check.txt', 'r') as f: lines = f.readlines() f.close() first_weight = np.float(lines[3].lstrip('[').split(',')[0]) if first_weight < 1e-9: ss = "taql 'update cl_temp.ms set WEIGHT_SPECTRUM=WEIGHT_SPECTRUM*1e11'" os.system(ss) ## find the zero baseline amplitudes if it isn't set if zbl == 0: print 'Calculating the zero baseline flux (mean of amplitudes on DE601 -- DE605' ## use baseline DE601 -- DE605 (shortest international to interational baseline) ## njj - failing that, use the first two try: de601_idx = aidx[[ i for i, val in enumerate(tel) if 'DE601' in val ]][0] de605_idx = aidx[[ i for i, val in enumerate(tel) if 'DE605' in val ]][0] except: print '...Not present. Using %s %s instead' % (tel[0], tel[1]) de601_idx, de605_idx = aidx[0], aidx[1] ss = "taql 'select means(gaggr(abs(DATA)),0,1) from cl_temp.ms' where ANTENNA1==%s and ANTENNA2=%s > amp_check.txt" % ( de601_idx, de605_idx) os.system(ss) with open('amp_check.txt', 'r') as f: lines = f.readlines() f.close() amps = np.asarray(lines[2].lstrip('[').rstrip(']\n').split(', '), dtype=float) zbl = np.max(amps) os.system('rm *_check.txt') ## convert vis to uv-fits if os.path.isfile(fitsout): os.system('rm ' + fitsout) ss = 'ms2uvfits in=cl_temp.ms out=%s writesyscal=F' % (fitsout) os.system(ss) ## observe the uv-fits print 'Reading in the observation to the EHT imager.' obs = eh.obsdata.load_uvfits(fitsout) if doplots: ## make plots print 'Plotting u-v coverage and amplitude vs. uv-distance' obs.plotall('u', 'v', conj=True, show=False, export_pdf=fitsout.replace('fits', 'u-v.pdf')) ## u-v coverage obs.plotall('uvdist', 'amp', show=False, export_pdf=fitsout.replace('fits', 'uvdist-amp.pdf')) ## etc ## the eht imager's default units is microarcseconds fov = fov_arcsec * 1e6 * RADPERUAS ## set up the gaussian prior if use_first: if not os.path.isfile('./first_2008.simple.npy'): os.system( 'wget http://www.jb.man.ac.uk/~njj/first_2008.simple.npy') import math, pyrap from pyrap import tables table = pyrap.tables.table('cl_temp.ms/FIELD', readonly=True) ra = math.degrees( float(table.getcol('PHASE_DIR')[0][0][0]) % (2 * math.pi)) dec = math.degrees(float(table.getcol('PHASE_DIR')[0][0][-1])) table.close() first = np.load('first_2008.simple.npy') MAXARCMIN, RADASEC = 2.0, 206265.0 corrfirst = correlate.correlate(np.array([[ ra, dec, ]]), 0, 1, first, 0, 1, MAXARCMIN / 60.0) if not len(corrfirst): # no FIRST, fall back to default prior_fwhm = prior_fwhm_arcsec * 1e6 * RADPERUAS gaussparams = (prior_fwhm, prior_fwhm, 0.0) emptyprior = eh.image.make_square(obs, npix, fov) gaussprior = emptyprior.add_gauss(zbl, gaussparams) print 'Prior image: circular Gaussian %f arcsec' % prior_fwhm * RADASEC else: fov_arcsec = max(10.0, 2.2 * corrfirst[:, 2].max() * 3600.0) fov = fov_arcsec * 1e6 * RADPERUAS emptyprior = eh.image.make_square(obs, npix, fov) for i in corrfirst: this = first[int(i[1])] zbl = this[ 3] * 4.0 / 1000.0 # multiply first flux by 4 - bit rough gaussparams = (np.deg2rad((max(7.0,this[4]))/3600.),\ np.deg2rad((max(5.0,this[5]))/3600.),\ np.deg2rad(this[6]),\ np.deg2rad(this[0]-ra),np.deg2rad(this[1]-dec)) print 'Prior: adding %.1fmJy Gaussian %.2f*%.2f asec, PA %.1f, at (%.3f,%.3f)asec' % \ (zbl*1000.,gaussparams[0]*RADASEC,gaussparams[1]*RADASEC,\ np.rad2deg(gaussparams[2]),\ gaussparams[3]*RADASEC,gaussparams[4]*RADASEC) else: prior_fwhm = prior_fwhm_arcsec * 1e6 * RADPERUAS gaussparams = (prior_fwhm, prior_fwhm, 0.0) emptyprior = eh.image.make_square(obs, npix, fov) gaussprior = emptyprior.add_gauss(zbl, gaussparams) gaussprior.display() ## the dirty beam print 'Calculating the dirty beam.' dbeam = obs.dirtybeam(npix, fov) dbeam.save_fits(fitsout.replace('fits', 'dirty_beam.fits')) ## the clean beam print 'Calculating the clean beam.' cbeam = obs.cleanbeam(npix, fov) cbeam.save_fits(fitsout.replace('fits', 'clean_beam.fits')) # dirty image print 'Calculating the dirty image.' dimage = obs.dirtyimage(npix, fov) dimage.save_fits(fitsout.replace('fits', 'dirty_image.fits')) ## beam parameters (in radians) beamparams = obs.fit_beam() print 'The beam is ' + str(beamparams[0] * 206265.) + ' by ' + str( beamparams[1] * 206265.) + ' arcsec.' ## maximum resolution (in radians) res = obs.res() print 'Maximum resolution is ' + str(res * 206265.) if use_bs: print 'Using bispectrum mode rather than cphase and camp separately.' ## try using bispectrum # bs_out = eh.imager_func( obs, gaussprior, gaussprior, zbl, d1='bs', maxit=100) bs_out = eh.imager_func(obs, gaussprior, gaussprior, zbl, d1='bs', s1='gs', alpha_d1=50, clipfloor=0.001, maxit=300, stop=0.0001) bs_outblur = bs_out.blur_gauss(beamparams, 0.5) # bs_out1 = eh.imager_func( obs, bs_outblur, bs_outblur, zbl, d1='bs', s1='gs', alpha_d1=50, maxit=300, stop=0.0001 ) bs_out1 = eh.imager_func(obs, bs_outblur, bs_outblur, zbl, d1='bs', s1='gs', alpha_d1=50, clipfloor=0.001, maxit=300, stop=0.0001) bs_outblur1 = bs_out1.blur_gauss((res, res, 0), 0.5) bs_finalout = bs_out1.blur_gauss(beamparams, 0.5) ## save to fits bs_out1.save_fits('./bs_' + imfile + 'im.fits') bs_finalout.save_fits('./bs_' + imfile + 'im_blur.fits') else: print 'Using closure phase and closure amplitude separately.' ## using d1 = closure phase and d2 = closure amplitude # out = eh.imager_func( obs, gaussprior, gaussprior, zbl, d1='cphase', maxit=niter, stop=conv_criteria ) out = eh.imager_func(obs, gaussprior, gaussprior, zbl, d1='cphase', d2='camp', s1='gs', s2='gs', alpha_d1=50, alpha_d2=50, clipfloor=cfloor, maxit=niter, stop=conv_criteria) outblur = out.blur_gauss(beamparams, 0.5) # out1 = eh.imager_func( obs, outblur, outblur, zbl, d1='cphase', maxit=niter, stop=conv_criteria ) out1 = eh.imager_func(obs, outblur, outblur, zbl, d1='cphase', d2='camp', s1='gs', s2='gs', alpha_d1=50, alpha_d2=50, clipfloor=cfloor, maxit=niter, stop=conv_criteria) out1blur = out1.blur_gauss((res, res, 0), 0.5) finalout = out1.blur_gauss(beamparams, 0.5) ## save to fits out.save_fits('./' + imfile + '_0_im.fits') outblur.save_fits('./' + imfile + '_0_im_blur.fits') out1.save_fits('./' + imfile + 'im.fits') finalout.save_fits('./' + imfile + 'im_blur.fits') print 'done.'