1
1
import numpy as np
2
2
3
3
from ..arrayset import ArraysetDataReader
4
+ from ..records .hashmachine import array_hash_digest
4
5
5
6
from collections import defaultdict
6
- import hashlib
7
- from typing import Sequence , Union , Iterable , NamedTuple
8
- import struct
7
+ from typing import Sequence , Union , Iterable , NamedTuple , Tuple
9
8
10
9
11
10
# -------------------------- typehints ---------------------------------------
21
20
# ------------------------------------------------------------------------------
22
21
23
22
24
- def _calculate_hash_digest (data : np .ndarray ) -> str :
25
- hasher = hashlib .blake2b (data , digest_size = 20 )
26
- hasher .update (struct .pack (f'<{ len (data .shape )} QB' , * data .shape , data .dtype .num ))
27
- digest = hasher .hexdigest ()
28
- return digest
29
-
30
-
31
23
class FakeNumpyKeyDict (object ):
32
24
def __init__ (self , group_spec_samples , group_spec_value , group_digest_spec ):
33
25
self ._group_spec_samples = group_spec_samples
34
26
self ._group_spec_value = group_spec_value
35
27
self ._group_digest_spec = group_digest_spec
36
28
37
29
def __getitem__ (self , key : np .ndarray ) -> ArraysetSampleNames :
38
- digest = _calculate_hash_digest (key )
30
+ digest = array_hash_digest (key )
39
31
spec = self ._group_digest_spec [digest ]
40
32
samples = self ._group_spec_samples [spec ]
41
33
return samples
@@ -53,7 +45,7 @@ def __len__(self) -> int:
53
45
return len (self ._group_digest_spec )
54
46
55
47
def __contains__ (self , key : np .ndarray ) -> bool :
56
- digest = _calculate_hash_digest (key )
48
+ digest = array_hash_digest (key )
57
49
res = True if digest in self ._group_digest_spec else False
58
50
return res
59
51
@@ -69,7 +61,7 @@ def values(self) -> Iterable[ArraysetSampleNames]:
69
61
for spec in self ._group_digest_spec .values ():
70
62
yield self ._group_spec_samples [spec ]
71
63
72
- def items (self ) -> Iterable [ArraysetSampleNames ]:
64
+ def items (self ) -> Iterable [Tuple [ np . ndarray , ArraysetSampleNames ] ]:
73
65
for spec in self ._group_digest_spec .values ():
74
66
yield (self ._group_spec_value [spec ], self ._group_spec_samples [spec ])
75
67
@@ -81,11 +73,10 @@ def __repr__(self):
81
73
def _repr_pretty_ (self , p , cycle ):
82
74
res = f'Mapping: Group Data Value -> Sample Name \n '
83
75
for k , v in self .items ():
84
- res += f'\n { k } :: { v } '
76
+ res += f'\n { k } :: { v } \n '
85
77
p .text (res )
86
78
87
79
88
-
89
80
# ---------------------------- MAIN METHOD ------------------------------------
90
81
91
82
@@ -112,7 +103,7 @@ def _setup(self):
112
103
for spec , names in self ._group_spec_samples .items ():
113
104
data = self .__arrayset ._fs [spec .backend ].read_data (spec )
114
105
self ._group_spec_value [spec ] = data
115
- digest = _calculate_hash_digest (data )
106
+ digest = array_hash_digest (data )
116
107
self ._group_digest_spec [digest ] = spec
117
108
118
109
@property
0 commit comments