@@ -112,27 +112,28 @@ def analyze_individual_status_by_generation(self, csv_output, status,
112112 aggregate_functions = None ,
113113 generations = 'all' ):
114114 print ('\n [INDIVIDUAL ' + status .upper () + ' STATUS ANALYSIS]' )
115- print ('Opening outputfile: ' + csv_output + '...' )
116- outputfile = open (csv_output , 'w' )
117115 print ('Getting population size...' )
118116 pop_size = \
119117 database_calls .db_reconstruct_simulation_parameters (self .cur ,
120118 self .starting_time )['population_size' ]
119+ print ('Constructing generations list...' )
120+ if generations == 'all' :
121+ generation_list = database_calls .db_list_generations (self .cur ,
122+ self .starting_time )
123+ else :
124+ generation_list = [str (gen ) for gen in generations ]
125+ print ('Preparing outputfile: ' + csv_output + '...' )
126+ outputfile = open (csv_output , 'w' )
121127 print ('Writing outputfile header...' )
122128 header = ['Generation' ] + [str (i ) for i in range (1 , pop_size + 1 )]
123129 if aggregate_functions != None :
124130 header = header + \
125131 [key for key in list (aggregate_functions .keys ())]
126132 outputfile .write (',' .join (header ) + '\n ' )
133+ outputfile .close ()
127134 print ('Starting main analysis...' )
128- if generations == 'all' :
129- generation_list = database_calls .db_list_generations (self .cur ,
130- self .starting_time )
131- else :
132- generation_list = [str (gen ) for gen in generations ]
133135 for generation in generation_list :
134136 print ('Analyzing generation ' + str (generation ) + '...' )
135- print ()
136137 status_list = \
137138 [status_analysis (stat )
138139 for stat in self .get_individual_status_list_by_generation (status , generation )]
@@ -141,22 +142,25 @@ def analyze_individual_status_by_generation(self, csv_output, status,
141142 if aggregate_functions != None :
142143 for key in list (aggregate_functions .keys ()):
143144 status_row .append (str (aggregate_functions [key ](status_list )))
145+ outputfile = open (csv_output , 'a' )
144146 outputfile .write (',' .join (status_row ) + '\n ' )
147+ outputfile .close ()
148+
145149 print ('\n Individual [' + status + '] analysis complete!' )
146150
147151 def analyze_status_group_count_by_generation (self , csv_output , status ,
148152 stats ,
149153 aggregate_functions = None ,
150154 generations = 'all' ):
151155 print ('\n [' + status .upper () + ' STATUS GROUP COUNT ANALYSIS]' )
152- print ('Opening outputfile: ' + csv_output + '...' )
153- outputfile = open (csv_output , 'w' )
154156 print ('Constructing generations list...' )
155157 if generations == 'all' :
156158 generation_list = database_calls .db_list_generations (self .cur ,
157159 self .starting_time )
158160 else :
159161 generation_list = generations
162+ print ('Preparing outputfile: ' + csv_output + '...' )
163+ outputfile = open (csv_output , 'w' )
160164 print ('Writing outputfile header...' )
161165 header = [str (stat ).replace (", " ,"-" ) for stat in stats ]
162166 if aggregate_functions == None :
@@ -165,10 +169,10 @@ def analyze_status_group_count_by_generation(self, csv_output, status,
165169 header = ['Generation' ] + header + \
166170 [key for key in list (aggregate_functions .keys ())]
167171 outputfile .write (',' .join (header ) + '\n ' )
172+ outputfile .close ()
168173 print ('Starting main analysis...' )
169174 for generation in generation_list :
170175 print ('Analyzing generation ' + str (generation ) + '...' )
171- print ()
172176 status_list = \
173177 self .get_individual_status_list_by_generation (status ,
174178 generation )
@@ -178,31 +182,34 @@ def analyze_status_group_count_by_generation(self, csv_output, status,
178182 if aggregate_functions != None :
179183 for key in list (aggregate_functions .keys ()):
180184 status_row .append (str (aggregate_functions [key ]([status_list .count (target_stat ) for target_stat in stats ])))
181- outputfile .write (',' .join (status_row ) + '\n ' )
185+ outputfile = open (csv_output , 'a' )
186+ outputfile .write (',' .join (status_row ) + '\n ' )
187+ outputfile .close ()
182188 print ('\n Grouped [' + status + '] count analysis complete!' )
183189
184190 def analyze_individual_genomes_by_generation (self , csv_output ,
185191 genome_analysis ,
186192 aggregate_functions = None ,
187193 generations = 'all' ):
188194 print ('\n [INDIVIDUAL GENOME ANALYSIS]' )
189- print ('Opening outputfile: ' + csv_output + '...' )
190- outputfile = open (csv_output , 'w' )
191195 print ('Getting population size...' )
192196 pop_size = database_calls .db_reconstruct_simulation_parameters (self .cur , self .starting_time )['population_size' ]
197+ print ('Constructing generations list...' )
198+ if generations == 'all' :
199+ generation_list = database_calls .db_list_generations (self .cur , self .starting_time )
200+ else :
201+ generation_list = generations
202+ print ('Preparing outputfile: ' + csv_output + '...' )
203+ outputfile = open (csv_output , 'w' )
193204 print ('Writing outputfile header...' )
194205 header = ['Generation' ] + [str (i ) for i in range (1 , pop_size + 1 )]
195206 if aggregate_functions != None :
196207 header = header + [key for key in list (aggregate_functions .keys ())]
197208 outputfile .write (',' .join (header ) + '\n ' )
209+ outputfile .close ()
198210 print ('Starting main analysis...' )
199- if generations == 'all' :
200- generation_list = database_calls .db_list_generations (self .cur , self .starting_time )
201- else :
202- generation_list = generations
203211 for generation in generation_list :
204212 print ('Analyzing generation ' + str (generation ) + '...' )
205- print ()
206213 genome_list = [genome_analysis (genome )
207214 for genome in self .get_individual_genome_list_by_generation (generation )]
208215 status_row = [str (generation )] + \
@@ -211,7 +218,9 @@ def analyze_individual_genomes_by_generation(self, csv_output,
211218 if aggregate_functions != None :
212219 for key in list (aggregate_functions .keys ()):
213220 status_row .append (str (aggregate_functions [key ](genome_list )))
221+ outputfile = open (csv_output , 'a' )
214222 outputfile .write (',' .join (status_row ) + '\n ' )
223+ outputfile .close ()
215224 print ('\n Individual genome analysis complete!' )
216225
217226 def analyze_status_group_genome_by_generation (self , csv_output ,
@@ -220,13 +229,13 @@ def analyze_status_group_genome_by_generation(self, csv_output,
220229 aggregate_functions = None ,
221230 generations = 'all' ):
222231 print ('\n [' + status .upper () + ' STATUS GROUP GENOME ANALYSIS]' )
223- print ('Opening outputfile: ' + csv_output + '...' )
224- outputfile = open (csv_output , 'w' )
225232 print ('Constructing generations list...' )
226233 if generations == 'all' :
227234 generation_list = database_calls .db_list_generations (self .cur , self .starting_time )
228235 else :
229236 generation_list = generations
237+ print ('Preparing outputfile: ' + csv_output + '...' )
238+ outputfile = open (csv_output , 'w' )
230239 print ('Writing outputfile header...' )
231240 header = [str (stat ).replace (", " ,"-" ) for stat in stats ]
232241 if aggregate_functions == None :
@@ -235,10 +244,10 @@ def analyze_status_group_genome_by_generation(self, csv_output,
235244 header = ['Generation' ] + header + \
236245 [key for key in list (aggregate_functions .keys ())]
237246 outputfile .write (',' .join (header ) + '\n ' )
247+ outputfile .close ()
238248 print ('Starting main analysis...' )
239249 for generation in generation_list :
240250 print ('Analyzing generation ' + str (generation ) + '...' )
241- print ()
242251 analyzed_genome_list = \
243252 [genome_analysis (self .get_status_group_genome_by_generation (status ,
244253 target_status , generation ))
@@ -249,5 +258,7 @@ def analyze_status_group_genome_by_generation(self, csv_output,
249258 if aggregate_functions != None :
250259 for key in list (aggregate_functions .keys ()):
251260 status_row .append (str (aggregate_functions [key ](analyzed_genome_list )))
252- outputfile .write (',' .join (status_row ) + '\n ' )
261+ outputfile = open (csv_output , 'a' )
262+ outputfile .write (',' .join (status_row ) + '\n ' )
263+ outputfile .close ()
253264 print ('\n Grouped [' + status + '] genome analysis complete!' )
0 commit comments