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 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
from typing import Any, Optional, Union
from pathlib import Path
import os
import io
import lmdb
import pickle
import gzip
import bz2
import lzma
import shutil
from tqdm import tqdm
import pandas as pd 
import numpy as np
from numpy import ndarray
import time
import torch
from torch import Tensor
from distutils.dir_util import copy_tree
from PIL import Image
from PIL import ImageFile

ImageFile.LOAD_TRUNCATED_IMAGES = True


def _default_encode(data: Any, protocol: int) -> bytes:
    return pickle.dumps(data, protocol=protocol)


def _ascii_encode(data: str) -> bytes:
    return data.encode("ascii")


def _default_decode(data: bytes) -> Any:
    return pickle.loads(data)


def _default_decompress(data: bytes) -> bytes:
    return data


def _decompress(compression: Optional[str]):
    if compression is None:
        _decompress = _default_decompress
    elif compression == "gzip":
        _decompress = gzip.decompress
    elif compression == "bz2":
        _decompress = bz2.decompress
    elif compression == "lzma":
        _decompress = lzma.decompress
    else:
        raise ValueError(f"Unknown compression algorithm: {compression}")

    return _decompress


class BaseLMDB(object):
    _database = None
    _protocol = None
    _length = None

    def __init__(
        self,
        path: Union[str, Path],
        readahead: bool = False,
        pre_open: bool = False,
        compression: Optional[str] = None
    ):
        """
        Base class for LMDB-backed databases.

        :param path: Path to the database.
        :param readahead: Enables the filesystem readahead mechanism.
        :param pre_open: If set to True, the first iterations will be faster, but it will raise error when doing multi-gpu training. If set to False, the database will open when you will retrieve the first item.
        """
        if not isinstance(path, str):
            path = str(path)

        self.path = path
        self.readahead = readahead
        self.pre_open = pre_open
        self._decompress = _decompress(compression)
        self._has_fetched_an_item = False

    @property
    def database(self):
        if self._database is None:
            self._database = lmdb.open(
                path=self.path,
                readonly=True,
                readahead=self.readahead,
                max_spare_txns=256,
                lock=False,
            )
        return self._database

    @database.deleter
    def database(self):
        if self._database is not None:
            self._database.close()
            self._database = None

    @property
    def protocol(self):
        """
        Read the pickle protocol contained in the database.

        :return: The set of available keys.
        """
        if self._protocol is None:
            self._protocol = self._get(
                item="protocol",
                encode_key=_ascii_encode,
                decompress_value=_default_decompress,
                decode_value=_default_decode,
            )
        return self._protocol

    @property
    def keys(self):
        """
        Read the keys contained in the database.

        :return: The set of available keys.
        """
        protocol = self.protocol
        keys = self._get(
            item="keys",
            encode_key=lambda key: _default_encode(key, protocol=protocol),
            decompress_value=_default_decompress,
            decode_value=_default_decode,
        )
        return keys

    def __len__(self):
        """
        Returns the number of keys available in the database.

        :return: The number of keys.
        """
        if self._length is None:
            self._length = len(self.keys)
        return self._length

    def __getitem__(self, item):
        """
        Retrieves an item or a list of items from the database.

        :param item: A key or a list of keys.
        :return: A value or a list of values.
        """
        self._has_fetched_an_item = True
        if not isinstance(item, list):
            item = self._get(
                item=item,
                encode_key=self._encode_key,
                decompress_value=self._decompress_value,
                decode_value=self._decode_value,
            )
        else:
            item = self._gets(
                items=item,
                encode_keys=self._encode_keys,
                decompress_values=self._decompress_values,
                decode_values=self._decode_values,
            )
        return item

    def _get(self, item, encode_key, decompress_value, decode_value):
        """
        Instantiates a transaction and its associated cursor to fetch an item.

        :param item: A key.
        :param encode_key:
        :param decode_value:
        :return:
        """
        with self.database.begin() as txn:
            with txn.cursor() as cursor:
                item = self._fetch(
                    cursor=cursor,
                    key=item,
                    encode_key=encode_key,
                    decompress_value=decompress_value,
                    decode_value=decode_value,
                )
        self._keep_database()
        return item

    def _gets(self, items, encode_keys, decompress_values, decode_values):
        """
        Instantiates a transaction and its associated cursor to fetch a list of items.

        :param items: A list of keys.
        :param encode_keys:
        :param decode_values:
        :return:
        """
        with self.database.begin() as txn:
            with txn.cursor() as cursor:
                items = self._fetchs(
                    cursor=cursor,
                    keys=items,
                    encode_keys=encode_keys,
                    decompress_values=decompress_values,
                    decode_values=decode_values,
                )
        self._keep_database()
        return items

    def _fetch(self, cursor, key, encode_key, decompress_value, decode_value):
        """
        Retrieve a value given a key.

        :param cursor:
        :param key: A key.
        :param encode_key:
        :param decode_value:
        :return: A value.
        """
        key = encode_key(key)
        value = cursor.get(key)
        value = decompress_value(value)
        value = decode_value(value)
        return value

    def _fetchs(self, cursor, keys, encode_keys, decompress_values, decode_values):
        """
        Retrieve a list of values given a list of keys.

        :param cursor:
        :param keys: A list of keys.
        :param encode_keys:
        :param decode_values:
        :return: A list of values.
        """
        keys = encode_keys(keys)
        _, values = list(zip(*cursor.getmulti(keys)))
        values = decompress_values(values)
        values = decode_values(values)
        return values

    def _encode_key(self, key: Any) -> bytes:
        """
        Converts a key into a byte key.

        :param key: A key.
        :return: A byte key.
        """
        return pickle.dumps(key, protocol=self.protocol)

    def _encode_keys(self, keys: list) -> list:
        """
        Converts keys into byte keys.

        :param keys: A list of keys.
        :return: A list of byte keys.
        """
        return [self._encode_key(key=key) for key in keys]

    def _decompress_value(self, value: bytes) -> bytes:
        return self._decompress(value)

    def _decompress_values(self, values: list) -> list:
        return [self._decompress_value(value=value) for value in values]

    def _decode_value(self, value: bytes) -> Any:
        """
        Converts a byte value back into a value.

        :param value: A byte value.
        :return: A value
        """
        return pickle.loads(value)

    def _decode_values(self, values: list) -> list:
        """
        Converts bytes values back into values.

        :param values: A list of byte values.
        :return: A list of values.
        """
        return [self._decode_value(value=value) for value in values]

    def _keep_database(self):
        """
        Checks if the database must be deleted.

        :return:
        """
        if not self.pre_open and not self._has_fetched_an_item:
            del self.database

    def __iter__(self):
        """
        Provides an iterator over the keys when iterating over the database.

        :return: An iterator on the keys.
        """
        return iter(self.keys)

    def __del__(self):
        """
        Closes the database properly.
        """
        del self.database

    @staticmethod
    def write(data_lst, indir, outdir):
        raise NotImplementedError


class PILlmdb(BaseLMDB):
    def __init__(
        self,
        lmdb_dir: Union[str, Path],
        image_list: Union[str, Path, pd.DataFrame]=None,
        index_key='id',
        **kwargs
    ):
        super().__init__(path=lmdb_dir, **kwargs)
        if image_list is None:
            self.ids = list(range(len(self.keys)))
            self.labels = list(range(len(self.ids)))
        else:
            df = pd.read_csv(str(image_list))
            assert index_key in df, f'[PILlmdb] Error! {image_list} must have id keys.'
            self.ids = df[index_key].tolist()
            assert max(self.ids) < len(self.keys)
            if 'label' in df:
                self.labels = df['label'].tolist()
            else:  # all numeric keys other than 'id' are labels
                keys = [key for key in df if (key!=index_key and type(df[key][0]) in [int, np.int64])]
                # df = df.drop('id', axis=1)
                self.labels = df[keys].to_numpy()
        self._length = len(self.ids)

    def __len__(self):
        return self._length

    def __iter__(self):
        return iter([self.keys[i] for i in self.ids])

    def __getitem__(self, index):
        key = self.keys[self.ids[index]]
        return super().__getitem__(key)

    def set_ids(self, ids):
        self.ids = [self.ids[i] for i in ids]
        self.labels = [self.labels[i] for i in ids]
        self._length = len(self.ids)
        
    def _decode_value(self, value: bytes):
        """
        Converts a byte image back into a PIL Image.

        :param value: A byte image.
        :return: A PIL Image image.
        """
        return Image.open(io.BytesIO(value))

    @staticmethod
    def write(indir, outdir, data_lst=None, transform=None):
        """
        create lmdb given data directory and list of image paths; or an iterator
        :param data_lst None or csv file containing 'path' key to store relative paths to the images
        :param indir root directory of the images
        :param outdir output lmdb, data.mdb and lock.mdb will be written here
        """
        
        outdir = Path(outdir)
        outdir.mkdir(parents=True, exist_ok=True)
        tmp_dir = Path("/tmp") / f"TEMP_{time.time()}"
        tmp_dir.mkdir(parents=True, exist_ok=True)
        dtype = {'str': False, 'pil': False}
        if isinstance(indir, str) or isinstance(indir, Path):
            indir = Path(indir)
            if data_lst is None:  # grab all images in this dir
                lst = list(indir.glob('**/*.jpg')) + list(indir.glob('**/*.png'))
            else:
                lst = pd.read_csv(data_lst)['path'].tolist()
                lst = [indir/p for p in lst]
            assert len(lst) > 0, f'Couldnt find any image in {indir} (Support only .jpg and .png) or list (must have path field).'
            n = len(lst)
            dtype['str'] = True 
        else:  # iterator
            n = len(indir)
            lst = iter(indir)
            dtype['pil'] = True 

        with lmdb.open(path=str(tmp_dir), map_size=2 ** 40) as env:
            # Add the protocol to the database.
            with env.begin(write=True) as txn:
                key = "protocol".encode("ascii")
                value = pickle.dumps(pickle.DEFAULT_PROTOCOL)
                txn.put(key=key, value=value, dupdata=False)
            # Add the keys to the database.
            with env.begin(write=True) as txn:
                key = pickle.dumps("keys")
                value = pickle.dumps(list(range(n)))
                txn.put(key=key, value=value, dupdata=False)
            # Add the images to the database.
            for key, value in tqdm(enumerate(lst), total=n, miniters=n//100, mininterval=300):
                with env.begin(write=True) as txn:
                    key = pickle.dumps(key)
                    if dtype['str']:
                        with value.open("rb") as file:
                            byteimg = file.read()
                    else:  # PIL
                        data = io.BytesIO()
                        value.save(data, 'png')
                        byteimg = data.getvalue()

                    if transform is not None:
                        im = Image.open(io.BytesIO(byteimg))
                        im = transform(im)
                        data = io.BytesIO()
                        im.save(data, 'png')
                        byteimg = data.getvalue()
                    txn.put(key=key, value=byteimg, dupdata=False)

        # Move the database to its destination.
        copy_tree(str(tmp_dir), str(outdir))
        shutil.rmtree(str(tmp_dir))



class MaskDatabase(PILlmdb):
    def _decode_value(self, value: bytes):
        """
        Converts a byte image back into a PIL Image.

        :param value: A byte image.
        :return: A PIL Image image.
        """
        return Image.open(io.BytesIO(value)).convert("1")


class LabelDatabase(BaseLMDB):
    pass


class ArrayDatabase(BaseLMDB):
    _dtype = None
    _shape = None

    def __init__(
        self,
        lmdb_dir: Union[str, Path],
        image_list: Union[str, Path, pd.DataFrame]=None,
        **kwargs
    ):
        super().__init__(path=lmdb_dir, **kwargs)
        if image_list is None:
            self.ids = list(range(len(self.keys)))
            self.labels = list(range(len(self.ids)))
        else:
            df = pd.read_csv(str(image_list))
            assert 'id' in df, f'[ArrayDatabase] Error! {image_list} must have id keys.'
            self.ids = df['id'].tolist()
            assert max(self.ids) < len(self.keys)
            if 'label' in df:
                self.labels = df['label'].tolist()
            else:  # all numeric keys other than 'id' are labels
                keys = [key for key in df if (key!='id' and type(df[key][0]) in [int, np.int64])]
                # df = df.drop('id', axis=1)
                self.labels = df[keys].to_numpy()
        self._length = len(self.ids)

    def set_ids(self, ids):
        self.ids = [self.ids[i] for i in ids]
        self.labels = [self.labels[i] for i in ids]
        self._length = len(self.ids)

    def __len__(self):
        return self._length

    def __iter__(self):
        return iter([self.keys[i] for i in self.ids])

    def __getitem__(self, index):
        key = self.keys[self.ids[index]]
        return super().__getitem__(key)

    @property
    def dtype(self):
        if self._dtype is None:
            protocol = self.protocol
            self._dtype = self._get(
                item="dtype",
                encode_key=lambda key: _default_encode(key, protocol=protocol),
                decompress_value=_default_decompress,
                decode_value=_default_decode,
            )
        return self._dtype

    @property
    def shape(self):
        if self._shape is None:
            protocol = self.protocol
            self._shape = self._get(
                item="shape",
                encode_key=lambda key: _default_encode(key, protocol=protocol),
                decompress_value=_default_decompress,
                decode_value=_default_decode,
            )
        return self._shape

    def _decode_value(self, value: bytes) -> ndarray:
        value = super()._decode_value(value)
        return np.frombuffer(value, dtype=self.dtype).reshape(self.shape)

    def _decode_values(self, values: list) -> ndarray:
        shape = (len(values),) + self.shape
        return np.frombuffer(b"".join(values), dtype=self.dtype).reshape(shape)

    @staticmethod
    def write(diter, outdir):
        """
        diter is an iterator that has __len__ method
        class Myiter():
            def __init__(self, data):
                self.data = data
            def __iter__(self):
                self.counter = 0
                return self
            def __len__(self):
                return len(self.data)
            def __next__(self):
                if self.counter < len(self):
                    out = self.data[self.counter]
                    self.counter+=1
                    return out
                else:
                    raise StopIteration
        a = iter(Myiter([1,2,3]))
        for i in a:
            print(i)
        """
        outdir = Path(outdir)
        outdir.mkdir(parents=True, exist_ok=True)
        tmp_dir = Path("/tmp") / f"TEMP_{time.time()}"
        tmp_dir.mkdir(parents=True, exist_ok=True)
        # Create the database.
        n = len(diter)
        with lmdb.open(path=str(tmp_dir), map_size=2 ** 40) as env:
            # Add the protocol to the database.
            with env.begin(write=True) as txn:
                key = "protocol".encode("ascii")
                value = pickle.dumps(pickle.DEFAULT_PROTOCOL)
                txn.put(key=key, value=value, dupdata=False)
            # Add the keys to the database.
            with env.begin(write=True) as txn:
                key = pickle.dumps("keys")
                value = pickle.dumps(list(range(n)))
                txn.put(key=key, value=value, dupdata=False)
            # Extract the shape and dtype of the values.
            value = next(iter(diter))
            shape = value.shape
            dtype = value.dtype
            # Add the shape to the database.
            with env.begin(write=True) as txn:
                key = pickle.dumps("shape")
                value = pickle.dumps(shape)
                txn.put(key=key, value=value, dupdata=False)
            # Add the dtype to the database.
            with env.begin(write=True) as txn:
                key = pickle.dumps("dtype")
                value = pickle.dumps(dtype)
                txn.put(key=key, value=value, dupdata=False)
            # Add the values to the database.
            with env.begin(write=True) as txn:
                for key, value in tqdm(enumerate(iter(diter)), total=n, miniters=n//100, mininterval=300):
                    key = pickle.dumps(key)
                    value = pickle.dumps(value)
                    txn.put(key=key, value=value, dupdata=False)

        # Move the database to its destination.
        copy_tree(str(tmp_dir), str(outdir))
        shutil.rmtree(str(tmp_dir))



class TensorDatabase(ArrayDatabase):
    def _decode_value(self, value: bytes) -> Tensor:
        return torch.from_numpy(super(TensorDatabase, self)._decode_value(value))

    def _decode_values(self, values: list) -> Tensor:
        return torch.from_numpy(super(TensorDatabase, self)._decode_values(values))