def test_load_save(): ndx1 = create_ndx() file_h5 = output_dir + '/test.h5' ndx1.save(file_h5) ndx3 = TrialNdx.load(file_h5) assert (ndx1 == ndx3) file_txt = output_dir + '/test.txt' ndx3.trial_mask[0, :] = True ndx3.trial_mask[:, 0] = True ndx3.save(file_txt) ndx2 = TrialNdx.load(file_txt) assert (ndx3 == ndx2)
def test_filter(): ndx1 = create_ndx() ndx2 = TrialNdx(ndx1.model_set[:5], ndx1.seg_set[:10], ndx1.trial_mask[:5, :10]) ndx3 = ndx1.filter(ndx2.model_set, ndx2.seg_set, keep=True) assert (ndx2 == ndx3)
def test_split(): ndx1 = create_ndx() num_parts = 3 ndx_list = [] for i in xrange(num_parts): for j in xrange(num_parts): ndx_ij = ndx1.split(i + 1, num_parts, j + 1, num_parts) ndx_list.append(ndx_ij) ndx2 = TrialNdx.merge(ndx_list) assert (ndx1 == ndx2)
def load_scores(score_file, enr_coh_file, coh_test_file, coh_coh_file): scores = TrialScores.load(score_file) scores_enr_coh = None scores_coh_test = None scores_coh_coh = None if enr_coh_file is not None: ndx = TrialNdx(scores.model_set, scores_enr_coh.seg_set) scores_enr_coh = TrialScores.load(enr_coh_file) scores_enr_coh = scores_enr_coh.align_with_ndx(ndx) if coh_test_file is not None: ndx = TrialNdx(scores_coh_test.model_set, scores.seg_set) scores_coh_test = TrialScores.load(coh_test_file) scores_coh_test = scores_coh_test.align_with_ndx(ndx) if coh_coh_file is not None: assert scores_enr_coh is not None and scores_coh_test is not None ndx = TrialNdx(scores_coh_test.model_set, scores_enr_coh.seg_set) scores_coh_coh = TrialScores.load(coh_coh_file) scores_coh_coh = scores_coh_coh.align_with_ndx(ndx) return scores, scores_enr_coh, scores_coh_test, scores_coh_coh
def test_merge(): ndx1 = create_ndx() ndx2 = TrialNdx(ndx1.model_set[:10], ndx1.seg_set, ndx1.trial_mask[:10, :]) ndx3 = TrialNdx(ndx1.model_set[5:], ndx1.seg_set, ndx1.trial_mask[5:, :]) ndx4 = TrialNdx.merge([ndx2, ndx3]) assert (ndx1 == ndx4) ndx2 = TrialNdx(ndx1.model_set, ndx1.seg_set[:10], ndx1.trial_mask[:, :10]) ndx3 = TrialNdx(ndx1.model_set, ndx1.seg_set[5:], ndx1.trial_mask[:, 5:]) ndx4 = TrialNdx.merge([ndx2, ndx3]) assert (ndx1 == ndx4)
def eval_calibration(in_score_file, ndx_file, model_file, out_score_file): logging.info('load ndx: %s' % ndx_file) try: ndx = TrialNdx.load_txt(ndx_file) except: ndx = TrialKey.load_txt(ndx_file) logging.info('load scores: %s' % in_score_file) scr = TrialScores.load_txt(in_score_file) scr = scr.align_with_ndx(ndx) logging.info('load model: %s' % model_file) lr = LR.load(model_file) logging.info('apply calibration') s_cal = lr.predict(scr.scores.ravel()) scr.scores = np.reshape(s_cal, scr.scores.shape) logging.info('save scores: %s' % out_score_file) scr.save_txt(out_score_file)
def convert(input_file, output_file, test_list, class_file, add_ext): scores = TrialScores.load(input_file) if test_list is None: seg_set = scores.seg_set else: with open(test_list, 'r') as f: seg_set = [ seg for seg in [line.rstrip().split(' ')[0] for line in f] if seg != 'segmentid' ] if add_ext: exts = [os.path.splitext(seg)[1] for seg in seg_set] seg_set = [os.path.splitext(seg)[0] for seg in seg_set] if class_file is None: model_set = scores.model_set else: with open(class_file, 'r') as f: model_set = [line.rstrip().split()[0] for line in f] ndx = TrialNdx(model_set, seg_set) scores = scores.set_missing_to_value(ndx, -100) if add_ext: scores.seg_set = [seg + ext for seg, ext in zip(scores.seg_set, exts)] with open(output_file, 'w') as f: f.write('segmentid\t') for model in scores.model_set[:-1]: f.write('%s\t' % model) f.write('%s\n' % scores.model_set[-1]) for i in xrange(scores.scores.shape[1]): f.write('%s\t' % scores.seg_set[i]) for j in xrange(scores.scores.shape[0] - 1): f.write('%f\t' % scores.scores[j, i]) f.write('%f\n' % scores.scores[-1, i])
def create_ndx(ndx_file='./tests/data_in/core-core_det5_ndx.h5'): ndx = TrialNdx.load(ndx_file) ndx.sort() return ndx