File size: 7,255 Bytes
38f87b5
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
import dataclasses
import inspect
import os
import re
import shutil
from functools import partial
from typing import Dict, Counter, Tuple
import operator

import numpy as np
import requests
from funcy import rcompose, identity
from loguru import logger
from tensorflow.keras import Model, Sequential
from tensorflow.keras.layers import Layer
from tensorflow.keras.losses import Loss


def dir_decorator(fn):
    def deco(path, *args, **kwargs):
        if path is not None:
            folder = os.path.dirname(path)
            if folder:
                os.makedirs(folder, exist_ok=True)
        return fn(path, *args, **kwargs)

    return deco

def extract_args(args):
    if il(args):
        if il(args[0]):
            return tuple(args[0])
        return tuple(args)
    return tuple(lw(args))


def comp(*args):
    return rcompose(*extract_args(args))


def is_model(obj):
    return isinstance(obj, (Model, Layer, Loss))

def list_dir(root):
    for path in os.listdir(root):
        yield os.path.join(root, path)

def is_int(obj, bool_=False):
    if not bool_ and isinstance(obj, bool):
        return False
    return isinstance(obj, (int, np.integer))
    # return issubclass(type(obj),int)


def is_float(obj):
    return isinstance(obj, (float, np.float))


def is_num(obj, bool_=False):
    return is_int(obj, bool_) or is_float(obj)


def get_args(obj):
    try:
        return list(inspect.signature(obj).parameters.keys())
    except Exception as e:
        logger.error(f"{e}. Returning 1.")
        return ["arg"]


def il(data):
    return isinstance(data, (list, tuple))


def lw(data, none_empty=True, convert_tuple=True):
    if isinstance(data, list):
        return data
    elif isinstance(data, tuple) and convert_tuple:
        return list(data)
    if none_empty and data is None:
        return []
    return [data]


def dict_from_list(keys=None, values=None) -> Dict:
    if values is None:
        values = list(range(len(lw(keys))))
    elif callable(values):
        values = [values(k) for k in keys]
    if keys is None:
        keys = list(range(len(lw(values))))
    elif callable(keys):
        keys = [keys(v) for v in values]
    if not isinstance(values, dict):
        values = dict(zip(keys[:len(lw(values))], lw(values)))
    return values


def call(fn, x=None):
    if len(get_args(fn)) > 0:
        if isinstance(x, dict):
            return fn(**x)
        elif il(x):
            return fn(*x)
        else:
            return fn(x)
    else:
        return fn()


def get_str_comp(str_comp):
    if callable(str_comp):
        return str_comp
    elif isinstance(str_comp, str):
        if str_comp == "in":
            return lambda x, y: x in y
        elif str_comp in ["equal", "="]:
            return lambda x, y: x == y
        elif str_comp == "re":
            return lambda x, y: re.match(x, y)
    return lambda x, y: x == y


def test(filters, x=None, str_comp="in"):
    str_comp = get_str_comp(str_comp)
    result = []
    for f in lw(filters):
        if isinstance(f, str):
            result.append(str_comp(f, x))
        elif callable(f):
            result.append(call(f, x))
        else:
            result.append(f == x)
    return result


test_all = comp([test, all])
test_any = comp([test, any])


def filter_keys(d, keys, reverse=False, str_comp="=", *args, **kwargs):
    fn = test_any
    if dataclasses.is_dataclass(keys):
        keys = dataclasses.asdict(keys)
    if isinstance(keys, dict):
        keys = list(keys.keys())
    if dataclasses.is_dataclass(d):
        d = dataclasses.asdict(d)
    if reverse:
        fn = comp(test_any, lambda x: not x)
    return {k: v for k, v in d.items() if fn(keys, k, str_comp=str_comp, *args, **kwargs)}
def none(x):
    return [] if x is None else x

def if_images(data):
    if not hasattr(data, "shape"):
        return False
    shape = data.shape
    return len(shape) > 3 or (len(shape) == 3 and shape[-1] > 4)


def download_file(url, path=None):
    if path is None:
        path = url.split("/")[-1]

    r = requests.get(url, stream=True)

    if r.status_code == 200:
        r.raw.decode_content = True

        with open(path, 'wb') as f:
            shutil.copyfileobj(r.raw, f)

        logger.info(f'File {url} sucessfully downloaded to {path} ')
    else:
        logger.info(f'File {url} couldn\'t be retreived')

@dataclasses.dataclass
class ImageType:
    mode: str = "RGB"
    range_: Tuple = (0, 255)
    channel: bool = True
    reverse: bool = False
    shape: bool = (None, 224, 224, 3)
    dtype: str = "uint8"
    counter: Counter = None

    def __eq__(self, other):
        return filter_keys(dataclasses.asdict(self), "counter", reverse=True) == filter_keys(dataclasses.asdict(other), "counter", reverse=True)


def image_type(data, histogram=True):
    data = np.asarray(data)
    if not if_images(data):
        return False
    shape = data.shape
    channel = True
    range_ = data.min(), data.max()
    counter = None
    reverse = None
    if shape[-1] == 3:
        mode = "RGB"
        if histogram:
            counter = Counter(list(get_last_dim(data, 3).reshape(-1)))
            reverse = counter.most_common(1)[0][0] == range_[1]
    else:
        mode = "L"
        if shape[-1] != 1:
            channel = False
            if histogram:
                counter = Counter(list(get_last_dim(data).reshape(-1)))
                reverse = counter.most_common(1)[0][0] == range_[1]
        else:
            if histogram:
                counter = Counter(list(get_last_dim(data, 3).reshape(-1)))
                reverse = counter.most_common(1)[0][0] == range_[1]
    return ImageType(mode, range_, channel, reverse, shape, data.dtype, counter)

timage = partial(image_type, histogram=False)

class OverrideDict(dict):
    def to_dict(self):
        # return {k: v for k, v in self.items()}
        return dict(self)

    def get_fn(self, index):
        return self.__getitem__(index)


class CalcDict(OverrideDict):
    def operate(self, other, op, right=False):
        if not hasattr(other, "__iter__"):
            other = [other] * len(self)
        if isinstance(other, dict):
            other = other.values()
        it = iter(other)
        if right:
            fn = lambda x, y: op(y, x)
        else:
            fn = op
        return CalcDict({k: fn(v, next(it)) for k, v in self.items()})

    def __add__(self, other):
        return self.operate(other, operator.add)

    def __sub__(self, other):
        return self.operate(other, operator.sub)

    def __mul__(self, other):
        return self.operate(other, operator.mul)

    def __truediv__(self, other):
        return self.operate(other, operator.truediv)

    def __radd__(self, other):
        return self.operate(other, operator.add, right=True)

    def __rsub__(self, other):
        return self.operate(other, operator.sub, right=True)

    def __rmul__(self, other):
        return self.operate(other, operator.mul, right=True)

    def __rtruediv__(self, other):
        return self.operate(other, operator.truediv, right=True)


def dict_from_list2(keys, values=None):
    if values is None:
        values = list(range(len(keys)))
    return dict(zip(keys[:len(lw(values))], lw(values)))