File size: 8,112 Bytes
2b534de
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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

import os,time
import numpy as np
from pathlib import Path
class ch_cwd_to_this_file:
    def __init__(self, _code_file_path):  # _code_file_path typically receives __file__
        self._code_file_path = _code_file_path
    def __enter__(self):
        self._old_dir = os.getcwd()
        cwd=os.path.dirname(os.path.abspath(self._code_file_path))
        os.chdir(cwd)
    def __exit__(self, exc_type, exc_val, exc_tb):
        os.chdir(self._old_dir)
# def img_2_img_full_path(img,format='jpg',original_name_or_path=''):
#     """
#     thread safe
#     """
#     assert isinstance(img,np.ndarray)
#     assert img.shape[2]==3 or img.shape[2]==4
#     original_img_name_without_dir=os.path.basename(original_name_or_path)
#     full_path = os.path.join(root_config.path_root, f'./tmp_images/[{root_config.DATASET}][{tmp_cate_or_obj}][{sequence_name}]{img_name_without_suffix}.jpg')
#     if not os.path.exists(os.path.dirname(full_path)):
#         os.makedirs(os.path.dirname(full_path))
#     print("get_data path:", full_path)
#     img.save(full_path)
#     return img_full_path

import datetime
import pytz
def beijing_datetime()->datetime.datetime:
    """
    Example: print(f'Current Beijing time = {beijing_time:%Y.%m.%d %H:%M:%S}')
    """
    # get the local timezone
    local_tz = datetime.datetime.now(datetime.timezone.utc).astimezone().tzinfo
    # get Beijing timezone
    beijing_tz = pytz.timezone('Asia/Shanghai')
    # get the current time
    now = datetime.datetime.now()
    # convert current time to local timezone
    local_time = now.astimezone(local_tz)
    # convert local time to Beijing timezone
    beijing_time:datetime.datetime = local_time.astimezone(beijing_tz)
    return beijing_time
def beijing_str_A( os_is_windows=False)->str:
    """
    print(  beijing_str_A()   )
    """
    ret= f"{beijing_datetime():%m.%d-%H:%M:%S}"
    if os_is_windows:
        ret=ret.replace(':',':')
    return ret





# convert numpy or tensor to json/dict
import json
import numpy
import PIL
import torch
from torch import Tensor


def to_list_to_primitive(obj):
    if isinstance(obj, numpy.ndarray):
        return obj.tolist()
    if isinstance(obj, torch.Tensor):
        return obj.cpu().data.numpy().tolist()
    if isinstance(obj, list):
        return [to_list_to_primitive(i) for i in obj]
    # if isinstance(obj, DataFrame):
    #     return obj.values.tolist()
    elif (isinstance(obj, numpy.int32) or
          isinstance(obj, numpy.int64) or
          isinstance(obj, numpy.float32) or
          isinstance(obj, numpy.float64)):
        return obj.item()
    elif (isinstance(obj, int) or
          isinstance(obj, float)
          ):
        return obj
    else:
        raise TypeError("got {}".format(type(obj)))
def to_ndarray(x):
    if isinstance(x, numpy.ndarray):
        return x
    if isinstance(x, torch.Tensor):
        return x.cpu().data.numpy()
    if isinstance(x, list):
        return numpy.array(x)
    if isinstance(x, PIL.Image.Image):
        return numpy.array(x)
    # if isinstance(x, int) or isinstance(x, float):
    #     return numpy.array([x])
    raise TypeError("got {}".format(type(x)))

def to_tensor(x):
    if isinstance(x, numpy.ndarray):
        return torch.from_numpy(x)
    if isinstance(x, torch.Tensor):
        return x
    if isinstance(x, PIL.Image.Image):
        return torch.from_numpy(numpy.array(x))
    if isinstance(x, list):
        return torch.tensor(x)
    # if isinstance(x, int) or isinstance(x, float):
    #     return torch.tensor([x])
    raise TypeError("got {}".format(type(x)))
def to_pil(x):
    import torch
    if isinstance(x, PIL.Image.Image):
        return x
    if isinstance(x, numpy.ndarray):
        return PIL.Image.fromarray(x)
    if isinstance(x, torch.Tensor):
        return PIL.Image.fromarray(x.cpu().data.numpy())
    raise TypeError("got {}".format(type(x)))


class myJSONEncoder(json.JSONEncoder):
    def default(self, obj):
        if isinstance(obj, numpy.ndarray):
            return obj.tolist()
        if isinstance(obj, Tensor):
            return obj.cpu().data.numpy().tolist()
        elif (isinstance(obj, numpy.int32) or
              isinstance(obj, numpy.int64) or
              isinstance(obj, numpy.float32) or
              isinstance(obj, numpy.float64)):
            return obj.item()
        elif isinstance(obj,Path):
            return str(obj)
        return json.JSONEncoder.default(self, obj)

if(__name__=="__main__"):
    import  torch
    dic = {'x': torch.randn(2, 3), 'rec': numpy.array([[11, 22, 33], [44, 55, 66], [77, 88, 99]])}
    s_dic=json.dumps(dic , cls=myJSONEncoder,
                     sort_keys=True, indent=2,
                     separators=(',', ': '), ensure_ascii=False)
    with open('test.json', 'w', encoding='utf8') as f:
        json.dump(dic,f,
                  # sort_keys=True,
                  sort_keys=False,
                  indent=2, separators=(',', ': '), ensure_ascii=False)
        
        

def truncate_str(string:str,MAX_LEN:int,suffix_if_truncate="......")->str:
    assert isinstance(string,str)
    if len(string)>  MAX_LEN:
        string=string[:MAX_LEN]+suffix_if_truncate
    return string
def map_string_to_int(string,MIN,MAX):
    """
    Map strings evenly into [MIN, MAX]
    """
    assert isinstance(MIN,int)
    assert isinstance(MAX,int)
    assert MAX-MIN>=2
    # compute ASCII sum
    sum = 0
    for char in string:
        sum += ord(char)
    # print("sum", sum)
    ret=2**sum
    ret += sum # avoid producing only powers of two
    ret=ret%(MAX-MIN)
    ret+=MIN
    return ret
if 0:
    import pprint
    def print_optimizer(optimizer):
        state_dict=optimizer.state_dict()
        param_groups=state_dict['param_groups']
        # for i,param_group in enumerate(param_groups):
        pprint.pprint(param_groups)


def dic_key_str_2_int(dic: dict) -> dict:
    ret = {}
    for k, v in dic.items():
        if isinstance(k, str) and k.isdigit():
            k = int(k)
        ret[k] = v
    return ret
def dic_key_str_2_int__nested(dic: dict) -> dict:
    ret = {}
    for k, v in dic.items():
        if isinstance(k, str) and k.isdigit():
            k = int(k)
        if isinstance(v, dict):
            v = dic_key_str_2_int__nested(v)
        ret[k] = v
    return ret
def dic_list_2_tuple_nested(dic: dict) -> dict:#if k,v is list, to tuple
    ret = {}
    for k, v in dic.items():
        if isinstance(k, list):
            k = tuple(k)
        if isinstance(v, list):
            v = tuple(v)
        if isinstance(v, dict):
            v = dic_list_2_tuple_nested(v)
        ret[k] = v
    return ret


import re

def inverse_fstring(string:str,fmt:str,):
    """
    Inverse of string format in python
    from https://stackoverflow.com/questions/48536295/inverse-of-string-format-in-python
    """
    reg_keys = '{([^{}:]+)[^{}]*}'
    reg_fmts = '{[^{}:]+[^{}]*}'
    pat_keys = re.compile(reg_keys)
    pat_fmts = re.compile(reg_fmts)

    keys = pat_keys.findall(fmt)
    lmts = pat_fmts.split(fmt)
    temp = string
    values = []
    for lmt in lmts:
        if not len(lmt)==0:
            value,temp = temp.split(lmt,1)
            if len(value)>0:
                values.append(value)
    if len(temp)>0:
        values.append(temp)
    return dict(zip(keys,values))
def sort_strings_asc_A(l:list,fmt:str)->list:
    """
    fmt: eg. 'home/frame{d}.png'
    """
    ret=sorted(l, key=  lambda s:int( inverse_fstring(s, fmt )['d'])   )
    return ret
from natsort import natsorted
def ls_natsort(folder,re_="*"):
    folder = Path(folder)
    files = list(folder.glob(re_))
    return natsorted(files )
    return natsorted(files, key=lambda x: x.name)



if __name__=='__main__':
    print(  beijing_str_A()  )
    if 1:
        fmt = '{k1:}+{k2:}={k:3}'
        res = '1+1=2'
        print (inverse_fstring(res,fmt))

        fmt = '{name:} {age:} {gender}'
        res = 'Alice 10 F'
        print (inverse_fstring(res,fmt))

        fmt = 'Hi, {k1:}, this is {k2:}'
        res = 'Hi, Alice, this is Bob'
        print (inverse_fstring(res,fmt))