Skip to content

Commit 333b7d0

Browse files
Library temporary fix (by markmipt)
1 parent c4c22aa commit 333b7d0

12 files changed

Lines changed: 65 additions & 14 deletions

CHANGELOG.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,9 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
55
and this project adheres to
66
[Semantic Versioning](https://semver.org/spec/v2.0.0.html).
77

8+
## [0.1.32] - 2021-07-29
9+
- Temporary fix suggested by markmipt for the library
10+
811
## [0.1.31] - 2021-07-01
912
- Change logging to specified logger object instead of standard logger
1013

deeplc/deeplc.py

Lines changed: 15 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -142,7 +142,8 @@ class DeepLC():
142142
Make predictions
143143
144144
"""
145-
145+
library = {}
146+
146147
def __init__(self,
147148
main_path=os.path.dirname(os.path.realpath(__file__)),
148149
path_model=None,
@@ -189,7 +190,6 @@ def __init__(self,
189190

190191
self.use_library = use_library
191192
self.write_library = write_library
192-
self.library = {}
193193

194194
if self.use_library:
195195
self.read_library()
@@ -243,7 +243,7 @@ def read_library(self):
243243
for line_num,line in enumerate(library_file):
244244
split_line = line.strip().split(",")
245245
try:
246-
self.library[split_line[0]] = float(split_line[1])
246+
DeepLC.library[split_line[0]] = float(split_line[1])
247247
except:
248248
logger.warning(
249249
"Could not use this library entry due to an error: %s", line
@@ -433,7 +433,7 @@ def make_preds_core(self,
433433
all_mods = [m_name for m_group_name,m_name in self.model.items()]
434434

435435
# TODO check if .keys() object is the same as set (or at least for set operations)
436-
idents_in_lib = set(self.library.keys())
436+
idents_in_lib = set(DeepLC.library.keys())
437437

438438
if self.use_library:
439439
for ident in seq_df["idents"]:
@@ -479,6 +479,7 @@ def make_preds_core(self,
479479
cnn_verbose = 0
480480

481481
# If we need to apply deep NN
482+
del self.library
482483
if len(seq_df.index) > 0:
483484
if self.cnn_model:
484485
if self.verbose:
@@ -504,6 +505,10 @@ def make_preds_core(self,
504505

505506
ret_preds = []
506507
ret_preds2 = []
508+
509+
self.library = {}
510+
if self.use_library:
511+
self.read_library()
507512

508513
# If we need to calibrate
509514
if calibrate:
@@ -554,7 +559,7 @@ def make_preds_core(self,
554559
p = list(self.calibration_core(uncal_preds,self.calibrate_dict[m_name],self.calibrate_min[m_name],self.calibrate_max[m_name]))
555560
ret_preds.append(p)
556561

557-
p2 = list(self.calibration_core([self.library[ri+"|"+m_name] for ri in rem_idents],self.calibrate_dict[m_name],self.calibrate_min[m_name],self.calibrate_max[m_name]))
562+
p2 = list(self.calibration_core([DeepLC.library[ri+"|"+m_name] for ri in rem_idents],self.calibrate_dict[m_name],self.calibrate_min[m_name],self.calibrate_max[m_name]))
558563
ret_preds2.append(p2)
559564

560565
ret_preds = np.array([sum(a)/len(a) for a in zip(*ret_preds)])
@@ -597,7 +602,7 @@ def make_preds_core(self,
597602

598603
ret_preds = self.calibration_core(uncal_preds,self.calibrate_dict,self.calibrate_min,self.calibrate_max)
599604

600-
p2 = list(self.calibration_core([self.library[ri+"|"+mod_name] for ri in rem_idents],self.calibrate_dict,self.calibrate_min,self.calibrate_max))
605+
p2 = list(self.calibration_core([DeepLC.library[ri+"|"+mod_name] for ri in rem_idents],self.calibrate_dict,self.calibrate_min,self.calibrate_max))
601606
ret_preds2.extend(p2)
602607
else:
603608
# first get uncalibrated prediction
@@ -649,7 +654,7 @@ def make_preds_core(self,
649654
lib_file.close()
650655
if self.reload_library: self.read_library()
651656

652-
p2 = [self.library[ri+"|"+m_name] for ri in rem_idents]
657+
p2 = [DeepLC.library[ri+"|"+m_name] for ri in rem_idents]
653658
ret_preds2.append(p2)
654659

655660
ret_preds = np.array([sum(a)/len(a) for a in zip(*ret_preds)])
@@ -677,7 +682,7 @@ def make_preds_core(self,
677682
lib_file.close()
678683
if self.reload_library: self.read_library()
679684

680-
ret_preds2 = np.array([self.library[ri+"|"+mod_name] for ri in rem_idents])
685+
ret_preds2 = np.array([DeepLC.library[ri+"|"+mod_name] for ri in rem_idents])
681686
elif isinstance(self.model, str):
682687
# No library write!
683688
mod_name = self.model
@@ -703,7 +708,7 @@ def make_preds_core(self,
703708
lib_file.close()
704709
if self.reload_library: self.read_library()
705710

706-
ret_preds2 = np.array([self.library[ri+"|"+mod_name] for ri in rem_idents])
711+
ret_preds2 = np.array([DeepLC.library[ri+"|"+mod_name] for ri in rem_idents])
707712
else:
708713
raise DeepLCError('No CNN model defined.')
709714
else:
@@ -732,7 +737,7 @@ def make_preds_core(self,
732737
if self.reload_library: self.read_library()
733738
except:
734739
pass
735-
ret_preds2 = [self.library[ri+"|"+mod_name] for ri in rem_idents]
740+
ret_preds2 = [DeepLC.library[ri+"|"+mod_name] for ri in rem_idents]
736741

737742
else:
738743
# No library write!

deeplc/models/full_hc_PXD005573_mcp_cb975cfdd4105f97efa0b3afffe075cc.hdf5 renamed to deeplc/mods/full_hc_PXD005573_mcp_cb975cfdd4105f97efa0b3afffe075cc.hdf5

File renamed without changes.

deeplc/models/full_hc_hela_hf_psms_aligned_1fd8363d9af9dcad3be7553c39396960.hdf5 renamed to deeplc/mods/full_hc_hela_hf_psms_aligned_1fd8363d9af9dcad3be7553c39396960.hdf5

File renamed without changes.

deeplc/models/full_hc_hela_hf_psms_aligned_8c22d89667368f2f02ad996469ba157e.hdf5 renamed to deeplc/mods/full_hc_hela_hf_psms_aligned_8c22d89667368f2f02ad996469ba157e.hdf5

File renamed without changes.

deeplc/models/full_hc_hela_hf_psms_aligned_cb975cfdd4105f97efa0b3afffe075cc.hdf5 renamed to deeplc/mods/full_hc_hela_hf_psms_aligned_cb975cfdd4105f97efa0b3afffe075cc.hdf5

File renamed without changes.

deeplc/models/full_hc_train_pxd001468_1fd8363d9af9dcad3be7553c39396960.hdf5 renamed to deeplc/mods/full_hc_train_pxd001468_1fd8363d9af9dcad3be7553c39396960.hdf5

File renamed without changes.

deeplc/models/full_hc_train_pxd001468_8c22d89667368f2f02ad996469ba157e.hdf5 renamed to deeplc/mods/full_hc_train_pxd001468_8c22d89667368f2f02ad996469ba157e.hdf5

File renamed without changes.

deeplc/models/full_hc_train_pxd001468_cb975cfdd4105f97efa0b3afffe075cc.hdf5 renamed to deeplc/mods/full_hc_train_pxd001468_cb975cfdd4105f97efa0b3afffe075cc.hdf5

File renamed without changes.

deeplc/test_deeplc.py

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
"Unit and integration tests for DeepLC."
2+
3+
# Standard library
4+
import logging
5+
import pytest
6+
import subprocess
7+
8+
# Third party
9+
import pandas as pd
10+
from sklearn.metrics import r2_score
11+
12+
# DeepLC
13+
import deeplc
14+
15+
16+
def test_cli_basic():
17+
""" Test command line interface help message. """
18+
assert subprocess.getstatusoutput('deeplc -h')[0] == 0, "`deeplc -h` \
19+
returned non-zero status code"
20+
21+
22+
def test_cli_full():
23+
"""" Test command line interface with input files."""
24+
file_path_pred = "examples/datasets/test_train.csv"
25+
file_path_cal = "examples/datasets/test_train.csv"
26+
file_path_out = "pytest_cli_out.csv"
27+
28+
command = [
29+
"deeplc", "--file_pred", file_path_pred, "--file_cal", file_path_cal,
30+
"--file_pred_out", file_path_out
31+
]
32+
subprocess.run(command, check=True)
33+
34+
preds_df = pd.read_csv(file_path_out)
35+
model_r2 = r2_score(preds_df['tr'], preds_df['predicted_tr'])
36+
logging.info("DeepLC R2 score on %s: %f", file_path_pred, model_r2)
37+
assert model_r2 > 0.95, f"DeepLC R2 score on {file_path_pred} below 0.95 \
38+
(was {model_r2})"
39+
40+
41+
if __name__ == "__main__":
42+
pytest.main()

0 commit comments

Comments
 (0)