Praveen Kulkarni commited on
Commit
a05f582
·
1 Parent(s): b399f39

utf8 encoding

Browse files
Files changed (2) hide show
  1. example.zip → data.zarr.zip +2 -2
  2. test.py +54 -36
example.zip → data.zarr.zip RENAMED
@@ -1,3 +1,3 @@
1
  version https://git-lfs.github.com/spec/v1
2
- oid sha256:72587c8172c62b3787af98643d94f0e1abd5da7657977bfd67a55ae4dca3e691
3
- size 18728
 
1
  version https://git-lfs.github.com/spec/v1
2
+ oid sha256:772cd963b3cf2f5a2626b34bead078947b460829f5ca691eb1cd230d399efe76
3
+ size 8029
test.py CHANGED
@@ -2,6 +2,8 @@ import zarr
2
  import numpy as np
3
  import datasets
4
  import fsspec
 
 
5
 
6
 
7
 
@@ -32,67 +34,83 @@ class ZarrTest:
32
 
33
  @staticmethod
34
  def create():
35
- store = zarr.DirectoryStore('data.zarr')
36
- root = zarr.group(store=store)
37
- z1 = root.zeros('trace', shape=(100, 100), chunks=(100, 10), dtype='i1', overwrite=True)
38
  z1[:] = 42
39
  z1[0, :] = np.arange(100)
40
  z1[:, 0] = np.arange(100)
41
- z2 = root.zeros('plaintext', shape=100, dtype='u1', overwrite=True)
42
- z3 = root.zeros('ciphertext', shape=100, dtype='u1', overwrite=True)
43
- z4 = root.zeros('key', shape=100, dtype='u1', overwrite=True)
44
  z2[:] = 22
45
  z3[:] = 33
46
  z4[:] = 44
47
- zarr.consolidate_metadata(store)
48
- store.close()
49
 
50
- store = zarr.DirectoryStore('data.zarr')
51
- root = zarr.group(store=store)
52
- test = np.all(z1[:] == root['trace'][:])
53
  print(test)
 
 
54
 
55
  @staticmethod
56
  def create_in_zip():
57
- store = zarr.ZipStore('data/example.zip', mode='w')
58
- root = zarr.group(store=store)
59
- z1 = root.zeros('foo/bar', shape=(100, 100), chunks=(10, 10), dtype='i4')
60
  z1[:] = 42
61
  z1[0, :] = np.arange(100)
62
  z1[:, 0] = np.arange(100)
63
- zarr.consolidate_metadata(store)
64
- store.close()
65
-
66
- store = zarr.ZipStore('data/example.zip', mode='r')
67
- root = zarr.group(store=store)
68
- z1 = root['foo/bar']
 
69
  print(z1[:])
70
- store.close()
71
-
72
- @staticmethod
73
- def load_zip_partially():
74
- """
75
- delete all 0.* files in zip file
76
- """
77
 
78
- store = zarr.ZipStore('data/example.zip', mode='r')
79
- root = zarr.open_consolidated(store)
80
- z1 = root['foo/bar']
81
- print(z1[:10])
82
- print(z1[10:])
83
- store.close()
84
 
85
  @staticmethod
86
- def load_zip_partially_from_hf():
87
  """
88
  Note that we load data generated via create_in_zip to upload to HF
89
  """
90
  from datasets import load_dataset
91
  import datasets
92
- ds = load_dataset("spikingneurons/test", streaming=True, trust_remote_code=True)
93
- print(ds)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
94
 
95
  ZarrTest.create()
 
 
96
  # ZarrTest.load_zip_partially_from_hf()
97
 
98
  class Test(datasets.GeneratorBasedBuilder):
 
2
  import numpy as np
3
  import datasets
4
  import fsspec
5
+ from huggingface_hub import HfFileSystem
6
+ from fsspec.implementations.zip import ZipFileSystem
7
 
8
 
9
 
 
34
 
35
  @staticmethod
36
  def create():
37
+ store1 = zarr.DirectoryStore('data.zarr')
38
+ root1 = zarr.group(store=store1)
39
+ z1 = root1.zeros('trace', shape=(100, 100), chunks=(100, 10), dtype='i1', overwrite=True)
40
  z1[:] = 42
41
  z1[0, :] = np.arange(100)
42
  z1[:, 0] = np.arange(100)
43
+ z2 = root1.zeros('plaintext', shape=100, dtype='u1', overwrite=True)
44
+ z3 = root1.zeros('ciphertext', shape=100, dtype='u1', overwrite=True)
45
+ z4 = root1.zeros('key', shape=100, dtype='u1', overwrite=True)
46
  z2[:] = 22
47
  z3[:] = 33
48
  z4[:] = 44
49
+ zarr.consolidate_metadata(store1)
 
50
 
51
+ store2 = zarr.DirectoryStore('data.zarr')
52
+ root2 = zarr.group(store=store2)
53
+ test = np.all(z1[:] == root2['trace'][:])
54
  print(test)
55
+ store1.close()
56
+ store2.close()
57
 
58
  @staticmethod
59
  def create_in_zip():
60
+ store1 = zarr.ZipStore('data.zarr.zip')
61
+ root1 = zarr.group(store=store1)
62
+ z1 = root1.zeros('trace', shape=(100, 100), chunks=(100, 10), dtype='i1', overwrite=True)
63
  z1[:] = 42
64
  z1[0, :] = np.arange(100)
65
  z1[:, 0] = np.arange(100)
66
+ z2 = root1.zeros('plaintext', shape=100, dtype='u1', overwrite=True)
67
+ z3 = root1.zeros('ciphertext', shape=100, dtype='u1', overwrite=True)
68
+ z4 = root1.zeros('key', shape=100, dtype='u1', overwrite=True)
69
+ z2[:] = 22
70
+ z3[:] = 33
71
+ z4[:] = 44
72
+ zarr.consolidate_metadata(store1)
73
  print(z1[:])
74
+ store1.close()
 
 
 
 
 
 
75
 
76
+ store2 = zarr.ZipStore('data.zarr.zip')
77
+ root2 = zarr.open_consolidated(store2)
78
+ print(root2['trace'][:])
79
+ store2.close()
 
 
80
 
81
  @staticmethod
82
+ def load_from_hf():
83
  """
84
  Note that we load data generated via create_in_zip to upload to HF
85
  """
86
  from datasets import load_dataset
87
  import datasets
88
+ # ds = load_dataset("spikingneurons/test", streaming=True, trust_remote_code=True)
89
+ # print(ds)
90
+ _fs = HfFileSystem()
91
+ _files = _fs.ls("datasets/spikingneurons/test", detail=False)
92
+ print(_files)
93
+ # import xarray as xr
94
+ # _arr = xr.open_dataset('hf://datasets/spikingneurons/test/data.zarr', engine='zarr', chunks={})
95
+ from zarr.storage import FSStore
96
+ store = FSStore('hf://datasets/spikingneurons/test/data.zarr')
97
+ root = zarr.group(store=store)
98
+ # store = FSStore('zip://*::hf://datasets/spikingneurons/test/example.zip')
99
+ # store11 = FSStore('zip://hf://datasets/spikingneurons/test/example.zip')
100
+ # store22 = FSStore('zip+hf://datasets/spikingneurons/test/example.zip')
101
+ # store = zarr.DirectoryStore(UPath('hf://datasets/spikingneurons/test') / 'data.zarr')
102
+ # store1 = zarr.DirectoryStore('data.zarr')
103
+ # root1 = zarr.group(store=store1)
104
+ with fsspec.open('hf://datasets/spikingneurons/test/example.zip', 'rb', fs=HfFileSystem()) as zip_file:
105
+ _zip_fs = ZipFileSystem(zip_file)
106
+ store4 = FSStore(url="", fs=_zip_fs)
107
+ root4 = zarr.group(store=store4)
108
+ print(root4['foo']['bar'][:])
109
+ print(root['trace'][:, 0])
110
 
111
  ZarrTest.create()
112
+ ZarrTest.create_in_zip()
113
+ # ZarrTest.load_from_hf()
114
  # ZarrTest.load_zip_partially_from_hf()
115
 
116
  class Test(datasets.GeneratorBasedBuilder):