Skip to content

Commit 37fc06c

Browse files
authored
Merge pull request #5994 from kayahans/qmca_image
Nexus: add qmca feature to save plots to image files (png, pdf, svg, eps)
2 parents d464117 + 235a4d1 commit 37fc06c

3 files changed

Lines changed: 200 additions & 30 deletions

File tree

docs/analyzing.rst

Lines changed: 96 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -53,8 +53,11 @@ typing ``qmca`` at the command line with no other inputs (also try
5353
-e EQUILIBRATION, --equilibration=EQUILIBRATION
5454
Equilibration length in blocks (default=auto).
5555
-a, --average Average over files in each series (default=False).
56+
--save_average Save averaged data into scalar.dat files.
57+
(default=False).
5658
-w WEIGHTS, --weights=WEIGHTS
5759
List of weights for averaging (default=None).
60+
-j JOIN, --join=JOIN Join data for a range of series (default=None).
5861
-b, --reblock (pending) Use reblocking to calculate statistics
5962
(default=False).
6063
-p, --plot Plot quantities vs. series (default=False).
@@ -69,7 +72,14 @@ typing ``qmca`` at the command line with no other inputs (also try
6972
--noac Alias for --noautocorr (default=False).
7073
--sac Show autocorrelation of sample data (default=False).
7174
--sv Show variance of sample data (default=False).
72-
-i, --image (pending) Save image files (default=False).
75+
--sort Display output data sorted alphabetically by file name
76+
(default=False).
77+
-i, --image Save plot files (default prefix: qmca_plot).
78+
--image-prefix=BASENAME
79+
Save plot files with basename prefix
80+
(default=None).
81+
--image-format=FORMAT Output format for saved plots: png, pdf, svg, or eps
82+
(default=png).
7383
-r, --report (pending) Write a report (default=False).
7484
-s, --show_options Print user provided options (default=False).
7585
-x, --examples Print examples and exit (default=False).
@@ -81,6 +91,17 @@ typing ``qmca`` at the command line with no other inputs (also try
8191
Show number of samples needed to maintain error bar on
8292
larger system: desired particle number first, current
8393
particle number second (default=none)
94+
--fp=PRECISION Sets the floating point precision of displayed
95+
statistical results. Must be a floating point format
96+
string such as 16.8f, 8.6e, or similar (default=none).
97+
--nowarn Suppress warning messages (default=False).
98+
--average_all Average over all files, ignoring differences in path
99+
(default=False).
100+
--twist_info=TWIST_INFO
101+
Use twist weights in twist_info.dat files or not.
102+
Options: "use", "ignore", "require". "use" means use
103+
when present, "ignore" means do not use, "require"
104+
means must be used (default=use).
84105

85106
.. _qmca-mean-error:
86107

@@ -566,29 +587,59 @@ command line with no other input. This following is a current list:
566587
::
567588

568589
Abbreviations and full names for quantities:
569-
ar = AcceptRatio
570-
bc = BlockCPU
571-
bw = BlockWeight
572-
ce = CorrectedEnergy
573-
de = DiffEff
574-
e = LocalEnergy
575-
ee = ElecElec
576-
eff = Efficiency
577-
ii = IonIon
578-
k = Kinetic
579-
kc = KEcorr
580-
l = LocalECP
581-
le2 = LocalEnergy_sq
582-
mpc = MPC
583-
n = NonLocalECP
584-
nw = NumOfWalkers
585-
p = LocalPotential
586-
sw = AvgSentWalkers
587-
te = TrialEnergy
588-
ts = TotalSamples
589-
tt = TotalTime
590-
v = Variance
591-
w = Weight
590+
ar = AcceptRatio
591+
bc = BlockCPU
592+
bw = BlockWeight
593+
ce = CorrectedEnergy
594+
de = DiffEff
595+
de_AB = dLocEne_0_1
596+
dee_AB = dElecElec_0_1
597+
dii_AB = dIonIon_0_1
598+
e = LocalEnergy
599+
e_A = LocEne_0
600+
e_B = LocEne_1
601+
ee = ElecElec
602+
ee_A = ElecElec_0
603+
ee_B = ElecElec_1
604+
ee_m = ElecElec_m
605+
ee_p = ElecElec_p
606+
eff = Efficiency
607+
ei_A = ElecIon_0
608+
ei_AB = dElecIon_0_1
609+
ei_B = ElecIon_1
610+
el = EnergyEstim__nume_real
611+
fl = Flux
612+
fl_A = Flux_0
613+
fl_AB = dFlux_0_1
614+
fl_B = Flux_1
615+
ii = IonIon
616+
ii_A = IonIon_0
617+
ii_B = IonIon_1
618+
k = Kinetic
619+
k_A = Kinetic_0
620+
k_AB = dKinetic_0_1
621+
k_B = Kinetic_1
622+
k_m = Kinetic_m
623+
k_p = Kinetic_p
624+
kc = KEcorr
625+
l = LocalECP
626+
le2 = LocalEnergy_sq
627+
mpc = MPC
628+
n = NonLocalECP
629+
nw = NumOfWalkers
630+
p = LocalPotential
631+
p_A = LocPot_0
632+
p_AB = dLocPot_0_1
633+
p_B = LocPot_1
634+
p_p = LocalPotential_pure
635+
sw = AvgSentWalkers
636+
te = TrialEnergy
637+
ts = TotalSamples
638+
tt = TotalTime
639+
v = Variance
640+
w = Weight
641+
wp_A = wpsi_0
642+
wp_B = wpsi_1
592643

593644
See the output overview for ``scalar.dat``
594645
(:ref:`scalardat-file`) and ``dmc.dat``
@@ -877,6 +928,27 @@ Plotting a trace of the local energy:
877928

878929
>qmca -t -q e *scalar*
879930

931+
Saving a trace plot of the local energy to a PNG file (default prefix: qmca_plot):
932+
933+
::
934+
935+
>qmca -t -q e --image *scalar*
936+
937+
Saving a trace plot of the local energy to a PNG file with a custom prefix:
938+
939+
::
940+
941+
>qmca -t -q e --image-prefix myrun *scalar*
942+
943+
Saving a trace plot to a PDF file for publication (vector format):
944+
945+
::
946+
947+
>qmca -t -q e --image-format pdf --image-prefix myrun *scalar*
948+
949+
PDF, SVG, and EPS output are resolution-independent and better suited to journal
950+
submission than PNG.
951+
880952
Applying an equilibration cutoff to VMC data (series 0):
881953

882954
::

nexus/nexus/bin/qmca

Lines changed: 48 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -835,6 +835,12 @@ QMCA examples:
835835
qmca -pa -q e -e 10 qmc.g*.s*.scalar.dat
836836
qmca -ta -q e -e 10 qmc.g*.s*.scalar.dat
837837
838+
saving plots to files (-i/--image uses prefix qmca_plot; --image-prefix sets a custom prefix)
839+
qmca -t -q e -i qmc.s*.scalar.dat
840+
qmca -ta -q ev --image-prefix myrun qmc.g*.s*.scalar.dat
841+
saving plots to PDF files (--image-format pdf for vector output suited to publication)
842+
qmca -t -q e --image-format pdf --image-prefix myrun qmc.s*.scalar.dat
843+
838844
multiple quantities and prefixes can be used for plots as well
839845
'''
840846
self.log(examples)
@@ -931,9 +937,17 @@ QMCA examples:
931937
action='store_true',default=False,
932938
help='Display output data sorted alphabetically by file name (default=%default).'
933939
)
934-
parser.add_option('-i','--image',dest='image',
940+
parser.add_option('-i','--image', dest='save_image',
935941
action='store_true',default=False,
936-
help='(pending) Save image files (default=%default).'
942+
help='Save plot files (default prefix: qmca_plot).'
943+
)
944+
parser.add_option('--image-prefix',dest='image_prefix',
945+
default='None',metavar='BASENAME',
946+
help='Save plot files with basename prefix (default=%default).'
947+
)
948+
parser.add_option('--image-format',dest='image_format',
949+
default='png',metavar='FORMAT',
950+
help='Output format for saved plots: png, pdf, svg, or eps (default=%default).'
937951
)
938952
parser.add_option('-r','--report',dest='report',
939953
action='store_true',default=False,
@@ -979,7 +993,7 @@ QMCA examples:
979993
help='Use twist weights in twist_info.dat files or not. Options: "use", "ignore", "require". "use" means use when present, "ignore" means do not use, "require" means must be used (default=%default).'
980994
)
981995

982-
unsupported = set('histogram image reblock report'.split())
996+
unsupported = set('histogram reblock report'.split())
983997

984998
units = obj(hartree='Ha',ha='Ha',ev='eV',rydberg='Ry',ry='Ry',kelvin='K',kj_mol='kJ_mol',joules='J')
985999

@@ -1064,9 +1078,19 @@ QMCA examples:
10641078
#end if
10651079
opt.join = j
10661080
#end if
1067-
if opt.image=='None':
1068-
opt.image=None
1081+
if opt.image_prefix!='None':
1082+
opt.image = opt.image_prefix
1083+
elif opt.save_image:
1084+
opt.image = 'qmca_plot'
1085+
else:
1086+
opt.image = None
1087+
#end if
1088+
image_formats = ('png','pdf','svg','eps')
1089+
fmt = opt.image_format.lower().lstrip('.')
1090+
if fmt not in image_formats:
1091+
self.error('image_format option is invalid\nimage_format must be one of: {}\nyou provided: {}'.format(image_formats,opt.image_format))
10691092
#end if
1093+
opt.image_format = fmt
10701094
twist_info_options = ('use','ignore','require')
10711095
if opt.twist_info not in twist_info_options:
10721096
self.error('twist_info option is invalid\ntwist_info must be one of: {}\nyou provided: {}'.format(twist_info_options,opt.twist_info))
@@ -1449,7 +1473,9 @@ QMCA examples:
14491473
tracestyle = color+line
14501474
histstyle = color+line
14511475
iplot,itrace,ihist = list(range(3))
1476+
mode_names = ['series','trace','histogram']
14521477
modes = [opt.plot,opt.trace,opt.histogram]
1478+
save_plots = opt.image is not None
14531479
quantities = []
14541480
for q in opt.quantities:
14551481
all_present = True
@@ -1562,12 +1588,28 @@ QMCA examples:
15621588
ax.legend(loc=2,bbox_to_anchor=(1.0,1.0),borderaxespad=0.)
15631589
#end if
15641590
#end if
1591+
if save_plots:
1592+
safe_prefix = prefix.replace('/','_').replace(os.sep,'_')
1593+
fmt = opt.image_format
1594+
fname = '{0}_{1}_{2}_{3}.{4}'.format(
1595+
opt.image,safe_prefix,quantity,mode_names[mode],fmt)
1596+
save_kwargs = dict(format=fmt,bbox_inches='tight')
1597+
if fmt=='png':
1598+
save_kwargs['dpi'] = 150
1599+
#end if
1600+
fig.savefig(fname,**save_kwargs)
1601+
if self.verbose:
1602+
self.log('saved plot to {0}'.format(fname),n=1)
1603+
#end if
1604+
plt.close(fig)
1605+
#end if
15651606
#end if
15661607
#end for
15671608
#end if
15681609
#end for
15691610
#end for
1570-
plt.show()
1611+
if not save_plots:
1612+
plt.show()
15711613
#end if
15721614
#end def analyze_data
15731615
#end class QMCA

nexus/nexus/tests/test_qmca.py

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -266,3 +266,59 @@ def test_weighted_twist_average():
266266
os.chdir(cwd)
267267
#end def test_weighted_twist_average
268268

269+
270+
271+
def test_save_plot_pdf():
272+
273+
os.environ['MPLBACKEND'] = 'Agg'
274+
try:
275+
import matplotlib.pyplot # noqa: F401
276+
except (ImportError, RuntimeError):
277+
pytest.skip('matplotlib not available')
278+
279+
cwd = Path.cwd()
280+
os.chdir(QA_PATHS["vmc"])
281+
282+
prefix = 'testplot_pdf'
283+
plot_file = f'{prefix}_vmc_LocalEnergy_trace.pdf'
284+
if os.path.exists(plot_file):
285+
os.remove(plot_file)
286+
287+
command = (f'MPLBACKEND=Agg {sys.executable} {QMCA_EXE} -t -q e -e 5 '
288+
f'--image-prefix {prefix} --image-format pdf --nowarn *scalar*')
289+
out,err,rc = execute(command)
290+
assert rc==0
291+
assert os.path.exists(plot_file)
292+
293+
os.remove(plot_file)
294+
os.chdir(cwd)
295+
#end def test_save_plot_pdf
296+
297+
298+
299+
def test_save_plot_png_default():
300+
301+
os.environ['MPLBACKEND'] = 'Agg'
302+
try:
303+
import matplotlib.pyplot # noqa: F401
304+
except (ImportError, RuntimeError):
305+
pytest.skip('matplotlib not available')
306+
307+
cwd = Path.cwd()
308+
os.chdir(QA_PATHS["vmc"])
309+
310+
prefix = 'testplot_png'
311+
plot_file = f'{prefix}_vmc_LocalEnergy_trace.png'
312+
if os.path.exists(plot_file):
313+
os.remove(plot_file)
314+
315+
command = (f'MPLBACKEND=Agg {sys.executable} {QMCA_EXE} -t -q e -e 5 '
316+
f'--image-prefix {prefix} --image --nowarn *scalar*')
317+
out,err,rc = execute(command)
318+
assert rc==0
319+
assert os.path.exists(plot_file)
320+
321+
os.remove(plot_file)
322+
os.chdir(cwd)
323+
#end def test_save_plot_png_default
324+

0 commit comments

Comments
 (0)