Update README.md
Browse files
README.md
CHANGED
|
@@ -30,6 +30,67 @@ configs:
|
|
| 30 |
path: data/train-*
|
| 31 |
---
|
| 32 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 33 |
# How to use
|
| 34 |
|
| 35 |
```py
|
|
|
|
| 30 |
path: data/train-*
|
| 31 |
---
|
| 32 |
|
| 33 |
+
# Converting script
|
| 34 |
+
|
| 35 |
+
```py
|
| 36 |
+
|
| 37 |
+
DATA_DIR = Path("/path/to/cached/hugging_face/datasets/for/MLDS-NUS/Experimental_Images")
|
| 38 |
+
# should end with something like "snapshots/fd299418e9435f8fd98956a3f0a7344d208cc142"
|
| 39 |
+
|
| 40 |
+
|
| 41 |
+
def calc_left_right(data: np.ndarray):
|
| 42 |
+
left_rights = []
|
| 43 |
+
for im in data:
|
| 44 |
+
nonzero_columns = (im != 0).any(axis=-2)
|
| 45 |
+
left = nonzero_columns.argmax() if nonzero_columns.any() else -1
|
| 46 |
+
# Find the rightmost non-zero column
|
| 47 |
+
right = len(nonzero_columns) - 1 - nonzero_columns[::-1].argmax() if nonzero_columns.any() else -1
|
| 48 |
+
left_right = np.array([left, right])
|
| 49 |
+
left_rights.append(left_right)
|
| 50 |
+
left_rights = np.stack(left_rights, axis=0) # shape: (seq_len, 2)
|
| 51 |
+
return left_rights
|
| 52 |
+
|
| 53 |
+
|
| 54 |
+
def calc_barycenter(data: np.ndarray) -> np.ndarray:
|
| 55 |
+
"""
|
| 56 |
+
Calculate the barycenter of the polymer from the snapshot.
|
| 57 |
+
Assumes snapshot shape is (100, 500).
|
| 58 |
+
"""
|
| 59 |
+
xx = np.arange(data.shape[-2]).reshape(-1, 1)
|
| 60 |
+
bary_x = (data * xx).sum(axis=(-2, -1)) / data.sum(axis=(-2, -1))
|
| 61 |
+
yy = np.arange(data.shape[-1]).reshape(1, -1)
|
| 62 |
+
bary_y = (data * yy).sum(axis=(-2, -1)) / data.sum(axis=(-2, -1))
|
| 63 |
+
barycenter = np.stack([bary_x, bary_y], axis=-1) # (seq_len, 2)
|
| 64 |
+
return barycenter
|
| 65 |
+
|
| 66 |
+
|
| 67 |
+
def gen():
|
| 68 |
+
for folder in ["30V_Jan24", "60V_Dec24"]:
|
| 69 |
+
|
| 70 |
+
with open(DATA_DIR / f"{folder}.pkl", "rb") as f:
|
| 71 |
+
data = pickle.load(f)
|
| 72 |
+
|
| 73 |
+
for k, v in data.items():
|
| 74 |
+
frame = np.clip(v, 0, 255).astype(np.uint8) # save memory
|
| 75 |
+
left_rights = calc_left_right(255 - frame)
|
| 76 |
+
barycenters = calc_barycenter(255 - frame)
|
| 77 |
+
|
| 78 |
+
yield {
|
| 79 |
+
"config": folder,
|
| 80 |
+
"traj_id": k,
|
| 81 |
+
"shape": list(frame.shape),
|
| 82 |
+
"data": frame,
|
| 83 |
+
"left_right": left_rights,
|
| 84 |
+
"barycenter": barycenters,
|
| 85 |
+
}
|
| 86 |
+
|
| 87 |
+
|
| 88 |
+
ds = Dataset.from_generator(gen)
|
| 89 |
+
ds = ds.with_format("numpy")
|
| 90 |
+
|
| 91 |
+
ds.push_to_hub("MLDS-NUS/polymer_re-upload")
|
| 92 |
+
```
|
| 93 |
+
|
| 94 |
# How to use
|
| 95 |
|
| 96 |
```py
|