22import matplotlib
33matplotlib .use ('Agg' )
44import shutil
5- from subprocess import Popen , PIPE , check_output
5+ from subprocess import Popen , PIPE , check_output , STDOUT
66import os
77import pwd
88import shlex
1212import math
1313
1414print ("*****************************" )
15- print ("OpenWorm Master Script v0.9.2 " )
15+ print ("OpenWorm Master Script v0.9.3 " )
1616print ("*****************************" )
1717print ("" )
1818print ("This script attempts to run a full pass through the OpenWorm scientific libraries." )
7676def execute_with_realtime_output (command , directory , env = None ):
7777 p = None
7878 try :
79- p = Popen (shlex .split (command ), stdout = PIPE , bufsize = 1 , cwd = directory , env = env )
79+
80+ print ('>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>' )
81+ print ('>> Executing command: %s' % command )
82+ print ('>> --------------------------------------------------------------' )
83+ p = Popen (shlex .split (command ),
84+ stdout = PIPE ,
85+ stderr = STDOUT ,
86+ cwd = directory ,
87+ env = env )
8088 with p .stdout :
8189 for line in iter (p .stdout .readline , b'' ):
82- print (line .decode ('utf-8' ), end = '' )
90+ print ('>> %s' % line .decode ('utf-8' ), end = '' )
8391 p .wait () # wait for the subprocess to exit
8492 except KeyboardInterrupt as e :
8593 print ("Caught CTRL+C" )
8694 if p :
8795 p .kill ()
8896 raise e
97+ print ('>> --------------------------------------------------------------' )
98+ print ('>> Command exited with %i: %s' % (p .returncode ,command ))
99+ print ('>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>\n ' )
100+
101+ if p .returncode != 0 :
102+ print ('Exiting as the last command failed' )
103+ exit (p .returncode )
104+
89105
90106
91107sys .path .append (os .environ ['C302_HOME' ])
@@ -110,6 +126,10 @@ def execute_with_realtime_output(command, directory, env=None):
110126if 'DURATION' in os .environ :
111127 sim_duration = float (os .environ ['DURATION' ])
112128
129+ noc302 = False
130+ if 'NOC302' in os .environ :
131+ noc302 = bool (os .environ ['NOC302' ])
132+
113133DEFAULTS = {'duration' : sim_duration ,
114134 'dt' : 0.005 ,
115135 'dtNrn' : 0.05 ,
@@ -119,13 +139,15 @@ def execute_with_realtime_output(command, directory, env=None):
119139 'verbose' : False ,
120140 'device' : 'GPU' ,
121141 'configuration' : 'worm_crawl_half_resolution' ,
122- 'noc302' : False ,
142+ 'noc302' : noc302 ,
123143 'datareader' : 'UpdatedSpreadsheetDataReader2' ,
124144 'outDir' : OW_OUT_DIR }
125145
126146my_env = os .environ .copy ()
127147my_env ["DISPLAY" ] = ":44"
128148
149+ # Xvfb or X virtual framebuffer is a display server implementing the X11 display server protocol.
150+ # In contrast to other display servers, Xvfb performs all graphical operations in virtual memory without showing any screen output.
129151os .system ('Xvfb :44 -listen tcp -ac -screen 0 1920x1080x24+32 &' ) # TODO: terminate xvfb after recording
130152
131153try :
@@ -151,6 +173,9 @@ def execute_with_realtime_output(command, directory, env=None):
151173 DEFAULTS ['datareader' ],
152174 'simulations' )
153175 #DEFAULTS['outDir'])
176+
177+ if noc302 : command += ' -noc302'
178+
154179 execute_with_realtime_output (command , os .environ ['SIBERNETIC_HOME' ], env = my_env )
155180except KeyboardInterrupt as e :
156181 pass
@@ -162,9 +187,13 @@ def execute_with_realtime_output(command, directory, env=None):
162187 for directory in dirnames :
163188 if directory .startswith ('%s_%s' % (DEFAULTS ['c302params' ], DEFAULTS ['reference' ])):
164189 all_subdirs .append (os .path .join (dirpath , directory ))
190+ if directory .startswith ('Sibernetic' ):
191+ all_subdirs .append (os .path .join (dirpath , directory ))
165192
166193latest_subdir = max (all_subdirs , key = os .path .getmtime )
167-
194+ print ('\n ========================================================================\n ' )
195+ print ('Finished main simulation, data saved in: %s' % latest_subdir )
196+ execute_with_realtime_output ('ls -alt %s' % latest_subdir , os .environ ['SIBERNETIC_HOME' ], env = my_env )
168197
169198try :
170199 os .mkdir ('%s/output' % OW_OUT_DIR )
@@ -198,19 +227,27 @@ def execute_with_realtime_output(command, directory, env=None):
198227 print ("Moving %s to %s" % (wcon , new_sim_out ))
199228 shutil .move (wcon , new_sim_out )
200229
230+ time .sleep (2 )
201231
202232# Rerun and record simulation
203233os .system ('export DISPLAY=:44' )
204234sibernetic_movie_name = '%s.mp4' % os .path .split (latest_subdir )[- 1 ]
205- os .system ('tmux new-session -d -s SiberneticRecording "DISPLAY=:44 ffmpeg -r 30 -f x11grab -draw_mouse 0 -s 1920x1080 -i :44 -filter:v "crop=1200:800:100:100" -cpu-used 0 -b:v 384k -qmin 10 -qmax 42 -maxrate 384k -bufsize 1000k -an %s/%s"' % (new_sim_out , sibernetic_movie_name ))
235+ command = 'tmux new-session -d -s SiberneticRecording "DISPLAY=:44 ffmpeg -r 30 -f x11grab -draw_mouse 0 -s 1920x1080 -i :44 -filter:v "crop=1200:800:100:100" -cpu-used 0 -b:v 384k -qmin 10 -qmax 42 -maxrate 384k -bufsize 1000k -an %s/%s"' % (new_sim_out , sibernetic_movie_name )
236+ execute_with_realtime_output (command , os .environ ['SIBERNETIC_HOME' ], env = my_env )
237+
238+ time .sleep (3 )
239+
240+ execute_with_realtime_output ('tmux list-sessions' , os .environ ['SIBERNETIC_HOME' ], env = my_env )
206241
207242command = './Release/Sibernetic -f %s -l_from lpath=%s' % (DEFAULTS ['configuration' ], latest_subdir )
208243execute_with_realtime_output (command , os .environ ['SIBERNETIC_HOME' ], env = my_env )
209244
210- os . system ('tmux send-keys -t SiberneticRecording q' )
211- os . system ('tmux send-keys -t SiberneticRecording "exit" C-m' )
245+ execute_with_realtime_output ('tmux send-keys -t SiberneticRecording q' , os . environ [ 'SIBERNETIC_HOME' ], env = my_env )
246+ execute_with_realtime_output ('tmux send-keys -t SiberneticRecording "exit" C-m' , os . environ [ 'SIBERNETIC_HOME' ], env = my_env )
212247
213- time .sleep (3 )
248+ time .sleep (5 )
249+
250+ execute_with_realtime_output ('ls -alt %s' % latest_subdir , os .environ ['SIBERNETIC_HOME' ], env = my_env )
214251
215252# Remove black frames at the beginning of the recorded video
216253command = "ffmpeg -i %s/%s -vf blackdetect=d=0:pic_th=0.70:pix_th=0.10 -an -f null - 2>&1 | grep blackdetect" % (new_sim_out , sibernetic_movie_name )
@@ -247,7 +284,7 @@ def execute_with_realtime_output(command, directory, env=None):
247284os .system ('ffmpeg -ss 1 -i %s/cut_%s -vf "select=gt(scene\,0.1)" -vsync vfr -vf fps=fps=1/1 %s' % (new_sim_out , sibernetic_movie_name , 'tmp/out%06d.jpg' ))
248285os .system ('ffmpeg -r 100 -i %s -r 100 -vb 60M %s/speeded_%s' % ('tmp/out%06d.jpg' , new_sim_out , sibernetic_movie_name ))
249286
250- os .system ('sudo rm -r tmp/*' )
287+ os .system ('rm -r tmp/*' )
251288
252289
253290
0 commit comments