File size: 6,318 Bytes
6922a11
 
25e8803
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
9f3d281
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
25e8803
6922a11
b86b875
 
 
6922a11
fdc7b4f
 
6922a11
 
 
 
14006a2
 
 
6bfd36b
 
 
6922a11
 
6bfd36b
b86b875
6bfd36b
 
6922a11
25e8803
 
 
f3fda3f
9f3d281
 
 
 
6922a11
 
 
b86b875
6922a11
8506e9a
2be4127
 
 
ed4c89c
 
f9adebe
 
 
 
 
 
ed4c89c
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
4c913f3
2be4127
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
ed4c89c
 
2be4127
8506e9a
f9adebe
 
8506e9a
f9adebe
 
 
 
2be4127
 
 
 
 
8506e9a
2f29aaa
 
 
 
 
 
 
 
 
e2f385e
 
2f29aaa
 
 
 
 
0f53fb6
 
8506e9a
 
2be4127
 
 
 
 
 
 
 
 
282cc13
2be4127
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
---
dataset_info:
- config_name: 30V_Jan24
  features:
  - name: config
    dtype: string
  - name: traj_id
    dtype: string
  - name: shape
    list: int64
  - name: data
    list:
      list:
        list: uint8
  - name: left_right
    list:
      list: int64
  - name: barycenter
    list:
      list: float64
  splits:
  - name: train
    num_bytes: 1011799274
    num_examples: 220
  download_size: 32061722
  dataset_size: 1011799274
- config_name: 60V_Dec24
  features:
  - name: config
    dtype: string
  - name: traj_id
    dtype: string
  - name: shape
    list: int64
  - name: data
    list:
      list:
        list: uint8
  - name: left_right
    list:
      list: int64
  - name: barycenter
    list:
      list: float64
  splits:
  - name: train
    num_bytes: 1107240702
    num_examples: 605
  download_size: 34858112
  dataset_size: 1107240702
- config_name: default
  features:
  - name: config
    dtype: string
  - name: traj_id
    dtype: string
  - name: shape
    list: int64
  - name: data
    list:
      list:
        list: uint8
  - name: left_right
    list:
      list: int64
  - name: barycenter
    list:
      list: float64
  splits:
  - name: train
    num_bytes: 2119039976
    num_examples: 825
  download_size: 66925984
  dataset_size: 2119039976
configs:
- config_name: 30V_Jan24
  data_files:
  - split: train
    path: 30V_Jan24/train-*
- config_name: 60V_Dec24
  data_files:
  - split: train
    path: 60V_Dec24/train-*
- config_name: default
  data_files:
  - split: train
    path: data/train-*
---

# Descriptions

## Converting script

```py
import pickle
from pathlib import Path

import numpy as np
from datasets import Dataset


DATA_DIR = Path("/path/to/cached/hugging_face/datasets/for/MLDS-NUS/Experimental_Images")
# should end with something like "snapshots/fd299418e9435f8fd98956a3f0a7344d208cc142"


def calc_left_right(data: np.ndarray):
    left_rights = []
    for im in data:
        nonzero_columns = (im != 0).any(axis=-2)
        left = nonzero_columns.argmax() if nonzero_columns.any() else -1
        # Find the rightmost non-zero column
        right = len(nonzero_columns) - 1 - nonzero_columns[::-1].argmax() if nonzero_columns.any() else -1
        left_right = np.array([left, right])
        left_rights.append(left_right)
    left_rights = np.stack(left_rights, axis=0)  # shape: (seq_len, 2)
    return left_rights


def calc_barycenter(data: np.ndarray) -> np.ndarray:
    """
    Calculate the barycenter of the polymer from the snapshot.
    Assumes snapshot shape is (100, 500).
    """
    xx = np.arange(data.shape[-2]).reshape(-1, 1)
    bary_x = (data * xx).sum(axis=(-2, -1)) / data.sum(axis=(-2, -1))
    yy = np.arange(data.shape[-1]).reshape(1, -1)
    bary_y = (data * yy).sum(axis=(-2, -1)) / data.sum(axis=(-2, -1))
    barycenter = np.stack([bary_x, bary_y], axis=-1)  # (seq_len, 2)
    return barycenter


def gen():
    for folder in ["30V_Jan24", "60V_Dec24"]:

        with open(DATA_DIR / f"{folder}.pkl", "rb") as f:
            data = pickle.load(f)
    
        for k, v in data.items():
            frame = np.clip(v, 0, 255).astype(np.uint8) # save memory
            left_rights = calc_left_right(255 - frame)
            barycenters = calc_barycenter(255 - frame)
    
            yield {
                "config": folder,
                "traj_id": k,
                "shape": list(frame.shape),
                "data": frame,
                "left_right": left_rights,
                "barycenter": barycenters,
            }


ds = Dataset.from_generator(gen)
ds = ds.with_format("numpy")

ds.push_to_hub("MLDS-NUS/polymer-dynamics_experimental-data")


# upload by configs
def gen(folder: str):

    with open(DATA_DIR / f"{folder}.pkl", "rb") as f:
        data = pickle.load(f)

    for k, v in data.items():
        frame = np.clip(v, 0, 255).astype(np.uint8)
        left_rights = calc_left_right(255 - frame)
        barycenters = calc_barycenter(255 - frame)

        yield {
            "config": folder,
            "traj_id": k,
            "shape": list(frame.shape),
            "data": frame,
            "left_right": left_rights,
            "barycenter": barycenters,
        }


for config_name in ["30V_Jan24", "60V_Dec24"]:
    ds = Dataset.from_generator(lambda cn=config_name: gen(cn))
    ds = ds.with_format("numpy")

    ds.push_to_hub(
        "MLDS-NUS/polymer-dynamics_experimental-data",
        config_name=config_name,
        data_dir=f"{config_name}",
    )

```

## How to use

Directly loading by [datasets](https://huggingface.co/docs/datasets/installation) is supported now!

```py
from datasets import load_dataset
import numpy as np


hf_dataset_30V = load_dataset("MLDS-NUS/polymer-dynamics_experimental-data", config_name="30V_Jan24")
hf_dataset_60V = load_dataset("MLDS-NUS/polymer-dynamics_experimental-data", config_name="60V_Jan24")

hf_dataset_30V = hf_dataset_30V.with_format("numpy")["train"]
hf_dataset_60V = hf_dataset_60V.with_format("numpy")["train"]


for sample in hf_dataset_30V:
  for k, v in sample.items():
      if isinstance(v, np.ndarray):
          print(f"{k}: {type(v)}, shape={v.shape}, dtype={v.dtype}")
      else:
          print(f"{k}: {v}")
```

output:

```txt
config: 30V_Jan24
traj_id: 30V_Tra_0
shape: <class 'numpy.ndarray'>, shape=(3,), dtype=int64
data: <class 'numpy.ndarray'>, shape=(160, 100, 350), dtype=int64
left_right: <class 'numpy.ndarray'>, shape=(160, 2), dtype=int64
barycenter: <class 'numpy.ndarray'>, shape=(160, 2), dtype=float32
```

## How to contribute

```py
import numpy as np
from datasets import Dataset


def gen(config_name: str):

    for data in your_database_retriever(config_name):

        frame = ...
        traj_id = ...
        shape = ...
        left_rights = ...
        barycenters = ...

        yield {
            "config": config_name,
            "traj_id": traj_id,
            "shape": shape,
            "data": data, # a np.ndarray object of shape `shape`
            "left_right": left_rights,
            "barycenter": barycenters,
        }


config_name = ...
ds = Dataset.from_generator(lambda cn=config_name: gen(cn))
ds = ds.with_format("numpy")

ds.push_to_hub(
    "MLDS-NUS/polymer-dynamics_experimental-data",
    config_name=config_name,
    data_dir=f"{config_name}",
)

```