Skip to content

Commit 3e9ac7e

Browse files
committed
Add the initial tools
1 parent b992d9d commit 3e9ac7e

15 files changed

Lines changed: 436 additions & 0 deletions

tools/cwrap.dir/concore.init1

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
[0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0]

tools/cwrap.dir/concore.init2

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
[0.0, 0.0, 0.0]

tools/cwrap.dir/concore.name1

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
u

tools/cwrap.dir/concore.name2

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
ym

tools/cwrap.dir/concore.yuyu

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
yuyu

tools/cwrap.py

Lines changed: 124 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,124 @@
1+
#CW
2+
import concore
3+
import requests
4+
import time
5+
from ast import literal_eval
6+
import os
7+
8+
#time.sleep(7)
9+
timeout_max = 20
10+
concore.delay = 0.02
11+
12+
try:
13+
apikey=open(concore.inpath+'1/concore.apikey',newline=None).readline().rstrip()
14+
except:
15+
try:
16+
#perhaps this should be removed for security
17+
apikey=open('./concore.apikey',newline=None).readline().rstrip()
18+
except:
19+
apikey = ''
20+
21+
try:
22+
yuyu=open(concore.inpath+'1/concore.yuyu',newline=None).readline().rstrip()
23+
except:
24+
try:
25+
yuyu=open('./concore.yuyu',newline=None).readline().rstrip()
26+
except:
27+
yuyu = 'yuyu'
28+
29+
try:
30+
name1=open(concore.inpath+'1/concore.name1',newline=None).readline().rstrip()
31+
except:
32+
try:
33+
name1=open('./concore.name1',newline=None).readline().rstrip()
34+
except:
35+
name1 = 'u'
36+
37+
try:
38+
name2=open(concore.inpath+'1/concore.name2',newline=None).readline().rstrip()
39+
except:
40+
try:
41+
name2=open('./concore.name2',newline=None).readline().rstrip()
42+
except:
43+
name2 = 'ym'
44+
45+
try:
46+
init_simtime_u = open(concore.inpath+'1/concore.init1',newline=None).readline().rstrip()
47+
except:
48+
try:
49+
init_simtime_u = open('./concore.init1',newline=None).readline().rstrip()
50+
except:
51+
init_simtime_u = "[0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0]"
52+
53+
try:
54+
init_simtime_ym = open(concore.inpath+'1/concore.init2',newline=None).readline().rstrip()
55+
except:
56+
try:
57+
init_simtime_ym = open('./concore.init2',newline=None).readline().rstrip()
58+
except:
59+
init_simtime_ym = "[0.0, 0.0, 0.0]"
60+
61+
print(apikey)
62+
print(yuyu)
63+
print(name1+'='+init_simtime_u)
64+
print(name2+'='+init_simtime_ym)
65+
66+
while not os.path.exists(concore.inpath+'1/'+name1):
67+
time.sleep(concore.delay)
68+
69+
70+
#Nsim = 150
71+
concore.default_maxtime(150)
72+
#init_simtime_u = "[0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0]"
73+
#init_simtime_ym = "[0.0, 0.0, 0.0]"
74+
75+
ym = concore.initval(init_simtime_ym)
76+
oldym = init_simtime_ym
77+
oldt = 0
78+
79+
80+
while(concore.simtime<concore.maxtime):
81+
print("CW outer loop")
82+
while concore.unchanged():
83+
u = concore.read(1,name1,init_simtime_u)
84+
f = {'file1': open(concore.inpath+'1/'+name1, 'rb')}
85+
print("CW: before post u="+str(u))
86+
print('http://www.controlcore.org/pm/'+yuyu+apikey+'&fetch='+name2)
87+
r = requests.post('http://www.controlcore.org/pm/'+yuyu+apikey+'&fetch='+name2, files=f,timeout=timeout_max)
88+
if r.status_code!=200:
89+
print("bad POST request "+str(r.status_code))
90+
quit()
91+
if len(r.text)!=0:
92+
try:
93+
t=literal_eval(r.text)[0]
94+
except:
95+
print("bad eval "+r.text)
96+
timeout_count = 0
97+
t1 = time.perf_counter()
98+
print("CW: after post status="+str(r.status_code)+" r.content="+str(r.content)+" t="+str(t))
99+
#while r.text==oldym or len(r.content)==0:
100+
while oldt==t or len(r.content)==0:
101+
time.sleep(concore.delay)
102+
print("CW waiting status="+str(r.status_code)+" content="+ r.content.decode('utf-8')+" t="+str(t))
103+
f = {'file1': open(concore.inpath+'1/'+name1, 'rb')}
104+
try:
105+
r = requests.post('http://www.controlcore.org/pm/'+yuyu+apikey+'&fetch='+name2, files=f,timeout=timeout_max)
106+
except:
107+
print("CW: bad request")
108+
timeout_count += 1
109+
if r.status_code!=200 or time.perf_counter()-t1 > 1.1*timeout_max: #timeout_count>100:
110+
print("timeout or bad POST request "+str(r.status_code))
111+
quit()
112+
if len(r.text)!=0:
113+
try:
114+
t=literal_eval(r.text)[0]
115+
except:
116+
print("bad eval "+r.text)
117+
oldt = t
118+
oldym = r.text
119+
print("CW: oldym="+oldym+" t="+str(concore.simtime))
120+
concore.write(1,name2,oldym)
121+
#concore.write(1,"ym",init_simtime_ym)
122+
print("retry="+str(concore.retrycount))
123+
124+

tools/learn.py

Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
1+
import concore
2+
import numpy as np
3+
import matplotlib.pyplot as plt
4+
import time
5+
GENERATE_PLOT = 1
6+
7+
concore.delay = 0.002
8+
concore.default_maxtime(150)
9+
init_simtime_u = "[0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0]"
10+
init_simtime_ym = "[0.0, 0.0, 0.0]"
11+
u = concore.initval(init_simtime_u)
12+
ym = concore.initval(init_simtime_ym)
13+
ut = (concore.maxtime+1)*[np.array(u).T]
14+
ymt = (concore.maxtime+1)*[np.array(ym).T]
15+
oldsimtime = concore.simtime
16+
while(concore.simtime<concore.maxtime):
17+
while concore.unchanged():
18+
u = concore.read(concore.iport["VCY"],"u",init_simtime_u)
19+
ym = concore.read(concore.iport["VPY"],"ym",init_simtime_ym)
20+
if concore.simtime > oldsimtime:
21+
ut[int(concore.simtime)] = np.array(u).T
22+
ymt[int(concore.simtime)] = np.array(ym).T
23+
oldsimtime = concore.simtime
24+
print("retry="+str(concore.retrycount))
25+
26+
#################
27+
# plot inputs and outputs
28+
29+
if GENERATE_PLOT == 1:
30+
u1 = [x[0].item() for x in ut]
31+
u2 = [x[1].item() for x in ut]
32+
u3 = [x[2].item() for x in ut]
33+
u4 = [x[3].item() for x in ut]
34+
u5 = [x[4].item() for x in ut]
35+
u6 = [x[5].item() for x in ut]
36+
Nsim = len(u1)
37+
plt.figure()
38+
plt.subplot(321)
39+
plt.plot(range(Nsim), u1)
40+
plt.ylabel('Pw1 (s)')
41+
plt.subplot(322)
42+
plt.plot(range(Nsim), u2)
43+
plt.ylabel('Pf1 (Hz)')
44+
plt.subplot(323)
45+
plt.plot(range(Nsim), u3)
46+
plt.xlabel('Learn Cycles')
47+
plt.ylabel('Pw2 (s)')
48+
plt.subplot(324)
49+
plt.plot(range(Nsim), u4)
50+
plt.ylabel('Pf2 (Hz)')
51+
plt.subplot(325)
52+
plt.plot(range(Nsim), u5)
53+
plt.ylabel('Pw3 (s)')
54+
plt.subplot(326)
55+
plt.plot(range(Nsim), u6)
56+
plt.xlabel('Learn Cycles')
57+
plt.ylabel('Pf3 (Hz)')
58+
plt.savefig("stim.pdf")
59+
plt.tight_layout()
60+
61+
ym1 = [x[0].item() for x in ymt]
62+
ym2 = [x[1].item() for x in ymt]
63+
Nsim = len(ym1)
64+
plt.figure()
65+
plt.subplot(211)
66+
plt.plot(range(Nsim), ym1)
67+
plt.ylabel('MAP (mmHg)')
68+
plt.legend(['Learn MAP'], loc=0)
69+
plt.subplot(212)
70+
plt.plot(range(Nsim), ym2)
71+
plt.xlabel('Cycles')
72+
plt.ylabel('HR (bpm)')
73+
plt.legend(['Learn HR'], loc=0)
74+
plt.savefig("hrmap.pdf")
75+
plt.show()

tools/plotu.py

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
import concore
2+
import numpy as np
3+
import matplotlib.pyplot as plt
4+
import time
5+
print("plot u")
6+
7+
concore.delay = 0.005
8+
concore.default_maxtime(150)
9+
init_simtime_u = "[0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0]"
10+
init_simtime_ym = "[0.0, 0.0, 0.0]"
11+
ut = []
12+
ymt = []
13+
u = concore.initval(init_simtime_u)
14+
while(concore.simtime<concore.maxtime):
15+
while concore.unchanged():
16+
u = concore.read(1,"u",init_simtime_u)
17+
concore.write(1,"u",u)
18+
print("u="+str(u))
19+
ut.append(np.array(u).T)
20+
print("retry="+str(concore.retrycount))
21+
22+
#################
23+
24+
# plot inputs and outputs
25+
u1 = [x[0].item() for x in ut]
26+
u2 = [x[1].item() for x in ut]
27+
u3 = [x[2].item() for x in ut]
28+
u4 = [x[3].item() for x in ut]
29+
u5 = [x[4].item() for x in ut]
30+
u6 = [x[5].item() for x in ut]
31+
32+
Nsim = len(u1)
33+
plt.figure()
34+
plt.subplot(321)
35+
plt.plot(range(Nsim), u1)
36+
plt.ylabel('Pw1 (s)')
37+
plt.subplot(322)
38+
plt.plot(range(Nsim), u2)
39+
plt.ylabel('Pf1 (Hz)')
40+
plt.subplot(323)
41+
plt.plot(range(Nsim), u3)
42+
plt.xlabel('Cycles')
43+
plt.ylabel('Pw2 (s)')
44+
plt.subplot(324)
45+
plt.plot(range(Nsim), u4)
46+
plt.ylabel('Pf2 (Hz)')
47+
plt.subplot(325)
48+
plt.plot(range(Nsim), u5)
49+
plt.ylabel('Pw3 (s)')
50+
plt.subplot(326)
51+
plt.plot(range(Nsim), u6)
52+
plt.xlabel('Cycles')
53+
plt.ylabel('Pf3 (Hz)')
54+
plt.savefig("stim.pdf")
55+
plt.tight_layout()
56+
plt.show()
57+
58+

tools/plotym.py

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
import concore
2+
import numpy as np
3+
import matplotlib.pyplot as plt
4+
import time
5+
print("plot ym")
6+
7+
concore.delay = 0.005
8+
concore.default_maxtime(150)
9+
init_simtime_u = "[0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0]"
10+
init_simtime_ym = "[0.0, 0.0, 0.0]"
11+
ut = []
12+
ymt = []
13+
ym = concore.initval(init_simtime_ym)
14+
while(concore.simtime<concore.maxtime):
15+
while concore.unchanged():
16+
ym = concore.read(1,"ym",init_simtime_ym)
17+
concore.write(1,"ym",ym)
18+
print(" ym="+str(ym))
19+
ymt.append(np.array(ym).T)
20+
print("retry="+str(concore.retrycount))
21+
22+
#################
23+
24+
# plot inputs and outputs
25+
ym1 = [x[0].item() for x in ymt]
26+
ym2 = [x[1].item() for x in ymt]
27+
Nsim = len(ym1)
28+
29+
plt.figure()
30+
plt.subplot(211)
31+
plt.plot(range(Nsim), ym1)
32+
plt.ylabel('MAP (mmHg)')
33+
plt.legend(['MAP'], loc=0)
34+
plt.subplot(212)
35+
plt.plot(range(Nsim), ym2)
36+
plt.xlabel('Cycles')
37+
plt.ylabel('HR (bpm)')
38+
plt.legend(['HR'], loc=0)
39+
plt.savefig("hrmap.pdf")
40+
plt.show()

tools/pwrap.dir/concore.init1

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
[0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0]

0 commit comments

Comments
 (0)