-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathmisc_pytorch_fastai_dataset.py
174 lines (131 loc) · 3.42 KB
/
misc_pytorch_fastai_dataset.py
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
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
# ---
# jupyter:
# jupytext:
# text_representation:
# extension: .py
# format_name: percent
# format_version: '1.3'
# jupytext_version: 1.16.2
# kernelspec:
# display_name: Python 3 (ipykernel)
# language: python
# name: python3
# ---
# %% [markdown]
# # Datasets
#
# Datasets are `Iterable` (through their `__getitem__` and `__len__` attribute).
# Datasets are provided to `DataLoaders` which perform the aggreation to batches.
# %%
from pimmslearn.io.datasplits import long_format
from fastai.collab import CollabDataLoaders
import random
import pandas as pd
import pimmslearn.io.datasets as datasets
import pimmslearn.utils as test_data
# %%
N, M = 15, 7
data = test_data.create_random_missing_data(N, M, prop_missing=.4)
# %% [markdown]
# ## Datasets
#
# - `DatasetWithMaskAndNoTarget`
# - `DatasetWithTarget`
# - `DatasetWithTargetSpecifyTarget`
# %% [markdown]
# ### `DatasetWithMaskAndNoTarget`
# - base class for datasets with missing values and no target
# %%
dataset = datasets.DatasetWithMaskAndNoTarget(df=pd.DataFrame(data))
for _mask, _array in dataset:
break
_array, _mask
# %% [markdown]
# ### `DatasetWithTarget`
# %%
data = test_data.create_random_missing_data(N, M, prop_missing=0.3)
dataset = datasets.DatasetWithTarget(df=pd.DataFrame(data))
for _mask, _array, target in dataset:
if any(_mask):
print(_array, _mask, target, sep='\n')
break
# %% [markdown]
# ### `DatasetWithTargetSpecifyTarget`
# %%
data = test_data.create_random_missing_data(N, M, prop_missing=0.2)
df = pd.DataFrame(data)
val_y = df.stack().groupby(level=0).sample(frac=0.2)
targets = val_y.unstack().sort_index(axis=1)
df[targets.notna()] = pd.NA
df
# %% [markdown]
# The targets are complementary
# %%
targets
# %%
dataset = datasets.DatasetWithTargetSpecifyTarget(df=df, targets=targets)
for _mask, _array, target in dataset:
if any(_mask):
print(_mask, _array, target, sep='\n')
break
# %%
row = random.randint(0, len(dataset) - 1)
print(f"{row = }")
dataset[row]
# %%
dataset[row:row + 2]
# %% [markdown]
# ## DataLoaders
#
# FastAI DataLoaders accept pytorch datasets
# %%
# , MSELossFlat, Learner
# from fastai.collab import EmbeddingDotBias
data = pd.DataFrame(data)
data.index.name, data.columns.name = ('Sample ID', 'peptide')
df_long = long_format(pd.DataFrame(data))
df_long.reset_index(inplace=True)
df_long.head()
# %%
dls = CollabDataLoaders.from_df(df_long, valid_pct=0.15,
user_name='Sample ID', item_name='peptide', rating_name='intensity',
bs=4)
type(dls.dataset), dls.dataset._dl_type # no __mro__?
# %% [markdown]
# Iterating over the dataset gives the column names
# %%
for x in dls.dataset:
print(x)
# %% [markdown]
# Training DataFrame is hidden under items
# %%
dls.dataset.items
# %%
for x in dls.train_ds:
print(x)
break
# %%
dls.train_ds
# %% [markdown]
# Iterating over the dataset returns columns, not single rows
# %%
# dls.train_ds.__getitem__??
# %%
dls.train_ds.items['Sample ID']
# %% [markdown]
# But the `DataLoader` return the numeric representation in batches:
# %%
for batch in dls.train_ds:
break
batch
# %%
# dls.train.__iter__??
# %%
# _SingleProcessDataLoaderIter??
# %% [markdown]
# So.. It seems too complicated
# - the `_collate_fn` seems to aggrete the data from the DataFrame
# - should be possible to keep track of that
# %%
next(iter(dls.dataset))
# %%