-
Notifications
You must be signed in to change notification settings - Fork 11
Expand file tree
/
Copy pathpyhist.cpp
More file actions
141 lines (115 loc) · 2.84 KB
/
pyhist.cpp
File metadata and controls
141 lines (115 loc) · 2.84 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
#include "../utils/utils.hh"
#include "pyhist.hh"
#include <cstring>
#include <cstdlib>
//pyhist.hh.
PyHist::PyHist(const char* namei, enum pipe_type pipe){
pipe_state = pipe;
verify_dir("FIGS/");
chdir("FIGS/");
pypipe = popen("python", "w");
assrt(pypipe != NULL);
chdir("..");
assrt(strlen(namei)<25);
strcpy(name, namei);
state = 0;
for(int i=0; i < MAX_NUM_PYPLT_CMDS; i++)
cmd[i] = new char[MAX_CMD_PYPLT_LEN];
cmdnum = 0;
savedata = 0;
sprintf(cmdstr, "import matplotlib as mpl\n");
issue_command(cmdstr);
sprintf(cmdstr, "mpl.rcParams['font.size'] = 18\n");
issue_command(cmdstr);
/*
* Changes backend if PLTOFF.
*/
if(pipe_state == PLTOFF){
sprintf(cmdstr, "mpl.use('PDF', warn = True)\n");
issue_command(cmdstr);
}
sprintf(cmdstr, "from matplotlib import pyplot as plt\n");
issue_command(cmdstr);
sprintf(cmdstr, "from numpy import genfromtxt\n");
issue_command(cmdstr);
sprintf(cmdstr, "fig = plt.figure(1)\n");
issue_command(cmdstr);
sprintf(cmdstr, "ax = plt.subplot(111)\n");
issue_command(cmdstr);
}
PyHist::~PyHist(){
sprintf(cmdstr, "plt.close()\n");
issue_command(cmdstr);
int rval = pclose(pypipe);
assrt(rval != -1);
//pipe must be closed (as above) before data is removed.
if(savedata==0){
sprintf(cmdstr, "rm FIGS/%s_*", name);
system(cmdstr);
}
for(int i=0; i < MAX_NUM_PYPLT_CMDS; i++)
delete[] cmd[i];
}
void PyHist::issue_command(const char *cstr){
assrt(cmdnum < MAX_NUM_PYPLT_CMDS);
int len = strlen(cstr);
assrt(len < MAX_CMD_PYPLT_LEN-1);
assrt(cstr[len-1] == '\n');
strcpy(cmd[cmdnum], cstr);
fprintf(pypipe, "%s", cmd[cmdnum++]);
}
void PyHist::prep_data(double *x, int n){
char fname[200];
sprintf(fname, "FIGS/%s_x.dat", name);
array_out(x, n, 1, fname);
sprintf(cmdstr,
"x = genfromtxt('%s_x.dat', dtype='float')\n", name);
issue_command(cmdstr);
}
void PyHist::hist(double *x, int n){
assrt(state == 0);
state = 1;
prep_data(x, n);
}
void PyHist::bins(int b){
assrt(state == 1);
state = 2;
sprintf(cmdstr,
"h, b, p = plt.hist(x, bins = %d)\n", b);
issue_command(cmdstr);
}
void PyHist::title(const char* s){
assrt(state == 2);
sprintf(cmdstr, "plt.title('%s', fontsize=20) \n", s);
issue_command(cmdstr);
}
void PyHist::pycmd(const char* s){
assrt(state = 2);
int len = strlen(s);
assrt(len < MAX_CMD_PYPLT_LEN-1);
assrt(s[len-1] == '\n');
issue_command(s);
}
void PyHist::show(){
assrt(state == 2);
state = 3;
if(pipe_state == PLTON){
sprintf(cmdstr, "plt.show() \n");
issue_command(cmdstr);
}
else{
sprintf(cmdstr, "plt.savefig('%s.pdf') \n",name);
issue_command(cmdstr);
}
}
void PyHist::savescript(){
assrt(state==3);
savedata = 1;
char fname[200];
sprintf(fname, "FIGS/%s.py", name);
FILE *fp = fopen(fname, "w");
fprintf(fp, "#! /usr/bin/env python\n");
for(int i=0; i < cmdnum; i++)
fprintf(fp, "%s", cmd[i]);
fclose(fp);
}