Skip to content

Commit c32458e

Browse files
Merge pull request #134 from arnor-sigurdsson/add-sequence-configuration-guide
Add sequence configuration guide
2 parents aa9a0ea + a28c03e commit c32458e

4 files changed

Lines changed: 1519 additions & 1173 deletions

File tree

docs/configuration_guides/guides_index.rst

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,10 @@ then return here for copy-paste configurations and optimization guidance.
2222
**Looking for specific options?** See the full :doc:`../api/api_reference`
2323
for comprehensive parameter documentation.
2424

25+
.. note::
26+
This section is currently as work in progress.
27+
Expect more guides to be added over time.
28+
2529
Global Configuration Template
2630
-----------------------------
2731

@@ -40,4 +44,5 @@ Input Data Templates
4044
.. toctree::
4145
:maxdepth: 1
4246

43-
inputs/genomics_guide
47+
inputs/genomics_guide
48+
inputs/sequence_guide
Lines changed: 339 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,339 @@
1+
Sequence Data Guide
2+
===================
3+
4+
Ready-to-use configurations for sequence data analysis
5+
using Transformer-based models in EIR.
6+
7+
- **Supported data types:** Text (NLP), protein/peptide sequences, DNA/RNA,
8+
time series, and other discrete token sequences
9+
- **Data format:** A folder with ``.txt`` files (filename is the ID) or
10+
a ``.csv`` file with columns ``"ID"`` and ``"Sequence"``
11+
- **Models:** Built-in transformer (``sequence-default``),
12+
external pretrained models (BERT, RoBERTa, etc., see :ref:`external-sequence-models`).
13+
14+
.. note::
15+
**First step:** Copy the :doc:`../guides_index` global configuration as your ``globals.yaml``
16+
17+
.. contents::
18+
:local:
19+
:depth: 2
20+
21+
Quick Start
22+
-----------
23+
24+
- **Use cases:** Sequence classification (sentiment, protein function), regression (binding affinity), or generation
25+
- **Data requirements:** Sequence data in text files or CSV format, labels for supervised tasks
26+
27+
**Files needed:**
28+
29+
.. code-block:: yaml
30+
:caption: inputs.yaml
31+
32+
input_info:
33+
input_source: data/protein_sequences/ # Path to folder with .txt files or .csv file
34+
input_name: sequence
35+
input_type: sequence
36+
37+
input_type_info:
38+
max_length: 512 # Sequence length (int, 'max', or 'average')
39+
40+
# Split on characters for proteins/DNA
41+
# ("" for char-level, " " for words,
42+
# null for no splitting e.g. when using BPE tokenizer)
43+
split_on: ""
44+
45+
tokenizer: null # No tokenizer (see advanced options below)
46+
min_freq: 2 # Minimum token frequency for vocabulary
47+
48+
model_config:
49+
model_type: sequence-default # Built-in transformer for sequences
50+
model_init_config:
51+
embedding_dim: 128 # Token embedding dimension
52+
num_layers: 4 # Number of transformer layers
53+
num_heads: 8 # Number of attention heads per layer
54+
dropout: 0.10 # Dropout rate
55+
56+
.. note::
57+
The ``input_source`` can be:
58+
59+
- A directory of ``.txt`` files where the filename (without extension) is the sample ID
60+
- A ``.csv`` file with columns ``"ID"`` and ``"Sequence"``
61+
62+
For protein/DNA sequences, use ``split_on: ""`` for character-level tokenization.
63+
For natural language, use ``split_on: " "`` for word-level tokenization.
64+
65+
Alternatively, set ``split_on: null`` for no splitting, and use the
66+
`BPE tokenizer <https://en.wikipedia.org/wiki/Byte_pair_encoding>`_
67+
(``tokenizer: "bpe"``) for an adaptive vocabulary.
68+
69+
70+
.. code-block:: yaml
71+
:caption: outputs.yaml
72+
73+
output_info:
74+
output_name: sequence_label
75+
output_source: data/labels.csv # Must contain "ID" column + targets
76+
output_type: tabular
77+
78+
output_type_info:
79+
target_cat_columns:
80+
- Function_Class # Categorical target (e.g., protein function)
81+
target_con_columns:
82+
- Binding_Affinity # Continuous target (optional)
83+
84+
**Run command:**
85+
86+
.. code-block:: bash
87+
88+
eirtrain --global_configs globals.yaml \
89+
--input_configs inputs.yaml \
90+
--output_configs outputs.yaml
91+
92+
About Sequence Models
93+
---------------------
94+
95+
**Full model configuration with all available parameters:**
96+
97+
.. code-block:: yaml
98+
:caption: Advanced sequence configuration
99+
100+
model_config:
101+
model_type: sequence-default
102+
model_init_config:
103+
# Architecture parameters
104+
embedding_dim: 128 # Dimension of token embeddings
105+
num_layers: 6 # Number of transformer layers
106+
num_heads: 8 # Number of attention heads
107+
dropout: 0.10 # Dropout rate in transformer layers
108+
109+
# Advanced architecture options
110+
dim_feedforward: 512 # Feedforward network dimension
111+
112+
# Attention mechanisms
113+
window_size: null # Local attention window (null = full attention)
114+
115+
As always, please refer to the
116+
API documentation :ref:`sequence-configurations` for
117+
the full list of available parameters and more in-depth explanations.
118+
119+
Common Use Cases
120+
----------------
121+
122+
Natural Language Processing
123+
^^^^^^^^^^^^^^^^^^^^^^^^^^^
124+
125+
For text classification, sentiment analysis, or document classification:
126+
127+
.. code-block:: yaml
128+
:caption: Text classification setup
129+
130+
input_type_info:
131+
max_length: 512
132+
split_on: " " # Split on whitespace for words
133+
tokenizer: "basic_english" # English text normalization
134+
min_freq: 5 # Filter rare words
135+
136+
Biological Sequences
137+
^^^^^^^^^^^^^^^^^^^^
138+
139+
For protein, peptide, or DNA sequence analysis:
140+
141+
.. code-block:: yaml
142+
:caption: Protein sequence setup
143+
144+
input_type_info:
145+
max_length: 1024 # Typical protein length
146+
split_on: "" # Character-level tokenization
147+
tokenizer: null # No additional tokenization
148+
min_freq: 1 # Keep all amino acids/nucleotides
149+
150+
Time Series Data
151+
^^^^^^^^^^^^^^^^
152+
153+
For sequential numeric data represented as text
154+
(assumes they have e.g. been binned/discretized beforehand):
155+
156+
.. code-block:: yaml
157+
:caption: Time series setup
158+
159+
input_type_info:
160+
max_length: "average" # Use average sequence length
161+
split_on: "," # Split on delimiter
162+
tokenizer: null # No tokenization
163+
sampling_strategy_if_longer: "uniform" # Random sampling for long sequences
164+
165+
Advanced Tokenization
166+
---------------------
167+
168+
**BPE (Byte Pair Encoding) Tokenization:**
169+
170+
For subword tokenization, particularly useful for handling out-of-vocabulary words:
171+
172+
.. code-block:: yaml
173+
:caption: BPE tokenizer configuration
174+
175+
input_type_info:
176+
tokenizer: "bpe"
177+
adaptive_tokenizer_max_vocab_size: 10000 # Maximum vocabulary size
178+
vocab_file: null # Will be trained on your data
179+
split_on: null # BPE handles splitting internally
180+
181+
182+
**Custom Vocabulary:**
183+
184+
Using a pre-defined vocabulary file:
185+
186+
.. code-block:: yaml
187+
:caption: Custom vocabulary setup
188+
189+
input_type_info:
190+
vocab_file: "data/custom_vocab.json" # JSON file with token->id mapping
191+
192+
.. note::
193+
The vocab file is a optional text file containing pre-defined vocabulary to use
194+
for the training. If this is not passed in, the framework will automatically
195+
build the vocabulary from the training data. Passing in a vocabulary file is
196+
therefore useful if (a) you want to manually specify / limit the vocabulary used
197+
and/or (b) you want to save time by pre-computing the vocabulary.
198+
199+
Here, there are two formats supported:
200+
201+
- A ``.json`` file containing a dictionary with the vocabulary as keys and
202+
the corresponding token IDs as values. For example:
203+
``{"the": 0, "cat": 1, "sat": 2, "on": 3, "the": 4, "mat": 5}``
204+
205+
- A ``.json`` file with the results of training and saving the vocabulary of
206+
a Huggingface BPE tokenizer. This is the file create by calling
207+
``hf_tokenizer.save()``. This is only valid when using the ``bpe`` tokenizer.
208+
209+
210+
211+
Sequence Length Strategies
212+
--------------------------
213+
214+
**Dynamic Length Calculation:**
215+
216+
.. code-block:: yaml
217+
:caption: Dynamic length options
218+
219+
input_type_info:
220+
max_length: "max" # Use longest sequence in dataset
221+
# OR
222+
max_length: "average" # Use average length
223+
# OR
224+
max_length: 512 # Fixed length
225+
226+
**Handling Long Sequences:**
227+
228+
.. code-block:: yaml
229+
:caption: Long sequence handling
230+
231+
input_type_info:
232+
sampling_strategy_if_longer: "uniform" # Random sampling for training
233+
# OR
234+
sampling_strategy_if_longer: "from_start" # Always truncate from beginning
235+
236+
.. note::
237+
Validation and test sets always use ``"from_start"`` for consistency,
238+
regardless of the training strategy.
239+
240+
External Pretrained Models
241+
--------------------------
242+
243+
For leveraging pretrained language models:
244+
245+
.. code-block:: yaml
246+
:caption: Using pretrained BERT
247+
248+
model_config:
249+
model_type: "bert-base-uncased" # Hugging Face model name
250+
pretrained_model: true # Use pretrained weights
251+
model_init_config:
252+
num_labels: 2 # Number of output classes
253+
254+
See :ref:`external-sequence-models` for the full list of supported models.
255+
256+
257+
Attribution Analysis
258+
--------------------
259+
260+
Enable feature importance analysis to understand which parts of sequences
261+
contribute most to predictions:
262+
263+
.. code-block:: yaml
264+
:caption: Attribution analysis setup (in globals.yaml)
265+
266+
attribution_analysis:
267+
compute_attributions: true
268+
max_attributions_per_class: 100 # Samples per class to analyze
269+
attributions_every_sample_factor: 4 # Compute every 4th evaluation
270+
271+
This uses `Integrated Gradients <https://arxiv.org/abs/1703.01365>`_ to compute
272+
token-level importance scores, helping you understand model decisions.
273+
274+
Complete Configuration Examples
275+
-------------------------------
276+
277+
**Protein Function Prediction:**
278+
279+
.. code-block:: yaml
280+
:caption: Complete protein classification setup
281+
282+
# inputs.yaml
283+
input_info:
284+
input_source: data/protein_sequences/
285+
input_name: protein_seq
286+
input_type: sequence
287+
input_type_info:
288+
max_length: 1024
289+
split_on: "" # Character-level for amino acids
290+
min_freq: 1 # Keep all amino acids
291+
model_config:
292+
model_type: sequence-default
293+
model_init_config:
294+
embedding_dim: 128
295+
num_layers: 4
296+
num_heads: 8
297+
dropout: 0.10
298+
299+
# outputs.yaml
300+
output_info:
301+
output_name: protein_function
302+
output_source: data/protein_labels.csv
303+
output_type: tabular
304+
output_type_info:
305+
target_cat_columns:
306+
- Enzyme_Class
307+
- Subcellular_Location
308+
309+
**Sentiment Analysis:**
310+
311+
.. code-block:: yaml
312+
:caption: Complete sentiment analysis setup
313+
314+
# inputs.yaml
315+
input_info:
316+
input_source: data/reviews.csv # CSV with ID and Sequence columns
317+
input_name: review_text
318+
input_type: sequence
319+
input_type_info:
320+
max_length: 512
321+
split_on: " " # Word-level tokenization
322+
tokenizer: "basic_english" # Text normalization
323+
min_freq: 5 # Filter rare words
324+
model_config:
325+
model_type: sequence-default
326+
model_init_config:
327+
embedding_dim: 256
328+
num_layers: 6
329+
num_heads: 8
330+
dropout: 0.10
331+
332+
# outputs.yaml
333+
output_info:
334+
output_name: sentiment
335+
output_source: data/sentiment_labels.csv
336+
output_type: tabular
337+
output_type_info:
338+
target_cat_columns:
339+
- Sentiment # Positive/Negative

pyproject.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ dependencies = [
4848
"lightning>=2.5.0.post0",
4949
"polars>=1.27.0",
5050
"diffusers>=0.33.1",
51-
"deeplake>=4.2.4",
51+
"deeplake==4.2.6",
5252
"torch==2.7",
5353
]
5454

0 commit comments

Comments
 (0)