Skip to content

Commit 396dd66

Browse files
committed
feat(import): add example for datasets importation
1 parent 9cd79f1 commit 396dd66

File tree

1 file changed

+58
-0
lines changed

1 file changed

+58
-0
lines changed

examples/upload_datasets.py

+58
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
"""Example to import datasets into Cytomine."""
2+
3+
import logging
4+
import os
5+
import sys
6+
from argparse import ArgumentParser
7+
8+
from cytomine import Cytomine
9+
from cytomine.models import StorageCollection
10+
11+
logging.basicConfig()
12+
logger = logging.getLogger("cytomine.client")
13+
logger.setLevel(logging.INFO)
14+
15+
16+
if __name__ == "__main__":
17+
parser = ArgumentParser(description="Example to import datasets into Cytomine")
18+
19+
parser.add_argument(
20+
"--host",
21+
default="demo.cytomine.be",
22+
help="The Cytomine host",
23+
)
24+
parser.add_argument(
25+
"--public_key",
26+
help="The Cytomine public key",
27+
)
28+
parser.add_argument(
29+
"--private_key",
30+
help="The Cytomine private key",
31+
)
32+
parser.add_argument(
33+
"--path",
34+
help="The path to the datasets to import",
35+
)
36+
params, _ = parser.parse_known_args(sys.argv[1:])
37+
38+
with Cytomine(
39+
host=params.host,
40+
public_key=params.public_key,
41+
private_key=params.private_key,
42+
) as cytomine:
43+
44+
# Check that the datasets path exists on your file system
45+
if not os.path.exists(params.path):
46+
raise ValueError(f"{params.path} does not exist!")
47+
48+
# To import the datasets, we need to know the ID of your Cytomine storage.
49+
storages = StorageCollection().fetch()
50+
storage = next(
51+
filter(lambda storage: storage.user == cytomine.current_user.id, storages)
52+
)
53+
if not storage:
54+
raise ValueError("Storage not found")
55+
56+
response = cytomine.import_datasets(params.path, storage.id)
57+
58+
print(response)

0 commit comments

Comments
 (0)