openprotein-python 0.5.0__py3-none-any.whl → 0.5.0.dev0__py3-none-any.whl
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- openprotein/api/embedding.py +10 -22
- openprotein/api/fold.py +5 -5
- openprotein/api/predictor.py +5 -74
- openprotein/api/svd.py +3 -46
- openprotein/app/models/align/msa.py +8 -1
- openprotein/app/models/align/prompt.py +6 -1
- openprotein/app/models/design.py +3 -9
- openprotein/app/models/embeddings/base.py +14 -29
- openprotein/app/models/embeddings/future.py +4 -25
- openprotein/app/models/embeddings/poet.py +22 -37
- openprotein/app/models/fold/alphafold2.py +7 -11
- openprotein/app/models/fold/esmfold.py +2 -5
- openprotein/app/models/futures.py +6 -21
- openprotein/app/models/predict.py +9 -34
- openprotein/app/models/predictor/predict.py +3 -3
- openprotein/app/models/predictor/predictor.py +5 -60
- openprotein/app/models/svd.py +10 -50
- openprotein/app/models/train.py +8 -11
- openprotein/base.py +31 -33
- openprotein/schemas/__init__.py +1 -9
- openprotein/schemas/align.py +13 -7
- openprotein/schemas/design.py +2 -2
- openprotein/schemas/job.py +0 -13
- openprotein/schemas/train.py +3 -3
- {openprotein_python-0.5.0.dist-info → openprotein_python-0.5.0.dev0.dist-info}/METADATA +1 -1
- {openprotein_python-0.5.0.dist-info → openprotein_python-0.5.0.dev0.dist-info}/RECORD +28 -28
- {openprotein_python-0.5.0.dist-info → openprotein_python-0.5.0.dev0.dist-info}/WHEEL +0 -0
- {openprotein_python-0.5.0.dist-info → openprotein_python-0.5.0.dev0.dist-info}/licenses/LICENSE.txt +0 -0
|
@@ -403,44 +403,29 @@ class PagedFuture(StreamingFuture, ABC):
|
|
|
403
403
|
|
|
404
404
|
with concurrent.futures.ThreadPoolExecutor(max_workers=num_workers) as executor:
|
|
405
405
|
# submit the paged requests
|
|
406
|
-
futures
|
|
407
|
-
index: int = 0
|
|
406
|
+
futures = []
|
|
408
407
|
for _ in range(num_workers * 2):
|
|
409
408
|
f = executor.submit(self.get_slice, offset, offset + step)
|
|
410
|
-
futures
|
|
411
|
-
index += 1
|
|
409
|
+
futures.append(f)
|
|
412
410
|
offset += step
|
|
413
411
|
|
|
414
412
|
# until we've retrieved all pages (known by retrieving a page with less than the requested number of records)
|
|
415
413
|
done = False
|
|
416
414
|
while not done:
|
|
417
|
-
|
|
418
|
-
futures_next: dict[concurrent.futures.Future, int] = {}
|
|
419
|
-
index_next: int = 0
|
|
420
|
-
next_result_index = 0
|
|
415
|
+
futures_next = []
|
|
421
416
|
# iterate the futures and submit new requests as needed
|
|
422
417
|
for f in concurrent.futures.as_completed(futures):
|
|
423
|
-
index = futures[f]
|
|
424
418
|
result_page = f.result()
|
|
425
|
-
results[index] = result_page
|
|
426
419
|
# check if we're done, meaning the result page is not full
|
|
427
420
|
done = done or len(result_page) < step
|
|
428
421
|
# if we aren't done, submit another request
|
|
429
422
|
if not done:
|
|
430
423
|
f = executor.submit(self.get_slice, offset, offset + step)
|
|
431
|
-
futures_next
|
|
432
|
-
index_next += 1
|
|
424
|
+
futures_next.append(f)
|
|
433
425
|
offset += step
|
|
434
426
|
# yield the results from this page
|
|
435
|
-
|
|
436
|
-
|
|
437
|
-
and results[next_result_index] is not None
|
|
438
|
-
):
|
|
439
|
-
result_page = results[next_result_index]
|
|
440
|
-
assert result_page is not None # checked above
|
|
441
|
-
for result in result_page:
|
|
442
|
-
yield result
|
|
443
|
-
next_result_index += 1
|
|
427
|
+
for result in result_page:
|
|
428
|
+
yield result
|
|
444
429
|
# update the list of futures and wait on them again
|
|
445
430
|
futures = futures_next
|
|
446
431
|
|
|
@@ -1,5 +1,3 @@
|
|
|
1
|
-
import logging
|
|
2
|
-
|
|
3
1
|
from openprotein.api import predict
|
|
4
2
|
from openprotein.base import APISession
|
|
5
3
|
from openprotein.schemas import (
|
|
@@ -10,8 +8,6 @@ from openprotein.schemas import (
|
|
|
10
8
|
|
|
11
9
|
from .futures import Future, PagedFuture
|
|
12
10
|
|
|
13
|
-
logger = logging.getLogger(__name__)
|
|
14
|
-
|
|
15
11
|
|
|
16
12
|
class PredictFuture(PagedFuture, Future):
|
|
17
13
|
"""Future Job for manipulating results"""
|
|
@@ -36,9 +32,10 @@ class PredictFuture(PagedFuture, Future):
|
|
|
36
32
|
def id(self):
|
|
37
33
|
return self.job.job_id
|
|
38
34
|
|
|
39
|
-
def _fmt_results(self,
|
|
35
|
+
def _fmt_results(self, job: WorkflowPredictJob):
|
|
36
|
+
results = job.result
|
|
40
37
|
dict_results = {}
|
|
41
|
-
if
|
|
38
|
+
if results is not None:
|
|
42
39
|
properties = set(
|
|
43
40
|
list(i["properties"].keys())[0]
|
|
44
41
|
for i in results[0].model_dump()["predictions"]
|
|
@@ -56,11 +53,10 @@ class PredictFuture(PagedFuture, Future):
|
|
|
56
53
|
}
|
|
57
54
|
return dict_results
|
|
58
55
|
|
|
59
|
-
def _fmt_ssp_results(
|
|
60
|
-
|
|
61
|
-
):
|
|
56
|
+
def _fmt_ssp_results(self, job: WorkflowPredictSingleSiteJob):
|
|
57
|
+
results = job.result
|
|
62
58
|
dict_results = {}
|
|
63
|
-
if
|
|
59
|
+
if results is not None:
|
|
64
60
|
properties = set(
|
|
65
61
|
list(i["properties"].keys())[0]
|
|
66
62
|
for i in results[0].model_dump()["predictions"]
|
|
@@ -113,34 +109,13 @@ class PredictFuture(PagedFuture, Future):
|
|
|
113
109
|
# else:
|
|
114
110
|
# return self._fmt_ssp_results(results)
|
|
115
111
|
|
|
116
|
-
def
|
|
117
|
-
|
|
118
|
-
results:
|
|
119
|
-
num_returned = self.page_size
|
|
120
|
-
offset = 0
|
|
121
|
-
|
|
122
|
-
while num_returned >= self.page_size:
|
|
123
|
-
try:
|
|
124
|
-
predict_job_results = self.get_results(
|
|
125
|
-
page_offset=offset, page_size=self.page_size
|
|
126
|
-
)
|
|
127
|
-
if predict_job_results.result is not None:
|
|
128
|
-
results += predict_job_results.result
|
|
129
|
-
num_returned = len(predict_job_results.result)
|
|
130
|
-
offset += num_returned
|
|
131
|
-
except Exception as exc:
|
|
132
|
-
if verbose:
|
|
133
|
-
logging.error(f"Failed to get results: {exc}")
|
|
134
|
-
|
|
135
|
-
if self.job.job_type == JobType.workflow_predict_single_site:
|
|
112
|
+
def get_slice(self, start: int, end: int):
|
|
113
|
+
results = self.get_results(page_size=end - start, page_offset=start)
|
|
114
|
+
if isinstance(results, WorkflowPredictSingleSiteJob):
|
|
136
115
|
return self._fmt_ssp_results(results)
|
|
137
116
|
else:
|
|
138
117
|
return self._fmt_results(results)
|
|
139
118
|
|
|
140
|
-
def get_slice(self, start: int, end: int):
|
|
141
|
-
results = self.get_results(page_size=end - start, page_offset=start)
|
|
142
|
-
return results.result or [] # could be none
|
|
143
|
-
|
|
144
119
|
def get_results(
|
|
145
120
|
self, page_size: int | None = None, page_offset: int | None = None
|
|
146
121
|
) -> WorkflowPredictSingleSiteJob | WorkflowPredictJob:
|
|
@@ -59,9 +59,9 @@ class PredictionResultFuture(Future):
|
|
|
59
59
|
data = predictor.predictor_predict_get_sequence_result(
|
|
60
60
|
self.session, self.job.job_id, sequence
|
|
61
61
|
)
|
|
62
|
-
return predictor.
|
|
62
|
+
return predictor.decode_score(data)
|
|
63
63
|
|
|
64
|
-
def get(self
|
|
64
|
+
def get(self) -> tuple[np.ndarray, np.ndarray]:
|
|
65
65
|
"""
|
|
66
66
|
Get embedding results for specified sequence.
|
|
67
67
|
|
|
@@ -75,4 +75,4 @@ class PredictionResultFuture(Future):
|
|
|
75
75
|
data = predictor.predictor_predict_get_batched_result(
|
|
76
76
|
self.session, self.job.job_id
|
|
77
77
|
)
|
|
78
|
-
return predictor.
|
|
78
|
+
return predictor.decode_score(data, batched=True)
|
|
@@ -1,10 +1,9 @@
|
|
|
1
|
-
import numpy as np
|
|
2
1
|
from openprotein.api import assaydata
|
|
3
2
|
from openprotein.api import job as job_api
|
|
4
3
|
from openprotein.api import predictor, svd
|
|
5
4
|
from openprotein.base import APISession
|
|
6
5
|
from openprotein.errors import InvalidParameterError
|
|
7
|
-
from openprotein.schemas import
|
|
6
|
+
from openprotein.schemas import PredictorMetadata, TrainJob
|
|
8
7
|
|
|
9
8
|
from ..assaydata import AssayDataset
|
|
10
9
|
from ..embeddings import EmbeddingModel
|
|
@@ -13,37 +12,6 @@ from ..svd import SVDModel
|
|
|
13
12
|
from .predict import PredictionResultFuture
|
|
14
13
|
|
|
15
14
|
|
|
16
|
-
class CVResultFuture(Future):
|
|
17
|
-
"""Future Job for manipulating results"""
|
|
18
|
-
|
|
19
|
-
job: CVJob
|
|
20
|
-
|
|
21
|
-
def __init__(
|
|
22
|
-
self,
|
|
23
|
-
session: APISession,
|
|
24
|
-
job: CVJob,
|
|
25
|
-
):
|
|
26
|
-
super().__init__(session, job)
|
|
27
|
-
|
|
28
|
-
@property
|
|
29
|
-
def id(self):
|
|
30
|
-
return self.job.job_id
|
|
31
|
-
|
|
32
|
-
def get(self, verbose: bool = False) -> tuple[np.ndarray, np.ndarray, np.ndarray]:
|
|
33
|
-
"""
|
|
34
|
-
Get embedding results for specified sequence.
|
|
35
|
-
|
|
36
|
-
Args:
|
|
37
|
-
sequence (bytes): sequence to fetch results for
|
|
38
|
-
|
|
39
|
-
Returns:
|
|
40
|
-
mu (np.ndarray): means of predictions
|
|
41
|
-
var (np.ndarray): variances of predictions
|
|
42
|
-
"""
|
|
43
|
-
data = predictor.predictor_crossvalidate_get(self.session, self.job.job_id)
|
|
44
|
-
return predictor.decode_crossvalidate(data)
|
|
45
|
-
|
|
46
|
-
|
|
47
15
|
class PredictorModel(Future):
|
|
48
16
|
"""
|
|
49
17
|
Class providing predict endpoint for fitted predictor models.
|
|
@@ -80,15 +48,15 @@ class PredictorModel(Future):
|
|
|
80
48
|
|
|
81
49
|
@property
|
|
82
50
|
def id(self):
|
|
83
|
-
return self.
|
|
51
|
+
return self.metadata.id
|
|
84
52
|
|
|
85
53
|
@property
|
|
86
54
|
def reduction(self):
|
|
87
|
-
return self.
|
|
55
|
+
return self.metadata.model_spec.features.reduction
|
|
88
56
|
|
|
89
57
|
@property
|
|
90
58
|
def sequence_length(self):
|
|
91
|
-
if (constraints := self.
|
|
59
|
+
if (constraints := self.metadata.model_spec.constraints) is not None:
|
|
92
60
|
return constraints.sequence_length
|
|
93
61
|
return None
|
|
94
62
|
|
|
@@ -100,7 +68,7 @@ class PredictorModel(Future):
|
|
|
100
68
|
|
|
101
69
|
@property
|
|
102
70
|
def training_properties(self) -> list[str]:
|
|
103
|
-
return self.
|
|
71
|
+
return self.metadata.training_dataset.properties
|
|
104
72
|
|
|
105
73
|
@property
|
|
106
74
|
def metadata(self):
|
|
@@ -157,16 +125,6 @@ class PredictorModel(Future):
|
|
|
157
125
|
),
|
|
158
126
|
)
|
|
159
127
|
|
|
160
|
-
def crossvalidate(self, n_splits: int | None = None) -> CVResultFuture:
|
|
161
|
-
return CVResultFuture.create(
|
|
162
|
-
session=self.session,
|
|
163
|
-
job=predictor.predictor_crossvalidate_post(
|
|
164
|
-
session=self.session,
|
|
165
|
-
predictor_id=self.id,
|
|
166
|
-
n_splits=n_splits,
|
|
167
|
-
),
|
|
168
|
-
)
|
|
169
|
-
|
|
170
128
|
def predict(self, sequences: list[bytes] | list[str]) -> PredictionResultFuture:
|
|
171
129
|
if self.sequence_length is not None:
|
|
172
130
|
for sequence in sequences:
|
|
@@ -181,16 +139,3 @@ class PredictorModel(Future):
|
|
|
181
139
|
session=self.session, predictor_id=self.id, sequences=sequences
|
|
182
140
|
),
|
|
183
141
|
)
|
|
184
|
-
|
|
185
|
-
def single_site(self, sequence: bytes | str) -> PredictionResultFuture:
|
|
186
|
-
if self.sequence_length is not None:
|
|
187
|
-
if len(sequence) != self.sequence_length:
|
|
188
|
-
raise InvalidParameterError(
|
|
189
|
-
f"Expected sequence to predict to be of length {self.sequence_length}"
|
|
190
|
-
)
|
|
191
|
-
return PredictionResultFuture.create(
|
|
192
|
-
session=self.session,
|
|
193
|
-
job=predictor.predictor_predict_single_site_post(
|
|
194
|
-
session=self.session, predictor_id=self.id, base_sequence=sequence
|
|
195
|
-
),
|
|
196
|
-
)
|
openprotein/app/models/svd.py
CHANGED
|
@@ -1,12 +1,11 @@
|
|
|
1
1
|
from typing import TYPE_CHECKING
|
|
2
2
|
|
|
3
|
-
import numpy as np
|
|
4
3
|
from openprotein.api import assaydata
|
|
5
4
|
from openprotein.api import job as job_api
|
|
6
5
|
from openprotein.api import predictor, svd
|
|
7
6
|
from openprotein.base import APISession
|
|
8
7
|
from openprotein.errors import InvalidParameterError
|
|
9
|
-
from openprotein.schemas import FeatureType, FitJob,
|
|
8
|
+
from openprotein.schemas import FeatureType, FitJob, SVDMetadata
|
|
10
9
|
|
|
11
10
|
from .assaydata import AssayDataset, AssayMetadata
|
|
12
11
|
from .embeddings import EmbeddingModel, EmbeddingResultFuture
|
|
@@ -51,19 +50,19 @@ class SVDModel(Future):
|
|
|
51
50
|
|
|
52
51
|
@property
|
|
53
52
|
def id(self):
|
|
54
|
-
return self.
|
|
53
|
+
return self.metadata.id
|
|
55
54
|
|
|
56
55
|
@property
|
|
57
56
|
def n_components(self):
|
|
58
|
-
return self.
|
|
57
|
+
return self.metadata.n_components
|
|
59
58
|
|
|
60
59
|
@property
|
|
61
60
|
def sequence_length(self):
|
|
62
|
-
return self.
|
|
61
|
+
return self.metadata.sequence_length
|
|
63
62
|
|
|
64
63
|
@property
|
|
65
64
|
def reduction(self):
|
|
66
|
-
return self.
|
|
65
|
+
return self.metadata.reduction
|
|
67
66
|
|
|
68
67
|
@property
|
|
69
68
|
def metadata(self):
|
|
@@ -103,9 +102,7 @@ class SVDModel(Future):
|
|
|
103
102
|
"""
|
|
104
103
|
return svd.svd_get_sequences(session=self.session, svd_id=self.id)
|
|
105
104
|
|
|
106
|
-
def embed(
|
|
107
|
-
self, sequences: list[bytes] | list[str], **kwargs
|
|
108
|
-
) -> EmbeddingResultFuture:
|
|
105
|
+
def embed(self, sequences: list[bytes]) -> EmbeddingResultFuture:
|
|
109
106
|
"""
|
|
110
107
|
Use this SVD model to get reduced embeddings from input sequences.
|
|
111
108
|
|
|
@@ -121,10 +118,7 @@ class SVDModel(Future):
|
|
|
121
118
|
"""
|
|
122
119
|
return EmbeddingResultFuture.create(
|
|
123
120
|
session=self.session,
|
|
124
|
-
job=svd.svd_embed_post(
|
|
125
|
-
session=self.session, svd_id=self.id, sequences=sequences, **kwargs
|
|
126
|
-
),
|
|
127
|
-
sequences=sequences,
|
|
121
|
+
job=svd.svd_embed_post(self.session, self.id, sequences),
|
|
128
122
|
)
|
|
129
123
|
|
|
130
124
|
def fit_gp(
|
|
@@ -153,14 +147,9 @@ class SVDModel(Future):
|
|
|
153
147
|
from .predictor import PredictorModel
|
|
154
148
|
|
|
155
149
|
model_id = self.id
|
|
156
|
-
# get assay if str
|
|
157
|
-
assay = (
|
|
158
|
-
assaydata.get_assay_metadata(session=self.session, assay_id=assay)
|
|
159
|
-
if isinstance(assay, str)
|
|
160
|
-
else assay
|
|
161
|
-
)
|
|
162
150
|
# extract assay_id
|
|
163
|
-
|
|
151
|
+
if isinstance(assay, str):
|
|
152
|
+
assay = assaydata.get_assay_metadata(session=self.session, assay_id=assay)
|
|
164
153
|
if (
|
|
165
154
|
self.sequence_length is not None
|
|
166
155
|
and assay.sequence_length != self.sequence_length
|
|
@@ -168,17 +157,7 @@ class SVDModel(Future):
|
|
|
168
157
|
raise InvalidParameterError(
|
|
169
158
|
f"Expected dataset to be of sequence length {self.sequence_length} due to svd fitted constraints"
|
|
170
159
|
)
|
|
171
|
-
if
|
|
172
|
-
raise InvalidParameterError("Expected (at-least) 1 property to train")
|
|
173
|
-
if not set(properties) <= set(assay.measurement_names):
|
|
174
|
-
raise InvalidParameterError(
|
|
175
|
-
f"Expected all provided properties to be a subset of assay's measurements: {assay.measurement_names}"
|
|
176
|
-
)
|
|
177
|
-
# TODO - support multitask
|
|
178
|
-
if len(properties) > 1:
|
|
179
|
-
raise InvalidParameterError(
|
|
180
|
-
"Training a multitask GP is not yet supported (i.e. number of properties should only be 1 for now)"
|
|
181
|
-
)
|
|
160
|
+
assay_id = assay.id if isinstance(assay, AssayDataset) else assay.assay_id
|
|
182
161
|
job = predictor.predictor_fit_gp_post(
|
|
183
162
|
session=self.session,
|
|
184
163
|
assay_id=assay_id,
|
|
@@ -190,22 +169,3 @@ class SVDModel(Future):
|
|
|
190
169
|
**kwargs,
|
|
191
170
|
)
|
|
192
171
|
return PredictorModel.create(session=self.session, job=job)
|
|
193
|
-
|
|
194
|
-
|
|
195
|
-
class SVDEmbeddingResultFuture(EmbeddingResultFuture, Future):
|
|
196
|
-
"""Future for manipulating results for embeddings-related requests."""
|
|
197
|
-
|
|
198
|
-
job: SVDEmbeddingsJob
|
|
199
|
-
|
|
200
|
-
def get_item(self, sequence: bytes) -> np.ndarray:
|
|
201
|
-
"""
|
|
202
|
-
Get embedding results for specified sequence.
|
|
203
|
-
|
|
204
|
-
Args:
|
|
205
|
-
sequence (bytes): sequence to fetch results for
|
|
206
|
-
|
|
207
|
-
Returns:
|
|
208
|
-
np.ndarray: embeddings
|
|
209
|
-
"""
|
|
210
|
-
data = svd.embed_get_sequence_result(self.session, self.job.job_id, sequence)
|
|
211
|
-
return svd.embed_decode(data)
|
openprotein/app/models/train.py
CHANGED
|
@@ -128,7 +128,7 @@ class TrainFuture(Future):
|
|
|
128
128
|
CVFuture
|
|
129
129
|
The cross-validation job associated with this training job.
|
|
130
130
|
"""
|
|
131
|
-
|
|
131
|
+
return CVFuture.create(
|
|
132
132
|
session=self.session,
|
|
133
133
|
job=train.crossvalidate(
|
|
134
134
|
session=self.session,
|
|
@@ -136,8 +136,6 @@ class TrainFuture(Future):
|
|
|
136
136
|
),
|
|
137
137
|
train_job_id=self.id,
|
|
138
138
|
)
|
|
139
|
-
self.crossvalidation = cv_future
|
|
140
|
-
return cv_future
|
|
141
139
|
|
|
142
140
|
def list_models(self):
|
|
143
141
|
"""
|
|
@@ -235,7 +233,7 @@ class CVFuture(PagedFuture, Future):
|
|
|
235
233
|
self,
|
|
236
234
|
session: APISession,
|
|
237
235
|
job: WorkflowCVJob,
|
|
238
|
-
train_job_id: str
|
|
236
|
+
train_job_id: str,
|
|
239
237
|
page_size: int = 1000,
|
|
240
238
|
):
|
|
241
239
|
"""
|
|
@@ -251,11 +249,6 @@ class CVFuture(PagedFuture, Future):
|
|
|
251
249
|
The Job object for this cross-validation job.
|
|
252
250
|
"""
|
|
253
251
|
super().__init__(session=session, job=job, page_size=page_size)
|
|
254
|
-
if train_job_id is None:
|
|
255
|
-
assert (
|
|
256
|
-
job.prerequisite_job_id is not None
|
|
257
|
-
), "expected prerequisite train job id"
|
|
258
|
-
train_job_id = job.prerequisite_job_id
|
|
259
252
|
self.train_job_id = train_job_id
|
|
260
253
|
|
|
261
254
|
def __str__(self) -> str:
|
|
@@ -268,8 +261,12 @@ class CVFuture(PagedFuture, Future):
|
|
|
268
261
|
def id(self):
|
|
269
262
|
return self.job.job_id
|
|
270
263
|
|
|
271
|
-
def _fmt_results(self, results: WorkflowCVJob)
|
|
272
|
-
return
|
|
264
|
+
def _fmt_results(self, results: WorkflowCVJob):
|
|
265
|
+
return (
|
|
266
|
+
[i.model_dump() for i in results.result]
|
|
267
|
+
if results.result is not None
|
|
268
|
+
else []
|
|
269
|
+
)
|
|
273
270
|
|
|
274
271
|
def get_slice(self, start: int, end: int):
|
|
275
272
|
results = self.get_crossvalidation(page_size=end - start, page_offset=start)
|
openprotein/base.py
CHANGED
|
@@ -1,12 +1,12 @@
|
|
|
1
|
-
|
|
2
|
-
from urllib.parse import urljoin
|
|
1
|
+
import openprotein.config as config
|
|
3
2
|
|
|
4
3
|
import requests
|
|
4
|
+
from urllib.parse import urljoin
|
|
5
|
+
from typing import Union
|
|
6
|
+
|
|
5
7
|
from requests.adapters import HTTPAdapter
|
|
6
8
|
from requests.packages.urllib3.util.retry import Retry
|
|
7
|
-
|
|
8
|
-
import openprotein.config as config
|
|
9
|
-
from openprotein.errors import APIError, AuthError, HTTPError
|
|
9
|
+
from openprotein.errors import HTTPError, APIError, AuthError
|
|
10
10
|
|
|
11
11
|
|
|
12
12
|
class BearerAuth(requests.auth.AuthBase):
|
|
@@ -38,28 +38,23 @@ class APISession(requests.Session):
|
|
|
38
38
|
>>> session = APISession("username", "password")
|
|
39
39
|
"""
|
|
40
40
|
|
|
41
|
-
def __init__(
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
backend: str = "https://api.openprotein.ai/api/",
|
|
46
|
-
timeout: int = 180,
|
|
47
|
-
):
|
|
41
|
+
def __init__(self, username:str,
|
|
42
|
+
password:str,
|
|
43
|
+
backend:str = "https://api.openprotein.ai/api/",
|
|
44
|
+
timeout:int = 180):
|
|
48
45
|
super().__init__()
|
|
49
46
|
self.backend = backend
|
|
50
47
|
self.verify = True
|
|
51
48
|
self.timeout = timeout
|
|
52
49
|
|
|
53
50
|
# Custom retry strategies
|
|
54
|
-
#
|
|
51
|
+
#auto retry for pesky connection reset errors and others
|
|
55
52
|
# 503 will catch if BE is refreshing
|
|
56
|
-
retry = Retry(
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
status_forcelist=[500, 502, 503, 504, 101, 104],
|
|
60
|
-
)
|
|
53
|
+
retry = Retry(total=4,
|
|
54
|
+
backoff_factor=3, #0,1,4,13s
|
|
55
|
+
status_forcelist=[500, 502, 503, 504, 101, 104])
|
|
61
56
|
adapter = HTTPAdapter(max_retries=retry)
|
|
62
|
-
self.mount(
|
|
57
|
+
self.mount('https://', adapter)
|
|
63
58
|
self.login(username, password)
|
|
64
59
|
|
|
65
60
|
def post(self, url, data=None, json=None, **kwargs):
|
|
@@ -73,27 +68,30 @@ class APISession(requests.Session):
|
|
|
73
68
|
:rtype: requests.Response
|
|
74
69
|
"""
|
|
75
70
|
timeout = self.timeout
|
|
76
|
-
if
|
|
77
|
-
timeout = kwargs.pop(
|
|
78
|
-
|
|
79
|
-
return self.request(
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
71
|
+
if 'timeout' in kwargs:
|
|
72
|
+
timeout = kwargs.pop('timeout')
|
|
73
|
+
|
|
74
|
+
return self.request("POST",
|
|
75
|
+
url,
|
|
76
|
+
data=data,
|
|
77
|
+
json=json,
|
|
78
|
+
timeout=timeout,
|
|
79
|
+
**kwargs)
|
|
80
|
+
|
|
81
|
+
def login(self, username:str, password:str):
|
|
82
|
+
"""
|
|
85
83
|
Authenticate connection to OpenProtein with your credentials.
|
|
86
|
-
|
|
84
|
+
|
|
87
85
|
Parameters
|
|
88
86
|
-----------
|
|
89
87
|
username: str
|
|
90
|
-
username
|
|
88
|
+
username
|
|
91
89
|
password: str
|
|
92
90
|
password
|
|
93
91
|
"""
|
|
94
92
|
self.auth = self._get_auth_token(username, password)
|
|
95
93
|
|
|
96
|
-
def _get_auth_token(self, username:
|
|
94
|
+
def _get_auth_token(self, username:str, password:str):
|
|
97
95
|
endpoint = "v1/login/access-token"
|
|
98
96
|
url = urljoin(self.backend, endpoint)
|
|
99
97
|
try:
|
|
@@ -108,9 +106,9 @@ class APISession(requests.Session):
|
|
|
108
106
|
|
|
109
107
|
result = response.json()
|
|
110
108
|
token = result.get("access_token")
|
|
111
|
-
if token is None:
|
|
109
|
+
if token is None:
|
|
112
110
|
raise AuthError("Unable to authenticate with given credentials.")
|
|
113
|
-
return BearerAuth(token)
|
|
111
|
+
return BearerAuth(token)
|
|
114
112
|
|
|
115
113
|
def request(
|
|
116
114
|
self, method: Union[str, bytes], url: Union[str, bytes], *args, **kwargs
|
openprotein/schemas/__init__.py
CHANGED
|
@@ -16,15 +16,7 @@ from .predict import (
|
|
|
16
16
|
PredictJob as WorkflowPredictJob,
|
|
17
17
|
PredictSingleSiteJob as WorkflowPredictSingleSiteJob,
|
|
18
18
|
)
|
|
19
|
-
from .design import
|
|
20
|
-
ModelCriterion,
|
|
21
|
-
Criterion,
|
|
22
|
-
DesignJobCreate,
|
|
23
|
-
DesignMetadata,
|
|
24
|
-
DesignResults,
|
|
25
|
-
DesignStep,
|
|
26
|
-
DesignJob,
|
|
27
|
-
)
|
|
19
|
+
from .design import DesignJobCreate, DesignMetadata, DesignResults, DesignJob
|
|
28
20
|
from .deprecated.poet import (
|
|
29
21
|
PoetScoreJob,
|
|
30
22
|
PoetSSPJob,
|
openprotein/schemas/align.py
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
from enum import Enum
|
|
2
2
|
from typing import Literal
|
|
3
3
|
|
|
4
|
-
from pydantic import BaseModel, Field
|
|
4
|
+
from pydantic import BaseModel, Field, model_validator
|
|
5
5
|
|
|
6
6
|
from .job import Job, JobType
|
|
7
7
|
|
|
@@ -34,16 +34,22 @@ class PromptPostParams(BaseModel):
|
|
|
34
34
|
|
|
35
35
|
|
|
36
36
|
class MSAJob(Job):
|
|
37
|
+
msa_id: str | None = None
|
|
37
38
|
job_type: Literal[JobType.align_align]
|
|
38
39
|
|
|
39
|
-
@
|
|
40
|
-
def
|
|
41
|
-
|
|
40
|
+
@model_validator(mode="before")
|
|
41
|
+
def set_msa_id(cls, values):
|
|
42
|
+
if not values.get("msa_id"):
|
|
43
|
+
values["msa_id"] = values.get("job_id")
|
|
44
|
+
return values
|
|
42
45
|
|
|
43
46
|
|
|
44
47
|
class PromptJob(MSAJob, Job):
|
|
48
|
+
prompt_id: str | None = None
|
|
45
49
|
job_type: Literal[JobType.align_prompt]
|
|
46
50
|
|
|
47
|
-
@
|
|
48
|
-
def
|
|
49
|
-
|
|
51
|
+
@model_validator(mode="before")
|
|
52
|
+
def set_prompt_id(cls, values):
|
|
53
|
+
if not values.get("prompt_id"):
|
|
54
|
+
values["prompt_id"] = values.get("job_id")
|
|
55
|
+
return values
|
openprotein/schemas/design.py
CHANGED
|
@@ -24,8 +24,8 @@ class DesignStep(BaseModel):
|
|
|
24
24
|
sequence: str
|
|
25
25
|
# scores: List[int]
|
|
26
26
|
# subscores_metadata: List[List[DesignSubscore]]
|
|
27
|
-
|
|
28
|
-
|
|
27
|
+
scores: list[float]
|
|
28
|
+
subscores_metadata: list[list[DesignSubscore]]
|
|
29
29
|
# umap1: float
|
|
30
30
|
# umap2: float
|
|
31
31
|
|
openprotein/schemas/job.py
CHANGED
|
@@ -102,19 +102,6 @@ class Job(BaseModel):
|
|
|
102
102
|
raise ValueError(f"Error parsing job from obj: {obj}: {e}")
|
|
103
103
|
return job # type: ignore - static checker cannot know runtime type
|
|
104
104
|
|
|
105
|
-
# hide extra allowed fields
|
|
106
|
-
def __repr_args__(self):
|
|
107
|
-
for k, v in self.__dict__.items():
|
|
108
|
-
field = self.model_fields.get(k)
|
|
109
|
-
if field and field.repr:
|
|
110
|
-
yield k, v
|
|
111
|
-
|
|
112
|
-
yield from (
|
|
113
|
-
(k, getattr(self, k))
|
|
114
|
-
for k, v in self.model_computed_fields.items()
|
|
115
|
-
if v.repr
|
|
116
|
-
)
|
|
117
|
-
|
|
118
105
|
# allows to carry over subclassed job fields when factory creating
|
|
119
106
|
model_config = ConfigDict(extra="allow")
|
|
120
107
|
|
openprotein/schemas/train.py
CHANGED
|
@@ -16,9 +16,9 @@ class CVItem(BaseModel):
|
|
|
16
16
|
|
|
17
17
|
class CVJob(Job):
|
|
18
18
|
job_type: Literal[JobType.workflow_crossvalidate]
|
|
19
|
-
num_rows: int
|
|
20
|
-
page_size: int
|
|
21
|
-
page_offset: int
|
|
19
|
+
num_rows: int
|
|
20
|
+
page_size: int
|
|
21
|
+
page_offset: int
|
|
22
22
|
result: list[CVItem] | None = None
|
|
23
23
|
|
|
24
24
|
|