1010
1111from ..selection import SubsetSelection
1212from ..video_reader import VideoReader
13+ from ..tools import setup_logger
1314
1415class IDIMethod :
1516 """Common functions for all methods.
@@ -25,6 +26,8 @@ def __init__(self, video: VideoReader, *args, **kwargs):
2526 self .process_number = 0
2627 self .configure (* args , ** kwargs )
2728
29+ # self.logger = setup_logger("pyidi", 10)
30+
2831 # Set the temporary directory
2932 self .temp_dir = os .path .join (self .video .root , 'temp_file' )
3033 self .settings_filename = os .path .join (self .temp_dir , 'settings.pkl' )
@@ -101,19 +104,25 @@ def create_temp_files(self, init_multi=False):
101104 if not init_multi :
102105 token = f'{ self .process_number :0>3.0f} '
103106
104- self .process_log = os .path .join (temp_dir , 'process_log_' + token + '.txt ' )
107+ self .process_log = os .path .join (temp_dir , 'process_log_' + token + '.json ' )
105108 self .points_filename = os .path .join (temp_dir , 'points.pkl' )
106109 self .disp_filename = os .path .join (temp_dir , 'disp_' + token + '.pkl' )
107110
111+ log = {
112+ "input_file" : self .video .input_file ,
113+ "token" : token ,
114+ "points_filename" : self .points_filename ,
115+ "disp_filename" : self .disp_filename ,
116+ "disp_shape" : (self .points .shape [0 ], self .N_time_points , 2 ),
117+ "start_frame" : self .start_time ,
118+ "stop_frame" : self .stop_time ,
119+ "step_frame" : self .step_time ,
120+ "analysis_run" : {
121+ f"run { self .analysis_run } " : {}
122+ },
123+ }
108124 with open (self .process_log , 'w' , encoding = 'utf-8' ) as f :
109- f .writelines ([
110- f'input_file: { self .video .input_file } \n ' ,
111- f'token: { token } \n ' ,
112- f'points_filename: { self .points_filename } \n ' ,
113- f'disp_filename: { self .disp_filename } \n ' ,
114- f'disp_shape: { (self .points .shape [0 ], self .N_time_points , 2 )} \n ' ,
115- f'analysis_run <{ self .analysis_run } >:'
116- ])
125+ json .dump (log , f , indent = 4 )
117126
118127 if not self .points .shape [0 ]:
119128 raise Exception ("Points not set. Please set the points before running the analysis." )
@@ -135,16 +144,15 @@ def update_log(self, last_time):
135144 :type last_time: int
136145 """
137146 with open (self .process_log , 'r' , encoding = 'utf-8' ) as f :
138- log = f . readlines ( )
147+ log = json . load ( f )
139148
140- log_entry = f'analysis_run <{ self .analysis_run } >: finished: { datetime .datetime .now ()} \t last time point: { last_time } '
141- if f'<{ self .analysis_run } >' in log [- 1 ]:
142- log [- 1 ] = log_entry
143- else :
144- log .append ('\n ' + log_entry )
149+ log ['analysis_run' ][f"run { self .analysis_run } " ] = {
150+ 'finished' : datetime .datetime .now ().strftime ("%Y-%m-%d %H:%M:%S" ),
151+ 'last_time_point' : last_time
152+ }
145153
146154 with open (self .process_log , 'w' , encoding = 'utf-8' ) as f :
147- f . writelines (log )
155+ json . dump (log , f , indent = 4 )
148156
149157 def resume_temp_files (self ):
150158 """Reload the settings written in the temporary files.
@@ -155,19 +163,26 @@ def resume_temp_files(self):
155163 temp_dir = self .temp_dir
156164 token = f'{ self .process_number :0>3.0f} '
157165
158- self .process_log = os .path .join (temp_dir , 'process_log_' + token + '.txt ' )
166+ self .process_log = os .path .join (temp_dir , 'process_log_' + token + '.json ' )
159167 self .disp_filename = os .path .join (temp_dir , 'disp_' + token + '.pkl' )
160168
161169 with open (self .process_log , 'r' , encoding = 'utf-8' ) as f :
162- log = f .readlines ()
170+ log = json .load (f )
171+
172+ shape = tuple ([int (_ ) for _ in log ['disp_shape' ]])
163173
164- shape = tuple ([int (_ ) for _ in log [4 ].replace (' ' , '' ).split (':' )[1 ].replace ('(' , '' ).replace (')' , '' ).split (',' )])
165-
166174 self .temp_disp = np .memmap (self .disp_filename , dtype = np .float64 , mode = 'r+' , shape = shape )
167175 self .displacements = np .array (self .temp_disp ).copy ()
168176
169- self .start_time = int (log [- 1 ].replace (' ' , '' ).rstrip ().split ('\t ' )[1 ].split (':' )[1 ]) + 1
170- self .analysis_run = int (log [- 1 ].split ('<' )[1 ].split ('>' )[0 ]) + 1
177+ self .start_time = log ['start_frame' ]
178+ self .stop_time = log ['stop_frame' ]
179+ self .step_time = log ['step_frame' ]
180+
181+ last_analysis_run = int (list (log ['analysis_run' ].keys ())[- 1 ].split (' ' )[- 1 ])
182+ self .completed_points = int (log ['analysis_run' ][f"run { last_analysis_run } " ]['last_time_point' ])
183+
184+ self .analysis_run = last_analysis_run + 1
185+ self .N_time_points = len (range (self .start_time , self .stop_time , self .step_time ))
171186
172187 def temp_files_check (self ):
173188 """Checking the settings of computation.
0 commit comments