Exemplo n.º 1
0
def test_save_repr():
    '''
    Test save repr
    '''

    model = initialize_model('CoCoNet', TEST_SHAPES, TEST_ARCHITECTURE)
    fasta = generate_fasta_file(*TEST_CTG_LENGTHS)
    coverage = generate_h5_file(*TEST_CTG_LENGTHS, filename='coverage.h5')

    output = {k: Path('repr_{}.h5'.format(k))
              for k in ['composition', 'coverage']}

    save_repr_all(model, fasta, coverage, n_frags=5, frag_len=FL, output=output,
                  min_ctg_len=0, wsize=WSIZE, wstep=WSTEP)

    assert all(out.is_file() for out in output.values())

    handles = {k: h5py.File(v, 'r') for (k, v) in output.items()}
    firsts = {k: handle.get(list(handle.keys())[0]).shape
              for k, handle in handles.items()}

    latent_dim = (TEST_ARCHITECTURE['composition']['neurons'][-1]
                  + TEST_ARCHITECTURE['coverage']['neurons'][-1])

    assert firsts['composition'] == (5, latent_dim)
    assert firsts['coverage'] == (5, latent_dim)

    fasta.unlink()
    coverage.unlink()
    for key, filename in output.items():
        handles[key].close()
        filename.unlink()
Exemplo n.º 2
0
def test_load_model():
    '''
    Test if model can be loaded
    '''

    args = {'compo_neurons': TEST_ARCHITECTURE['composition']['neurons'],
            'cover_neurons': TEST_ARCHITECTURE['coverage']['neurons'],
            'cover_filters': TEST_ARCHITECTURE['coverage']['n_filters'],
            'cover_kernel': TEST_ARCHITECTURE['coverage']['kernel_size'],
            'cover_stride': TEST_ARCHITECTURE['coverage']['conv_stride'],
            'merge_neurons': TEST_ARCHITECTURE['merge']['neurons'],
            'kmer': 4, 'no_rc': True,
            'fragment_length': FL, 'wsize': WSIZE, 'wstep': WSTEP}

    cfg = Configuration()
    cfg.init_config(output='.', **args)
    cfg.io['h5'] = generate_h5_file(FL, filename='coverage.h5')

    model = initialize_model('CoCoNet', cfg.get_input_shapes(), cfg.get_architecture())
    model_path = Path('CoCoNet.pth')

    torch.save({
        'state': model.state_dict()
    }, model_path)

    loaded_model = load_model(cfg)

    model_path.unlink()
    cfg.io['h5'].unlink()

    assert isinstance(loaded_model, CoCoNet)
Exemplo n.º 3
0
def test_learn_save_load_model():
    '''
    Check:
    - if the training goes through
    - if the model is saved
    '''

    model = initialize_model('CoCoNet', TEST_SHAPES, TEST_ARCHITECTURE)
    model_file = Path('{}/test_model.pth'.format(LOCAL_DIR))
    results_file = Path('{}/test_res.csv'.format(LOCAL_DIR))

    pair_files = {'train': Path('{}/pairs_train.npy'.format(LOCAL_DIR)),
                  'test': Path('{}/pairs_test.npy'.format(LOCAL_DIR))}

    coverage_file = generate_h5_file(*TEST_CTG_LENGTHS, filename='coverage.h5')
    fasta_file = generate_fasta_file(*TEST_CTG_LENGTHS, save=True)

    fasta = [(seq.id, str(seq.seq)) for seq in SeqIO.parse(fasta_file, 'fasta')]

    make_pairs(fasta, STEP, FL, output=pair_files['train'], n_examples=50)
    make_pairs(fasta, STEP, FL, output=pair_files['test'], n_examples=5)

    train(model, fasta_file, coverage_file, pair_files, results_file, output=model_file,
          **TEST_LEARN_PRMS)

    tests = model_file.is_file() and results_file.is_file()

    for path in list(pair_files.values()) + [fasta_file, coverage_file, model_file, results_file]:
        path.unlink()

    assert tests
Exemplo n.º 4
0
def test_init_combined_model():
    '''
    Check if model can be initialized
    '''

    model = initialize_model('CoCoNet', TEST_SHAPES, TEST_ARCHITECTURE)

    assert isinstance(model, CoCoNet)
Exemplo n.º 5
0
def test_init_coverage_model():
    '''
    Check if model can be initialized
    '''

    model = initialize_model('coverage',
                             TEST_SHAPES['coverage'],
                             TEST_ARCHITECTURE['coverage'])

    assert isinstance(model, CoverageModel)
Exemplo n.º 6
0
def test_init_composition_model():
    '''
    Check if model can be initialized
    '''

    model = initialize_model('composition',
                             TEST_SHAPES['composition'],
                             TEST_ARCHITECTURE['composition'])

    assert isinstance(model, CompositionModel)
Exemplo n.º 7
0
def test_coverage_model():
    '''
    Test if coverage model can compute an output
    '''
    model = initialize_model('coverage', TEST_SHAPES['coverage'], TEST_ARCHITECTURE['coverage'])
    x_rd = [torch.FloatTensor(4, *TEST_SHAPES['coverage'][::-1]).random_(0, 10) for _ in range(2)]
    y_rd = torch.FloatTensor(4, 1).random_(0, 1)

    pred = model(*x_rd)
    loss = model.compute_loss(pred, y_rd)

    assert 'coverage' in pred
    assert y_rd.shape == pred['coverage'].shape
    assert isinstance(loss, torch.FloatTensor)
Exemplo n.º 8
0
def generate_rd_model():

    model = initialize_model('CoCoNet', TEST_SHAPES, TEST_ARCHITECTURE)

    return model