Passed
Push — main ( 83a9fb...fa90c4 )
by Douglas
03:43
created

mandos.model.hit_dfs._to_hits()   A

Complexity

Conditions 2

Size

Total Lines 11
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 2
eloc 8
nop 1
dl 0
loc 11
rs 10
c 0
b 0
f 0
1
from typing import Sequence
0 ignored issues
show
introduced by
Missing module docstring
Loading history...
2
3
import numpy as np
0 ignored issues
show
introduced by
Unable to import 'numpy'
Loading history...
4
import pandas as pd
0 ignored issues
show
introduced by
Unable to import 'pandas'
Loading history...
5
from typeddfs import TypedDfs
0 ignored issues
show
introduced by
Unable to import 'typeddfs'
Loading history...
6
from typeddfs.abs_dfs import AbsDf
0 ignored issues
show
introduced by
Unable to import 'typeddfs.abs_dfs'
Loading history...
7
8
from mandos.model.concrete_hits import HIT_CLASSES
9
from mandos.model.hits import AbstractHit
10
11
12
def _from_hits(cls, hits: Sequence[AbstractHit]) -> AbsDf:
13
    data = []
14
    for hit in hits:
15
        x = {f: getattr(hit, f) for f in hit.__class__.fields()}
0 ignored issues
show
Coding Style Naming introduced by
Variable name "x" doesn't conform to snake_case naming style ('([^\\W\\dA-Z][^\\WA-Z]2,|_[^\\WA-Z]*|__[^\\WA-Z\\d_][^\\WA-Z]+__)$' pattern)

This check looks for invalid names for a range of different identifiers.

You can set regular expressions to which the identifiers must conform if the defaults do not match your requirements.

If your project includes a Pylint configuration file, the settings contained in that file take precedence.

To find out more about Pylint, please refer to their site.

Loading history...
16
        x["universal_id"] = hit.universal_id
17
        x["hit_class"] = hit.hit_class
18
        data.append(x)
19
    return cls([pd.Series(x) for x in data])
20
21
22
def _to_hits(self: AbsDf) -> Sequence[AbstractHit]:
23
    hits = []
24
    for row in self.itertuples():
25
        clazz = HIT_CLASSES[row.hit_class]
26
        # ignore extra columns
27
        # if cols are missing, let it fail on clazz.__init__
28
        data = {f: getattr(row, f) for f in clazz.fields()}
29
        # noinspection PyArgumentList
30
        hit = clazz(**data)
31
        hits.append(hit)
32
    return hits
33
34
35
HitDf = (
36
    TypedDfs.typed("HitDf")
37
    .require("record_id", dtype=str)
38
    .require("origin_inchikey", "matched_inchikey", dtype=str)
39
    .require("predicate", dtype=str)
40
    .require("object_id", "object_name", dtype=str)
41
    .require("search_key", "search_class", "data_source", dtype=str)
42
    .require("hit_class", dtype=str)
43
    .require("cache_date", "run_date")
44
    .reserve("inchi", "smiles", dtype=str)
45
    .reserve("compound_id", "compound_name", dtype=str)
46
    .reserve("chembl_id", "pubchem_id", dtype=str)
47
    .reserve("weight", dtype=np.float64)
48
    .add_classmethods(from_hits=_from_hits)
49
    .add_methods(to_hits=_to_hits)
50
    .strict(cols=False)
51
    .secure()
52
).build()
53
54
55
__all__ = ["HitDf"]
56