def plot_debug_grad(debug, tag, fold_exp, trg):
plt.close("all")
# f = plt.figure(figsize=(15, 10.8), dpi=300)
nbr_rows = int(len(debug["grad_sup"][0])/2)
f, axs = plt.subplots(nbr_rows, 2, sharex=True, sharey=False,
figsize=(15, 12.8), dpi=300)
if trg == "sup":
grad = np.array(debug["grad_sup"])
elif trg == "hint":
grad = np.array(debug["grad_hint"])
print grad.shape, trg
j = 0
for i in range(0, nbr_rows*2, 2):
w_vl = grad[:, i]
b_vl = grad[:, i+1]
axs[j, 0].plot(w_vl, label=trg)
axs[j, 0].set_title("w"+str(j))
axs[j, 1].plot(b_vl, label=trg)
axs[j, 1].set_title("b"+str(j))
axs[j, 0].grid(True)
axs[j, 1].grid(True)
j += 1
f.suptitle("Grad sup/hint:" + tag, fontsize=8)
plt.legend()
f.savefig(fold_exp+"/grad_" + trg + ".png", bbox_inches='tight')
plt.close("all")
del f
python类close()的实例源码
def plot_debug_ratio_grad(debug, fold_exp, r="h/s"):
plt.close("all")
# f = plt.figure(figsize=(15, 10.8), dpi=300)
nbr_rows = int(len(debug["grad_sup"][0])/2)
f, axs = plt.subplots(nbr_rows, 2, sharex=True, sharey=False,
figsize=(15, 12.8), dpi=300)
grads = np.array(debug["grad_sup"])
gradh = np.array(debug["grad_hint"])
if gradh.size != grads.size:
print "Can't calculate the ratio. It looks like you divided the " +\
"hint batch..."
return 0
print gradh.shape, grads.shape
j = 0
for i in range(0, nbr_rows*2, 2):
w_vls = grads[:, i]
b_vls = grads[:, i+1]
w_vl_h = gradh[:, i]
b_vlh = gradh[:, i+1]
if r == "h/s":
ratio_w = np.divide(w_vl_h, w_vls)
ratio_b = np.divide(b_vlh, b_vls)
elif r == "s/h":
ratio_w = np.divide(w_vls, w_vl_h)
ratio_b = np.divide(b_vls, b_vlh)
else:
raise ValueError("Either h/s or s/h.")
axs[j, 0].plot(ratio_w, label=r)
axs[j, 0].set_title("w"+str(j))
axs[j, 1].plot(ratio_b, label=r)
axs[j, 1].set_title("b"+str(j))
axs[j, 0].grid(True)
axs[j, 1].grid(True)
j += 1
f.suptitle("Ratio gradient: " + r, fontsize=8)
plt.legend()
f.savefig(fold_exp+"/ratio_grad_" + r.replace("/", "-") + ".png",
bbox_inches='tight')
plt.close("all")
del f
def plot_representations(X, y, title):
"""Plot distributions and thier labels."""
x_min, x_max = np.min(X, 0), np.max(X, 0)
X = (X - x_min) / (x_max - x_min)
f = plt.figure(figsize=(15, 10.8), dpi=300)
# ax = plt.subplot(111)
for i in range(X.shape[0]):
plt.text(X[i, 0], X[i, 1], str(y[i]),
color=plt.cm.Set1(y[i] / 10.),
fontdict={'weight': 'bold', 'size': 9})
# if hasattr(offsetbox, 'AnnotationBbox'):
# # only print thumbnails with matplotlib > 1.0
# shown_images = np.array([[1., 1.]]) # just something big
# for i in range(digits.data.shape[0]):
# dist = np.sum((X[i] - shown_images) ** 2, 1)
# if np.min(dist) < 4e-3:
# # don't show points that are too close
# continue
# shown_images = np.r_[shown_images, [X[i]]]
# imagebox = offsetbox.AnnotationBbox(
# offsetbox.OffsetImage(digits.images[i], cmap=plt.cm.gray_r),
# X[i])
# ax.add_artist(imagebox)
plt.xticks([]), plt.yticks([])
if title is not None:
plt.title(title)
return f
def test_plot_network1():
filename = abspath(join(testdir, 'plot_network1.png'))
if isfile(filename):
os.remove(filename)
inp_file = join(ex_datadir,'Net6.inp')
wn = wntr.network.WaterNetworkModel(inp_file)
plt.figure()
wntr.graphics.plot_network(wn)
plt.savefig(filename, format='png')
plt.close()
assert_true(isfile(filename))
def test_plot_tank_curve1():
filename = abspath(join(testdir, 'plot_pump_curve1.png'))
if isfile(filename):
os.remove(filename)
inp_file = join(ex_datadir,'Net3.inp')
wn = wntr.network.WaterNetworkModel(inp_file)
pump = wn.get_link('10')
plt.figure()
wntr.graphics.plot_pump_curve(pump)
plt.savefig(filename, format='png')
plt.close()
assert_true(isfile(filename))
def generate_setup_template_modify(outputfile='./tdose_setup_template_modify.txt',clobber=False,verbose=True):
"""
Generate setup text file template for modifying data cubes
--- INPUT ---
outputfile The name of the output which will contain the TDOSE setup template
clobber Overwrite files if they exist
verbose Toggle verbosity
--- EXAMPLE OF USE ---
import tdose_utilities as tu
filename = './tdose_setup_template_modify_new.txt'
tu.generate_setup_template_modify(outputfile=filename,clobber=True)
setup = tu.load_setup(setupfile=filename)
"""
if verbose: print ' --- tdose_utilities.generate_setup_template_modify() --- '
#------------------------------------------------------------------------------------------------------
if os.path.isfile(outputfile) & (clobber == False):
sys.exit(' ---> Outputfile already exists and clobber=False ')
else:
if verbose: print ' - Will store setup template in '+outputfile
if os.path.isfile(outputfile) & (clobber == True):
if verbose: print ' - Output already exists but clobber=True so overwriting it '
setuptemplate = """
#---------------------------------------------START OF TDOSE MODIFY SETUP---------------------------------------------
#
# Template for TDOSE (http://github.com/kasperschmidt/TDOSE) setup file for modifyinf data cubes
# Generated with tdose_utilities.generate_setup_template_modify() on %s
# Cube modifications are run independent of tdose.perform_extraction() with tdose.modify_cube()
#
# - - - - - - - - - - - - - - - - - - - - - - - - - MODIFYING CUBE - - - - - - - - - - - - - - - - - - - - - - - - - -
data_cube /path/datacube.fits # Path and name of fits file containing data cube to modify
cube_extension DATA_DCBGC # Name or number of fits extension containing data cube
source_model_cube /path/tdose_source_modelcube.fits # Path and name of fits file containing source model cube
source_extension DATA_DCBGC # Name or number of fits extension containing source model cube
modyified_cube tdose_modified_datacube # Name extension of file containing modified data cube.
modify_sources_list [1,2,5] # List of IDs of sources to remove from data cube using source model cube.
# For long list of IDs provide path and name of file containing IDs (only)
sources_action remove # Indicate how to modify the data cube. Chose between:
# 'remove' Sources in modify_sources_list are removed from data cube
# 'keep' All sources except the sources in modify_sources_list are removed from data cube
#----------------------------------------------END OF TDOSE MODIFY SETUP----------------------------------------------
""" % (tu.get_now_string())
fout = open(outputfile,'w')
fout.write(setuptemplate)
fout.close()
# = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = =
def galfit_model_ds9region(models,regionfileextension='ds9region',regcolor='red',clobber=False,verbose=True):
"""
Generate DS9 region file to indicate GALFIT components
--- INPUT ---
model List of GALFIT models to generate region files for
regionfileextension Extension for naming the DS9 region file
regcolor Color of regions to draw
clobber Overwrite existing file?
verbose Toggle verbosity
--- EXAMPLE OF USE ---
models = glob.glob('/path/to/models/model*.fits')
tu.galfit_model_ds9region(models,clobber=False)
"""
Nmodels = len(models)
if verbose: print ' - Generating DS9 region files for '+str(Nmodels)+' GALFIT models provided '
for model in models:
modelhdr = pyfits.open(model)[2].header
comkeys = []
regionfile = model.replace('.fits','_'+regionfileextension+'.reg')
for key in modelhdr.keys():
if 'COMP_' in key:
comkeys.append(key)
if os.path.isfile(regionfile):
if not clobber:
sys.exit(' ---> File already exists and clobber = False')
fout = open(regionfile,'w')
fout.write("# Region file format: DS9 version 4.1 \nimage\n")
for comp in comkeys:
compNo = comp.split('OMP_')[-1]
if not modelhdr[comp] == 'sky':
XC, XCERR = tu.galfit_getheadervalue(compNo,'XC',modelhdr)
YC, YCERR = tu.galfit_getheadervalue(compNo,'YC',modelhdr)
regstr = '# text(%s,%s) color=%s font="times 20 bold roman" text={%s} \n' % (XC,YC,regcolor,compNo)
fout.write(regstr)
fout.close()
if verbose: print ' - Saved region file to \n '+regionfile
# = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = =
def plot_fig(values, title, x_str, y_str, path, best_iter, std_vals=None):
"""Plot some values.
Input:
values: list or numpy.ndarray of values to plot (y)
title: string; the title of the plot.
x_str: string; the name of the x axis.
y_str: string; the name of the y axis.
path: string; path where to save the figure.
best_iter: integer. The epoch of the best iteration.
std_val: List or numpy.ndarray of standad deviation values that
corresponds to each value in 'values'.
"""
floating = 6
prec = "%." + str(floating) + "f"
if best_iter >= 0:
if isinstance(values, list):
if best_iter >= len(values):
best_iter = -1
if isinstance(values, np.ndarray):
if best_iter >= np.size:
best_iter = -1
v = str(prec % np.float(values[best_iter]))
else:
v = str(prec % np.float(values[-1]))
best_iter = -1
if best_iter == -1:
best_iter = len(values)
fig = plt.figure()
plt.plot(
values,
label="lower val: " + v + " at " + str(best_iter) + " " +
x_str)
plt.xlabel(x_str)
plt.ylabel(y_str)
plt.title(title, fontsize=8)
plt.legend(loc='upper right', fancybox=True, shadow=True, prop={'size': 8})
plt.grid(True)
fig.savefig(path, bbox_inches='tight')
plt.close('all')
del fig
def test_abu_chart(self):
from nugridpy import utils,ppn,data_plot
import matplotlib
matplotlib.use('agg')
import matplotlib.pylab as mpy
import os
# Perform tests within temporary directory
with TemporaryDirectory() as tdir:
# wget the data for a ppn run from the CADC VOspace
n = 3
for cycle in range(0,n):
cycle_str = str(cycle).zfill(2)
os.system("wget -q --content-disposition --directory '" + tdir + "' "
+ "'http://www.canfar.phys.uvic.ca/vospace/synctrans?TARGET="\
+ "vos%3A%2F%2Fcadc.nrc.ca%21vospace%2Fnugrid%2Fdata%2Fprojects%2Fppn%2Fexamples%2F"\
+ "ppn_Hburn_simple%2Fiso_massf000" + cycle_str + ".DAT&DIRECTION=pullFromVoSpace&PROTOCOL"\
+ "=ivo%3A%2F%2Fivoa.net%2Fvospace%2Fcore%23httpget'")
# test_data_dir should point to the correct location of a set of abundances data file
#nugrid_dir= os.path.dirname(os.path.dirname(ppn.__file__))
#NuPPN_dir= nugrid_dir + "/NuPPN"
#test_data_dir= NuPPN_dir + "/examples/ppn_C13_pocket/master_results"
p=ppn.abu_vector(tdir) # TODO: this function fails to raise an exception if path is not found!
mp=p.get('mod')
if len(mp) == 0:
raise IOError("Cannot locate a set of abundance data files")
sparse=10
cycles=mp[:1000:sparse]
form_str='%6.1F'
form_str1='%4.3F'
i=0
for cyc in cycles:
T9 = p.get('t9',fname=cyc)
Rho = p.get('rho',fname=cyc)
mod = p.get('mod',fname=cyc)
# time= p.get('agej',fname=cyc)*utils.constants.one_year
time= p.get('agej',fname=cyc)
mpy.close(i);mpy.figure(i);i += 1
p.abu_chart(cyc,mass_range=[0,41],plotaxis=[-1,22,-1,22],lbound=(-6,0),show=False)
mpy.title(str(mod)+' t='+form_str%time+'yr $T_9$='+form_str1%T9+' $\\rho$='+str(Rho))
png_file='abu_chart_'+str(cyc).zfill(len(str(max(mp))))+'.png'
mpy.savefig(png_file)
self.assertTrue(os.path.exists(png_file))
os.remove(png_file)
def plot_profiles_to_file(annot, pntr, ups=200, smooth_param=50):
pp = PdfPages(options.save_path + 'Figures/individual_signals.pdf')
clrs_ = ['red', 'blue', 'black', 'orange', 'magenta', 'cyan']
vec_sense = {}
vec_antisense = {}
# for qq in tq(range(annot.shape[0])):
for qq in tq(range(100)):
chname = annot['chr'].iloc[qq]
if annot['strand'].iloc[qq] == '+':
start = annot['start'].iloc[qq] - ups
stop = annot['end'].iloc[qq]
for key in pntr.keys():
vec_sense[key] = pntr[key][0].get_nparray(chname, start, stop - 1)
vec_antisense[key] = pntr[key][1].get_nparray(chname, start, stop - 1)
xran = np.arange(start, stop)
else:
start = annot['start'].iloc[qq]
stop = annot['end'].iloc[qq] + ups
for key in pntr.keys():
vec_sense[key] = np.flipud(pntr[key][1].get_nparray(chname, start, stop))
vec_antisense[key] = np.flipud(pntr[key][0].get_nparray(chname, start, stop))
xran = np.arange(stop, start, -1)
ax = {}
fig = pl.figure()
pl.title(annot['name'].iloc[qq])
for i, key in enumerate(pntr.keys()):
sm_vec_se = sm.smooth(vec_sense[key], smooth_param)[(smooth_param - 1):-(smooth_param - 1)]
sm_vec_as = sm.smooth(vec_antisense[key], smooth_param)[(smooth_param - 1):-(smooth_param - 1)]
ax[key] = pl.subplot(len(pntr), 1, i+1)
ax[key].plot(xran, vec_sense[key], label=key, color=clrs_[i], alpha=0.5)
ax[key].plot(xran, -vec_antisense[key], color=clrs_[i], alpha=0.5)
ax[key].plot(xran, sm_vec_se, color=clrs_[i], linewidth=2)
ax[key].plot(xran, -sm_vec_as, color=clrs_[i], linewidth=2)
ax[key].legend(loc='upper center', bbox_to_anchor=(0.5, 1.05), fontsize=6, ncol=1)
pp.savefig()
pl.close()
pp.close()
for pn in pntr.values():
pn[0].close()
pn[1].close()
def plot_2d(params_dir):
model_dirs = [name for name in os.listdir(params_dir)
if os.path.isdir(os.path.join(params_dir, name))]
if len(model_dirs) == 0:
model_dirs = [params_dir]
colors = plt.get_cmap('plasma')
plt.figure(figsize=(20, 10))
ax = plt.subplot(111)
ax.set_xlabel('Learning Rate')
ax.set_ylabel('Error rate')
i = 0
for model_dir in model_dirs:
model_df = pd.DataFrame()
for param_path in glob.glob(os.path.join(params_dir,
model_dir) + '/*.h5'):
param = dd.io.load(param_path)
gd = {'learning rate': param['hyperparameters']['learning_rate'],
'momentum': param['hyperparameters']['momentum'],
'dropout': param['hyperparameters']['dropout'],
'val. objective': param['best_epoch']['validate_objective']}
model_df = model_df.append(pd.DataFrame(gd, index=[0]),
ignore_index=True)
if i != len(model_dirs) - 1:
ax.scatter(model_df['learning rate'],
model_df['val. objective'],
s=128,
marker=(i+3, 0),
edgecolor='black',
linewidth=model_df['dropout'],
label=model_dir,
c=model_df['momentum'],
cmap=colors)
else:
im = ax.scatter(model_df['learning rate'],
model_df['val. objective'],
s=128,
marker=(i+3, 0),
edgecolor='black',
linewidth=model_df['dropout'],
label=model_dir,
c=model_df['momentum'],
cmap=colors)
i += 1
plt.colorbar(im, label='Momentum')
plt.legend()
plt.show()
plt.savefig('{}.eps'.format(os.path.join(IMAGES_DIRECTORY, 'params2d')), format='eps', dpi=1000)
plt.close()
def plotPolicy(self, policy, prefix):
plt.clf()
for idx in xrange(len(policy)):
i, j = self.env.getStateXY(idx)
dx = 0
dy = 0
if policy[idx] == 0: # up
dy = 0.35
elif policy[idx] == 1: #right
dx = 0.35
elif policy[idx] == 2: #down
dy = -0.35
elif policy[idx] == 3: #left
dx = -0.35
elif self.matrixMDP[i][j] != -1 and policy[idx] == 4: # termination
circle = plt.Circle(
(j + 0.5, self.numRows - i + 0.5 - 1), 0.025, color='k')
plt.gca().add_artist(circle)
if self.matrixMDP[i][j] != -1:
plt.arrow(j + 0.5, self.numRows - i + 0.5 - 1, dx, dy,
head_width=0.05, head_length=0.05, fc='k', ec='k')
else:
plt.gca().add_patch(
patches.Rectangle(
(j, self.numRows - i - 1), # (x,y)
1.0, # width
1.0, # height
facecolor = "gray"
)
)
plt.xlim([0, self.numCols])
plt.ylim([0, self.numRows])
for i in xrange(self.numCols):
plt.axvline(i, color='k', linestyle=':')
plt.axvline(self.numCols, color='k', linestyle=':')
for j in xrange(self.numRows):
plt.axhline(j, color='k', linestyle=':')
plt.axhline(self.numRows, color='k', linestyle=':')
plt.savefig(self.outputPath + prefix + 'policy.png')
plt.close()
def test_write_monitoring_report2():# with test results and graphics (encoded and linked)
filename1 = abspath(join(testdir, 'test_write_monitoring_report2_linked_graphics.html'))
filename2 = abspath(join(testdir, 'test_write_monitoring_report2_encoded_graphics.html'))
graphics_filename = abspath(join(testdir, 'custom_graphic.png'))
if isfile(filename1):
os.remove(filename1)
if isfile(filename2):
os.remove(filename2)
if isfile(graphics_filename):
os.remove(graphics_filename)
pecos.logger.initialize()
logger = logging.getLogger('pecos')
pm = pecos.monitoring.PerformanceMonitoring()
periods = 5
index = pd.date_range('1/1/2016', periods=periods, freq='H')
data = np.array([[1,2,3], [4,5,6], [7,8,9], [10,11,12], [13,14,15]])
df = pd.DataFrame(data=data, index=index, columns=['A', 'B', 'C'])
tfilter = pd.Series(data = (df.index < index[3]), index = df.index)
pm.add_dataframe(df, 'test', True)
pm.add_time_filter(tfilter)
pm.check_range([0,7]) # 2 test failures
filename_root = abspath(join(testdir, 'monitoring_report_graphic'))
test_results_graphics = pecos.graphics.plot_test_results(filename_root, pm)
plt.figure()
plt.plot([1, 2, 3],[1, 2, 3])
plt.savefig(graphics_filename, format='png')
plt.close()
custom_graphics = [graphics_filename]
logger.warning('Add a note')
pecos.io.write_monitoring_report(filename1, pm, test_results_graphics, custom_graphics, encode=False)
assert_true(isfile(filename1))
pecos.io.write_monitoring_report(filename2, pm, test_results_graphics, custom_graphics, encode=True)
assert_true(isfile(filename2))
def gaussian_twoD_testing():
""" Implement and check the estimator for a 2D gaussian fit. """
data = np.empty((121,1))
amplitude=np.random.normal(3e5,1e5)
center_x=91+np.random.normal(0,0.8)
center_y=14+np.random.normal(0,0.8)
sigma_x=np.random.normal(0.7,0.2)
sigma_y=np.random.normal(0.7,0.2)
offset=0
x = np.linspace(90,92,11)
y = np.linspace(13,15,12)
xx, yy = np.meshgrid(x, y)
axes=(xx.flatten(), yy.flatten())
theta_here=10./360.*(2*np.pi)
# data=qudi_fitting.twoD_gaussian_function((xx,yy),*(amplitude,center_x,center_y,sigma_x,sigma_y,theta_here,offset))
gmod,params = qudi_fitting.make_twoDgaussian_model()
data = gmod.eval(x=axes, amplitude=amplitude, center_x=center_x,
center_y=center_y, sigma_x=sigma_x, sigma_y=sigma_y,
theta=theta_here, offset=offset)
data += 50000*np.random.random_sample(np.shape(data))
gmod, params = qudi_fitting.make_twoDgaussian_model()
para=Parameters()
# para.add('theta',vary=False)
# para.add('center_x',expr='0.5*center_y')
# para.add('sigma_x',min=0.2*((92.-90.)/11.), max= 10*(x[-1]-y[0]) )
# para.add('sigma_y',min=0.2*((15.-13.)/12.), max= 10*(y[-1]-y[0]))
# para.add('center_x',value=40,min=50,max=100)
result = qudi_fitting.make_twoDgaussian_fit(xy_axes=axes, data=data)#,add_parameters=para)
#
# FIXME: What does "Tolerance seems to be too small." mean in message?
# print(result.message)
plt.close('all')
fig, ax = plt.subplots(1, 1)
ax.hold(True)
ax.imshow(result.data.reshape(len(y),len(x)),
cmap=plt.cm.jet, origin='bottom', extent=(x.min(), x.max(),
y.min(), y.max()),interpolation="nearest")
ax.contour(x, y, result.best_fit.reshape(len(y),len(x)), 8
, colors='w')
plt.show()
# plt.close('all')
print(result.fit_report())
# print('Message:',result.message)
def plot_fig(values, title, x_str, y_str, path, best_iter, std_vals=None):
"""Plot some values.
Input:
values: list or numpy.ndarray of values to plot (y)
title: string; the title of the plot.
x_str: string; the name of the x axis.
y_str: string; the name of the y axis.
path: string; path where to save the figure.
best_iter: integer. The epoch of the best iteration.
std_val: List or numpy.ndarray of standad deviation values that
corresponds to each value in 'values'.
"""
floating = 6
prec = "%." + str(floating) + "f"
if best_iter >= 0:
if isinstance(values, list):
if best_iter >= len(values):
best_iter = -1
if isinstance(values, np.ndarray):
if best_iter >= np.size:
best_iter = -1
v = str(prec % np.float(values[best_iter]))
else:
v = str(prec % np.float(values[-1]))
best_iter = -1
if best_iter == -1:
best_iter = len(values)
fig = plt.figure()
plt.plot(
values,
label="lower val: " + v + " at " + str(best_iter) + " " +
x_str)
plt.xlabel(x_str)
plt.ylabel(y_str)
plt.title(title, fontsize=8)
plt.legend(loc='upper right', fancybox=True, shadow=True, prop={'size': 8})
plt.grid(True)
fig.savefig(path, bbox_inches='tight')
plt.close('all')
del fig