@@ -145,19 +145,28 @@ def _mav_output_parser(filename):
145145
146146 return data
147147
148- # def _ti_test(outdir, log_evidence_mv):
149- # """
150- # Write a bestK result based in TI results.
151- # """
152- # bestk_dir = os.path.join(outdir, "bestK")
153- # os.makedirs(bestk_dir, exist_ok=True)
154- # bestk = max(log_evidence_mv, key=log_evidence_mv.get).replace("K", "1")
155- # bestk_file = open(os.path.join(bestk_dir, "TI_integration.txt"), "w")
156- # output_text = ("MavericK's estimation test revealed "
157- # "that the best value of 'K' is: {}\n".format(bestk))
158- # bestk_file.write(output_text)
159- # bestk_file.close()
160- # return [int(bestk)]
148+ def _ti_test (outdir , norm_evidence , parameters ):
149+ """
150+ Write a bestK result based on TI or STRUCTURE results.
151+ """
152+ if mav_ti_in_use (parameters ):
153+ # Use TI for bestK estimation
154+ criteria = norm_evidence [2 ]
155+ else :
156+ # Use Structure for bestK estimation
157+ criteria = norm_evidence [1 ]
158+ means = {x : y ["norm_mean" ] for x , y in criteria .items ()}
159+
160+ bestk_dir = os .path .join (outdir , "bestK" )
161+ os .makedirs (bestk_dir , exist_ok = True )
162+ bestk = max (means , key = means .get )
163+ bestk_file = open (os .path .join (bestk_dir , "TI_integration.txt" ), "w" )
164+ output_text = ("MavericK's estimation test revealed "
165+ "that the best value of 'K' is: {}\n " .format (bestk ))
166+ bestk_file .write (output_text )
167+ bestk_file .close ()
168+ return [int (bestk )]
169+ # TODO: Draw a plot!
161170
162171 def _gen_files_list (output_params , no_tests ):
163172 """
@@ -194,7 +203,7 @@ def _gen_files_list(output_params, no_tests):
194203
195204 return files_list , no_tests
196205
197- def _write_normalized_output (evidence , k_list ):
206+ def _write_normalized_output (evidence , k_list , parameters ):
198207 """
199208 Writes the normalized output file.
200209 """
@@ -243,7 +252,9 @@ def _write_normalized_output(evidence, k_list):
243252 outfile .write (line )
244253 outfile .write ("\n " )
245254
246-
255+ if no_tests is False :
256+ bestk = _ti_test (outdir , normalized , parameters )
257+ return bestk
247258
248259 output_params = ("outputEvidence" , "outputEvidence_on" ,
249260 "outputEvidenceDetails_on" , "outputEvidenceDetails" )
@@ -277,15 +288,10 @@ def _write_normalized_output(evidence, k_list):
277288 else :
278289 outfile .write (diff [1 ])
279290 if evidence is not None :
280- _write_normalized_output (evidence , k_list )
291+ bestk = _write_normalized_output (evidence , k_list , mav_params )
281292 outfile .close ()
282293
283- # TODO: BestK presentation
284- # TODO: Make tests comply with new code
285-
286- # if no_tests is False:
287- # bestk = _ti_test(outdir, log_evidence_mv)
288- # return bestk
294+ return bestk
289295
290296
291297def maverick_normalization (x_mean , x_sd , klist , draws = int (1e6 ), limit = 95 ):
0 commit comments