Skip to content

Commit 4e759b9

Browse files
committed
_decoding tests
1 parent 20b29e0 commit 4e759b9

File tree

5 files changed

+34
-29
lines changed

5 files changed

+34
-29
lines changed

‎ciftools/bin/data.py‎

Lines changed: 16 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
fromtypingimportDict, List, Optional, Union
1+
fromtypingimportAny, Dict, List, Optional, Union
22

33
importnumpyasnp
44
fromciftools.bin.decoderimportdecode_cif_data
@@ -11,11 +11,11 @@ def __init__(
1111
self,
1212
name: str,
1313
values: Union[np.ndarray, list[str]],
14-
value_kinds: Union[np.ndarray, None],
14+
value_mask: Union[np.ndarray, None],
1515
):
1616
self.name=name
1717
self._values=values
18-
self._value_kinds=value_kinds
18+
self._value_mask=value_mask
1919
self._row_count=len(values)
2020

2121
defget_string(self, row: int) ->str:
@@ -28,8 +28,8 @@ def get_float(self, row: int) -> float:
2828
returnfloat(self._values[row])
2929

3030
defget_value_presence(self, row: int) ->CIFValuePresenceEnum:
31-
ifself._value_kinds:
32-
returnself._value_kinds[row]
31+
ifself._value_mask:
32+
returnself._value_mask[row]
3333
return0
3434

3535
defare_values_equal(self, row_a: int, row_b: int) ->bool:
@@ -49,19 +49,23 @@ def as_ndarray(
4949
returnself._values[start:end]
5050
returnself._values[start:end].astype(dtype)
5151

52-
def__getitem__(self, idx: int) ->Union[str, float, int, None]:
53-
ifself._value_kindsandself._value_kinds[idx]:
52+
def__getitem__(self, idx: Any) ->Any:
53+
ifisinstance(idx, int) andself._value_maskandself._value_mask[idx]:
5454
returnNone
5555
returnself._values[idx]
5656

5757
def__len__(self):
5858
returnself._row_count
5959

60+
@property
61+
defvalue_presences(self) ->Optional[np.ndarray]:
62+
returnself._value_mask
63+
6064

6165
def_decode_cif_column(column: EncodedCIFColumn) ->CIFColumn:
6266
values=decode_cif_data(column["data"])
63-
value_kinds=decode_cif_data(column["mask"]) ifcolumn["mask"] elseNone
64-
returnBinaryCIFColumn(column["name"], values, value_kinds)
67+
value_mask=decode_cif_data(column["mask"]) ifcolumn["mask"] elseNone
68+
returnBinaryCIFColumn(column["name"], values, value_mask)
6569

6670

6771
classBinaryCIFCategory(CIFCategory):
@@ -100,7 +104,7 @@ def n_columns(self) -> int:
100104
returnlen
101105

102106
@property
103-
defcolumn_names(self) ->list[str]:
107+
deffield_names(self) ->list[str]:
104108
returnself._field_names
105109

106110

@@ -156,8 +160,8 @@ def from_data(data: EncodedCIFFile, *, lazy=True) -> "BinaryCIFFile":
156160
- False: decode all columns immediately
157161
"""
158162

159-
min_version=[0, 3, 0]
160-
version=map(int, data["version"].split("."))
163+
min_version=(0, 3, 0)
164+
version=tuple(map(int, data["version"].split(".")))
161165
ifversion<min_version:
162166
raiseValueError(f"Invalid version {data['version']}, expected >={'.'.join(map(str, min_version))}")
163167

‎ciftools/models/data.py‎

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
fromenumimportIntEnum
2-
fromtypingimportDict, List, Optional, Protocol, Union
2+
fromtypingimportAny, Dict, List, Optional, Protocol, Union
33

44
importnumpyasnp
55

@@ -34,7 +34,7 @@ def as_ndarray(
3434
) ->np.ndarray:
3535
...
3636

37-
def__getitem__(self, idx: int) ->Union[str, float, int, None]:
37+
def__getitem__(self, idx: Any) ->Any:
3838
...
3939

4040
def__len__(self) ->int:
@@ -62,7 +62,7 @@ def n_columns(self) -> int:
6262
...
6363

6464
@property
65-
defcolumn_names(self) ->List[str]:
65+
deffield_names(self) ->List[str]:
6666
...
6767

6868
def__getattr__(self, name: str) ->Optional[CIFColumn]:

‎pyproject.toml‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
[project]
2-
name = "CifTools"
2+
name = "ciftools"
33
version = "0.2.0"
44
authors = [
55
{name="Ravi Jose Tristao Ramos", email="[email protected]" },

‎tests/_decoding.py‎

Lines changed: 13 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,8 @@
22
importurllib.request
33

44
importmsgpack
5-
fromciftools.cif_format.binary.fileimportBinaryCIFFile
6-
fromciftools.cif_format.value_presenceimportValuePresenceEnum
5+
fromciftools.serializationimportloads
6+
fromciftools.models.dataimportCIFValuePresenceEnum
77

88

99
classTestEncodings_Decoding(unittest.TestCase):
@@ -12,7 +12,7 @@ def test(self):
1212

1313
print("mmCIF test")
1414
data=urllib.request.urlopen("https://models.rcsb.org/1tqn.bcif").read()
15-
parsed=BinaryCIFFile.loads(data, lazy=False)
15+
parsed=loads(data, lazy=False)
1616

1717
atom_site=parsed["1TQN"].atom_site
1818
entity=parsed[0]["entity"]
@@ -24,22 +24,23 @@ def test(self):
2424
print(entity.field_names)
2525
print(atom_site.rowCount)
2626
print(label_comp_id[0])
27-
print(label_comp_id.values[-1])
28-
print(len(label_comp_id.values))
27+
print(label_comp_id[-1])
2928
print(len(label_comp_id))
30-
print(cartn_x.values[0:10])
31-
print(atom_site["label_alt_id"].value_kinds[0] ==ValuePresenceEnum.NotSpecified)
29+
print(cartn_x[0:10])
30+
value_slice=cartn_x.as_ndarray(start=1, end=10, dtype='i4')
31+
print(value_slice)
32+
print(atom_site["label_alt_id"].value_presences[0] ==CIFValuePresenceEnum.NotSpecified)
3233
# print([[f"_{c.name}.{f}" for f in c.field_names] for c in parsed[0].categories.values()])
3334

3435
print("Volume Data test")
3536
data=urllib.request.urlopen(
3637
"https://ds.litemol.org/x-ray/1tqn/box/-22.367,-33.367,-21.634/-7.106,-10.042,-0.937?detail=1"
3738
).read()
38-
parsed=BinaryCIFFile.loads(msgpack.loads(data))
39+
parsed=loads(data)
3940

4041
print([b.headerforbinparsed.data_blocks])
41-
print(parsed[1].categories().keys())
42-
print(parsed["FO-FC"].categories().keys())
42+
print(parsed[1].categories.keys())
43+
print(parsed["FO-FC"].categories.keys())
4344
print(parsed[1]["volume_data_3d_info"].field_names)
44-
print(parsed[1]["volume_data_3d"].rowCount)
45-
print(parsed[1]["volume_data_3d"]["values"].values[0:10])
45+
print(parsed[1]["volume_data_3d"].n_rows)
46+
print(parsed[1]["volume_data_3d"]["values"][0:10])

‎tests/_run_all_tests.py‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
"interval_quantization",
99
"run_length",
1010
"string_array",
11-
# "_decoding",
11+
"_decoding",
1212
# "_encoding",
1313
]
1414

0 commit comments

Comments
(0)