def create_tensor(file1,mean_array):
video_1 = cv2.VideoCapture(file1)
# use cv to get frame number is not correct
len_1 = int(video_1.get(cv2.cv.CV_CAP_PROP_FRAME_COUNT))
tensor_1 = np.zeros([3,len_1,112,112])
count = 0
ret = True
while True:
ret, frame_1 = video_1.read()
if frame_1 is not None:
tensor_1[:,count,:,:] = np.swapaxes(cv2.resize(cropImg(frame_1),(112,112)),0,2) - mean_array
count = count+1
print count
else:
break
pdb.set_trace()
tensor = tensor_1[:,:count,:,:]
return tensor
python类use()的实例源码
def _get_data(url):
"""Helper function to get data over http or from a local file"""
if url.startswith('http://'):
# Try Python 2, use Python 3 on exception
try:
resp = urllib.urlopen(url)
encoding = resp.headers.dict.get('content-encoding', 'plain')
except AttributeError:
resp = urllib.request.urlopen(url)
encoding = resp.headers.get('content-encoding', 'plain')
data = resp.read()
if encoding == 'plain':
pass
elif encoding == 'gzip':
data = StringIO(data)
data = gzip.GzipFile(fileobj=data).read()
else:
raise RuntimeError('unknown encoding')
else:
with open(url, 'r') as fid:
data = fid.read()
fid.close()
return data
def SExtractorCat2fits(sextractorfiles,stringcols=[1],header=73,verbose=True):
"""
Converting an ascii catalog with columns defined in header in the SExtractor format, i.e. one column
name per row preceeded by a "#" and a column numner, and followed by a description (or any ascii file
with the given setup) to a fits binary table
--- INPUT ---
sextractorfiles List of ascii files to convert to fits
stringcols Columns to use a string format for (all other columns will be set to double float)
header Header containing the column names of the catalogs following the "SExtractor notation"
verbose Toggle verbosity
--- EXAMPLE OF USE ---
import glob
import tdose_utilities as tu
catalogs = glob.glob('/Volumes/DATABCKUP2/MUSE-Wide/catalogs_photometry/catalog_photometry_candels-cdfs-*.cat')
tu.SExtractorCat2fits(catalogs,stringcols=[1],header=73,verbose=True)
"""
for sexcat_ascii in sextractorfiles:
asciiinfo = open(sexcat_ascii,'r')
photcols = []
for line in asciiinfo:
if line.startswith('#'):
colname = line.split()[2]
photcols.append(colname)
photfmt = ['D']*len(photcols)
for stringcol in stringcols:
photfmt[stringcol] = 'A60'
sexcat_fits = tu.ascii2fits(sexcat_ascii,asciinames=photcols,skip_header=header,fitsformat=photfmt,verbose=verbose)
# = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = =
def galfit_getcentralcoordinate(modelfile,coordorigin=1,verbose=True):
"""
Return the central coordinates of a GALFIT model extracted using the reference image WCS and the FITSECT keyword
--- INPUT ---
modelfile Path and name to GALFIT model fits file to retrieve central coordinates for
coordorigin Origin of coordinates in reference image to use when converting pixels to degrees (skycoord)
verbose Toggle verbosity
--- EXAMPLE OF USE ---
fileG = '/Volumes/DATABCKUP2/TDOSEextractions/models_cutouts/model8685multicomponent/model_acs_814w_candels-cdfs-02_cut_v1.0_id8685_cutout7p0x7p0arcsec.fits' # Gauss components
fileS = '/Volumes/DATABCKUP2/TDOSEextractions/models_cutouts/model8685multicomponent/model_acs_814w_candels-cdfs-02_cut_v1.0_id9262_cutout2p0x2p0arcsec.fits' # Sersic components
xpix, ypix, ra_model, dec_model = tu.galfit_getcentralcoordinate(fileG,coordorigin=1)
"""
if verbose: print ' - Will extract central coordinates from '+modelfile
refimg_hdr = pyfits.open(modelfile)[1].header
model_hdr = pyfits.open(modelfile)[2].header
imgwcs = wcs.WCS(tu.strip_header(refimg_hdr.copy()))
fit_region = model_hdr['FITSECT']
cutrange_low_x = int(float(fit_region.split(':')[0].split('[')[-1]))
cutrange_low_y = int(float(fit_region.split(',')[-1].split(':')[0]))
xsize = model_hdr['NAXIS1']
ysize = model_hdr['NAXIS2']
xpix = cutrange_low_x + int(xsize/2.)
ypix = cutrange_low_y + int(ysize/2.)
if verbose: print ' - Converting pixel position to coordinates using a pixel origin='+str(coordorigin)
skycoord = wcs.utils.pixel_to_skycoord(xpix,ypix,imgwcs,origin=coordorigin)
ra_model = skycoord.ra.value
dec_model = skycoord.dec.value
return xpix,ypix,ra_model,dec_model
# = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = =
def actor_loss(self):
if self.config.mode == 'discrete':
log_prob = tf.reduce_sum(tf.log(self.a_prob) * tf.one_hot(self.action_input, self.action_dim, dtype=tf.float32),
axis=1, keep_dims=True)
# use entropy to encourage exploration
exp_v = log_prob * self.TD_loss
entropy = -tf.reduce_sum(self.a_prob * tf.log(self.a_prob), axis=1, keep_dims=True) # encourage exploration
exp_v = self.config.ENTROPY_BETA * entropy + exp_v
return tf.reduce_mean(-exp_v) # ????????log_prb????????????????????TD_loss
elif self.config.mode == 'continuous':
log_prob = self.action_normal_dist.log_prob(self.action_input)
exp_v = log_prob * self.TD_loss
# use entropy to encourage exploration
exp_v = self.config.ENTROPY_BETA * self.action_normal_dist.entropy() + exp_v
return tf.reduce_mean(-exp_v)
def v(self):
with tf.variable_scope('critic'):
w_i = tf.random_uniform_initializer(0., 0.1)
b_i = tf.zeros_initializer()
with tf.variable_scope('dense1'):
dense1 = dense(self.state_input, 100, [100], w_i, activation=tf.nn.relu6)
with tf.variable_scope('dense2'):
dense2 = dense(dense1, 1, [1], w_i, b_i, activation=None)
return dense2
# Note: We need 2 return value here: mu & sigma. So it is not suitable to use lazy_property.
def do_all():
raise RuntimeError("all is broken, don't use")
accuracy_against_sempre()
recall()
correct_function()
different_training_sets()
learning()
def _deferred_imports():
# Importing PyQt4 and matplotlib may take a long time--more than a second
# worst case--but neither one is needed at import time (or possibly ever).
# By deferring the import until required (at creation of a plot), the cost
# is much less apparent.
try:
from PyQt4 import QtCore, QtGui
import matplotlib
matplotlib.use('Qt4Agg')
from matplotlib import pyplot, mlab, pylab
from matplotlib.backends.backend_agg import FigureCanvasAgg
import numpy
from bulkio.bulkioInterfaces import BULKIO__POA
# Rebind the function to do nothing in future calls
def _deferred_imports():
pass
globals().update(locals())
except ImportError, e:
import platform
if 'el5' in platform.release() and 'PyQt4' in str(e):
raise RuntimeError("matplotlib-based plots are not available by default on Red Hat Enterprise Linux 5 (missing PyQt4 dependency)")
else:
raise RuntimeError("Missing required package for sandbox plots: '%s'" % e)
def _getSampleRate(self, sri):
if sri.xdelta > 0.0:
# Round sample rate to an integral value to account for the fact
# that there is typically some rounding error in the xdelta value.
return round(1.0 / sri.xdelta)
else:
# Bad SRI xdelta, use normalized value.
return 1.0
def _formatData(self, data, sri):
# Image data cannot be complex; just use the real component.
if sri.mode:
return [x.real for x in data]
else:
return data
def add_arguments(parser):
arg = parser.add_argument
group = parser.add_mutually_exclusive_group()
group.add_argument('--real-cdr3', action='store_true', default=False,
help='In addition to barcode, group sequences by real CDR3 (detected with regex).')
group.add_argument('--pseudo-cdr3', nargs='?', default=None,
type=slice_arg, const=slice(-80, -60), metavar='START:END',
help='In addition to barcode, group sequences by pseudo CDR3. '
'If START:END is omitted, use -80:-60.')
arg('--groups-output', metavar='FILE', default=None,
help='Write tab-separated table with groups to FILE')
arg('--plot-sizes', metavar='FILE', default=None,
help='Plot group sizes to FILE (.png or .pdf)')
arg('--limit', default=None, type=int, metavar='N',
help='Limit processing to the first N reads')
arg('--trim-g', action='store_true', default=False,
help="Trim 'G' nucleotides at 5' end")
arg('--minimum-length', '-l', type=int, default=0,
help='Minimum sequence length')
arg('--barcode-length', '-b', type=int, default=12,
help="Length of barcode. Positive for 5' barcode, negative for 3' barcode. Default: %(default)s")
arg('fastx', metavar='FASTA/FASTQ',
help='FASTA or FASTQ file (can be gzip-compressed) with sequences')
def diffD1D2(Flags):
# To check if the difference between D1 and D2 amplifies downstream
# First decide which model to use
delay = 1
if Flags == "Allsym":
(d1,d2,fsi,ti,ta,stn,gpi,ipctx,A,B,params) = calcRates(Flags,delay)
# D = Direct pathway, ID = Indirect pathway, HD = Hyperdirect pathway
# Reducing a full recurrent matrix leads to postive and negative contributions in ID and HD instead of pure just positive contributions
D = params['gpid1']
print "Direct",D
de1 = 1. + (params['d1d1']*params['fsifsi']) - (params['stnti']*params['stnstn'])
Ex1 = (params['stnti']*params['d1d1']*params['fsifsi']*params['gpistn'])/de1
Ex2 = (params['gpid1']*params['stnstn']*params['d1ta']*params['fsifsi'])/de1
IDpos = params['stnti']*params['tid2']*(params['d1ta']*params['gpid1']*params['tistn']+ Ex1 + Ex2)
print "IDpos",IDpos
Ex3 = (params['d1ta']+params['d1ta']*params['tata']+((params['stnta']*params['fsiti']*params['d1fsi'])/de1))
IDneg = params['gpid1']+params['gpiti']*params['tid2']+params['gpid1']*params['stnstn']*params['tid2']*Ex3
print "IDneg",IDneg
HDpos = (params['jstnctx']*params['gpid1']*params['stnstn']*params['fsiti']*params['d1fsi'])/de1
print "HDpos",HDpos
Ex4 = params['jstnctx']*params['d1d1']*params['fsifsi']*params['stnti']*params['gpistn']
Ex5 = params['jstnctx']*params['gpid1']*params['stnstn']*params['d1ta']*params['fsifsi']
HDneg = (Ex4 + Ex5)/de1
print "HDneg",HDneg
d1fix = np.mean(d1[:-10])
d2fix = np.mean(d2[:-10])
DelMSN = d1fix - d2fix
DelGpi = (D*d1fix) + ((IDpos - IDneg)*d2fix)
return (DelMSN,DelGpi)
def diffD1D2(Flags):
# To check if the difference between D1 and D2 amplifies downstream
# First decide which model to use
delay = 1
if Flags == "Allsym":
(d1,d2,fsi,ti,ta,stn,gpi,ipctx,A,B,params) = calcRates(Flags,delay)
# D = Direct pathway, ID = Indirect pathway, HD = Hyperdirect pathway
# Reducing a full recurrent matrix leads to postive and negative contributions in ID and HD instead of pure just positive contributions
D = params['gpid1']
print "Direct",D
de1 = 1. + (params['d1d1']*params['fsifsi']) - (params['stnti']*params['stnstn'])
Ex1 = (params['stnti']*params['d1d1']*params['fsifsi']*params['gpistn'])/de1
Ex2 = (params['gpid1']*params['stnstn']*params['d1ta']*params['fsifsi'])/de1
IDpos = params['stnti']*params['tid2']*(params['d1ta']*params['gpid1']*params['tistn']+ Ex1 + Ex2)
print "IDpos",IDpos
Ex3 = (params['d1ta']+params['d1ta']*params['tata']+((params['stnta']*params['fsiti']*params['d1fsi'])/de1))
IDneg = params['gpid1']+params['gpiti']*params['tid2']+params['gpid1']*params['stnstn']*params['tid2']*Ex3
print "IDneg",IDneg
HDpos = (params['jstnctx']*params['gpid1']*params['stnstn']*params['fsiti']*params['d1fsi'])/de1
print "HDpos",HDpos
Ex4 = params['jstnctx']*params['d1d1']*params['fsifsi']*params['stnti']*params['gpistn']
Ex5 = params['jstnctx']*params['gpid1']*params['stnstn']*params['d1ta']*params['fsifsi']
HDneg = (Ex4 + Ex5)/de1
print "HDneg",HDneg
d1fix = np.mean(d1[:-10])
d2fix = np.mean(d2[:-10])
DelMSN = d1fix - d2fix
DelGpi = (D*d1fix) + ((IDpos - IDneg)*d2fix)
return (DelMSN,DelGpi)
def diffD1D2(Flags):
# To check if the difference between D1 and D2 amplifies downstream
# First decide which model to use
delay = 1
if Flags == "Allsym":
(d1,d2,fsi,ti,ta,stn,gpi,ipctx,A,B,params) = calcRates(Flags,delay)
# D = Direct pathway, ID = Indirect pathway, HD = Hyperdirect pathway
# Reducing a full recurrent matrix leads to postive and negative contributions in ID and HD instead of pure just positive contributions
D = params['gpid1']
print "Direct",D
de1 = 1. + (params['d1d1']*params['fsifsi']) - (params['stnti']*params['stnstn'])
Ex1 = (params['stnti']*params['d1d1']*params['fsifsi']*params['gpistn'])/de1
Ex2 = (params['gpid1']*params['stnstn']*params['d1ta']*params['fsifsi'])/de1
IDpos = params['stnti']*params['tid2']*(params['d1ta']*params['gpid1']*params['tistn']+ Ex1 + Ex2)
print "IDpos",IDpos
Ex3 = (params['d1ta']+params['d1ta']*params['tata']+((params['stnta']*params['fsiti']*params['d1fsi'])/de1))
IDneg = params['gpid1']+params['gpiti']*params['tid2']+params['gpid1']*params['stnstn']*params['tid2']*Ex3
print "IDneg",IDneg
HDpos = (params['jstnctx']*params['gpid1']*params['stnstn']*params['fsiti']*params['d1fsi'])/de1
print "HDpos",HDpos
Ex4 = params['jstnctx']*params['d1d1']*params['fsifsi']*params['stnti']*params['gpistn']
Ex5 = params['jstnctx']*params['gpid1']*params['stnstn']*params['d1ta']*params['fsifsi']
HDneg = (Ex4 + Ex5)/de1
print "HDneg",HDneg
d1fix = np.mean(d1[:-10])
d2fix = np.mean(d2[:-10])
DelMSN = d1fix - d2fix
DelGpi = (D*d1fix) + ((IDpos - IDneg)*d2fix)
return (DelMSN,DelGpi)
def diffD1D2(Flags):
# To check if the difference between D1 and D2 amplifies downstream
# First decide which model to use
delay = 1
if Flags == "Allsym":
(d1,d2,fsi,ti,ta,stn,gpi,ipctx,A,B,params) = calcRates(Flags,delay)
# D = Direct pathway, ID = Indirect pathway, HD = Hyperdirect pathway
# Reducing a full recurrent matrix leads to postive and negative contributions in ID and HD instead of pure just positive contributions
D = params['gpid1']
print "Direct",D
de1 = 1. + (params['d1d1']*params['fsifsi']) - (params['stnti']*params['stnstn'])
Ex1 = (params['stnti']*params['d1d1']*params['fsifsi']*params['gpistn'])/de1
Ex2 = (params['gpid1']*params['stnstn']*params['d1ta']*params['fsifsi'])/de1
IDpos = params['stnti']*params['tid2']*(params['d1ta']*params['gpid1']*params['tistn']+ Ex1 + Ex2)
print "IDpos",IDpos
Ex3 = (params['d1ta']+params['d1ta']*params['tata']+((params['stnta']*params['fsiti']*params['d1fsi'])/de1))
IDneg = params['gpid1']+params['gpiti']*params['tid2']+params['gpid1']*params['stnstn']*params['tid2']*Ex3
print "IDneg",IDneg
HDpos = (params['jstnctx']*params['gpid1']*params['stnstn']*params['fsiti']*params['d1fsi'])/de1
print "HDpos",HDpos
Ex4 = params['jstnctx']*params['d1d1']*params['fsifsi']*params['stnti']*params['gpistn']
Ex5 = params['jstnctx']*params['gpid1']*params['stnstn']*params['d1ta']*params['fsifsi']
HDneg = (Ex4 + Ex5)/de1
print "HDneg",HDneg
d1fix = np.mean(d1[:-10])
d2fix = np.mean(d2[:-10])
DelMSN = d1fix - d2fix
DelGpi = (D*d1fix) + ((IDpos - IDneg)*d2fix)
return (DelMSN,DelGpi)
def _savePlot(self, data, filename):
import matplotlib
matplotlib.use('Agg')
import matplotlib.pyplot as plt
plt.plot(data)
plt.savefig(filename)
def url_for_figure(self, s: str):
"""
Override it to use flask.url_for
:param s:
:return:
"""
return "/fig/" + s
def url_for_snippet(self, label: str) -> str:
"""
Returns url for snippet by label.
Override this method to use Flask's url_for
:param label:
:return:
"""
return "/snippet/"+label
def search(node, critereon):
"""
Traverse the Theano graph starting at `node` and return a list of all nodes
which match the `critereon` function. When optimizing a cost function, you
can use this to get a list of all of the trainable params in the graph, like
so:
`lib.search(cost, lambda x: hasattr(x, "param"))`
or
`lib.search(cost, lambda x: hasattr(x, "param") and x.param==True)`
"""
def _search(node, critereon, visited):
if node in visited:
return []
visited.add(node)
results = []
if isinstance(node, T.Apply):
for inp in node.inputs:
results += _search(inp, critereon, visited)
else: # Variable node
if critereon(node):
results.append(node)
if node.owner is not None:
results += _search(node.owner, critereon, visited)
return results
return _search(node, critereon, set())
def __init__(self, name='expression', display=True):
super(Expression, self).__init__()
self.name = name
self.display = display
if display:
import matplotlib.pyplot as plt
plt.ion()
else:
import matplotlib
matplotlib.use('Agg')
import matplotlib.pyplot as plt
self.fig, (self.ax1, self.ax2) = plt.subplots(nrows=2, ncols=1)