File size: 4,073 Bytes
5032722
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
Created on Wed Jan 29 17:36:42 2025


About:
=======
initialize dataloaders and pytorch datasets

"""
import pickle
import torch
from torch.utils.data import DataLoader
import numpy as np

from dloaders.CountsDset import CountsDset 
from dloaders.CountsDset import jax_collator as collator
from dloaders.init_dataloader import init_dataloader
from dloaders.init_time_array import init_time_array

   
def init_counts_dset( args, 
                      task, 
                      training_argparse=None,
                      include_dataloader=True ):
    """
    initialize the dataloaders
    """
    #################################
    ### training-specific options   #
    #################################
    if task in ['train', 
                'resume_train']:
        only_test = False
        t_per_sample = args.pred_config['times_from'] == 't_per_sample'
        t_array_for_all_samples = init_time_array(args)
        subs_only = (args.pred_config['indel_model_type'] is None)
        
        if args.pred_config['subst_model_type'].lower() == 'hky85':
            emission_alphabet_size = 4
        else:
            emission_alphabet_size = 20
            
    
    #############################
    ### eval-specific options   #
    #############################
    elif task in ['eval']:
        only_test = True
        t_per_sample = training_argparse.pred_config['times_from'] == 't_per_sample'
        t_array_for_all_samples = init_time_array(training_argparse)
        subs_only = (training_argparse.pred_config['indel_model_type'] is None)
        
        if training_argparse.pred_config['subst_model_type'].lower() == 'hky85':
            emission_alphabet_size = 4
        else:
            emission_alphabet_size = 20
            
            
    #################
    ### LOAD DATA   #
    #################
    # test data
    assert type(args.test_dset_splits) == list

    print('Test dset:')
    for s in args.test_dset_splits:
        print(s)
    print()

    test_dset = CountsDset( data_dir = args.data_dir, 
                            split_prefixes = args.test_dset_splits,
                            emission_alphabet_size = emission_alphabet_size,
                            t_per_sample = t_per_sample,
                            subs_only = subs_only,
                            toss_alignments_longer_than = args.toss_alignments_longer_than)
    out = {'test_dset': test_dset,
           't_array_for_all_samples': t_array_for_all_samples}


    # training data
    if not only_test:
        assert type(args.train_dset_splits) == list

        print('Training dset:')
        for s in args.train_dset_splits:
            print(s)
        print()
        
        training_dset = CountsDset( data_dir = args.data_dir, 
                                    split_prefixes = args.train_dset_splits,
                                    emission_alphabet_size = emission_alphabet_size,
                                    t_per_sample = t_per_sample,
                                    subs_only = subs_only,
                                    toss_alignments_longer_than = args.toss_alignments_longer_than)
        out['training_dset'] = training_dset
        
        
    ############################################
    ### create dataloaders, output dictionary  #
    ############################################
    if include_dataloader:
        test_dl = init_dataloader(args = args, 
                                  pytorch_custom_dset = test_dset,
                                  shuffle = False,
                                  collate_fn = collator)
        out['test_dl'] = test_dl
        
        if not only_test:
            training_dl = init_dataloader(args = args, 
                                          pytorch_custom_dset = training_dset,
                                          shuffle = True,
                                          collate_fn = collator)
            out['training_dl'] = training_dl
            
    return out