dsa-helpers 1.0.6.dev0__tar.gz → 1.0.7.dev0__tar.gz

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (49) hide show
  1. {dsa_helpers-1.0.6.dev0/dsa_helpers.egg-info → dsa_helpers-1.0.7.dev0}/PKG-INFO +1 -1
  2. {dsa_helpers-1.0.6.dev0 → dsa_helpers-1.0.7.dev0}/dsa_helpers/mongo_utils.py +103 -3
  3. {dsa_helpers-1.0.6.dev0 → dsa_helpers-1.0.7.dev0}/dsa_helpers/utils.py +5 -2
  4. {dsa_helpers-1.0.6.dev0 → dsa_helpers-1.0.7.dev0/dsa_helpers.egg-info}/PKG-INFO +1 -1
  5. {dsa_helpers-1.0.6.dev0 → dsa_helpers-1.0.7.dev0}/pyproject.toml +1 -1
  6. {dsa_helpers-1.0.6.dev0 → dsa_helpers-1.0.7.dev0}/.gitignore +0 -0
  7. {dsa_helpers-1.0.6.dev0 → dsa_helpers-1.0.7.dev0}/.readthedocs.yaml +0 -0
  8. {dsa_helpers-1.0.6.dev0 → dsa_helpers-1.0.7.dev0}/LICENSE +0 -0
  9. {dsa_helpers-1.0.6.dev0 → dsa_helpers-1.0.7.dev0}/README.md +0 -0
  10. {dsa_helpers-1.0.6.dev0 → dsa_helpers-1.0.7.dev0}/app.py +0 -0
  11. {dsa_helpers-1.0.6.dev0 → dsa_helpers-1.0.7.dev0}/docs/requirements.txt +0 -0
  12. {dsa_helpers-1.0.6.dev0 → dsa_helpers-1.0.7.dev0}/docs/source/conf.py +0 -0
  13. {dsa_helpers-1.0.6.dev0 → dsa_helpers-1.0.7.dev0}/docs/source/index.rst +0 -0
  14. {dsa_helpers-1.0.6.dev0 → dsa_helpers-1.0.7.dev0}/dsa_helpers/__init__.py +0 -0
  15. {dsa_helpers-1.0.6.dev0 → dsa_helpers-1.0.7.dev0}/dsa_helpers/cli_utils.py +0 -0
  16. {dsa_helpers-1.0.6.dev0 → dsa_helpers-1.0.7.dev0}/dsa_helpers/dash/__init__.py +0 -0
  17. {dsa_helpers-1.0.6.dev0 → dsa_helpers-1.0.7.dev0}/dsa_helpers/dash/dash_paperdragon_utils.py +0 -0
  18. {dsa_helpers-1.0.6.dev0 → dsa_helpers-1.0.7.dev0}/dsa_helpers/dash/header.py +0 -0
  19. {dsa_helpers-1.0.6.dev0 → dsa_helpers-1.0.7.dev0}/dsa_helpers/dash/header_callbacks.py +0 -0
  20. {dsa_helpers-1.0.6.dev0 → dsa_helpers-1.0.7.dev0}/dsa_helpers/girder_utils.py +0 -0
  21. {dsa_helpers-1.0.6.dev0 → dsa_helpers-1.0.7.dev0}/dsa_helpers/image_utils.py +0 -0
  22. {dsa_helpers-1.0.6.dev0 → dsa_helpers-1.0.7.dev0}/dsa_helpers/imread.py +0 -0
  23. {dsa_helpers-1.0.6.dev0 → dsa_helpers-1.0.7.dev0}/dsa_helpers/imwrite.py +0 -0
  24. {dsa_helpers-1.0.6.dev0 → dsa_helpers-1.0.7.dev0}/dsa_helpers/ml/__init__.py +0 -0
  25. {dsa_helpers-1.0.6.dev0 → dsa_helpers-1.0.7.dev0}/dsa_helpers/ml/callbacks.py +0 -0
  26. {dsa_helpers-1.0.6.dev0 → dsa_helpers-1.0.7.dev0}/dsa_helpers/ml/datasets/SegFormerSegmentationDataset.py +0 -0
  27. {dsa_helpers-1.0.6.dev0 → dsa_helpers-1.0.7.dev0}/dsa_helpers/ml/datasets/__init__.py +0 -0
  28. {dsa_helpers-1.0.6.dev0 → dsa_helpers-1.0.7.dev0}/dsa_helpers/ml/datasets/utils.py +0 -0
  29. {dsa_helpers-1.0.6.dev0 → dsa_helpers-1.0.7.dev0}/dsa_helpers/ml/evaluate.py +0 -0
  30. {dsa_helpers-1.0.6.dev0 → dsa_helpers-1.0.7.dev0}/dsa_helpers/ml/metrics.py +0 -0
  31. {dsa_helpers-1.0.6.dev0 → dsa_helpers-1.0.7.dev0}/dsa_helpers/ml/object_detection/__init__.py +0 -0
  32. {dsa_helpers-1.0.6.dev0 → dsa_helpers-1.0.7.dev0}/dsa_helpers/ml/object_detection/get_pearce_roi_images.py +0 -0
  33. {dsa_helpers-1.0.6.dev0 → dsa_helpers-1.0.7.dev0}/dsa_helpers/ml/object_detection/tile_image.py +0 -0
  34. {dsa_helpers-1.0.6.dev0 → dsa_helpers-1.0.7.dev0}/dsa_helpers/ml/object_detection/utils.py +0 -0
  35. {dsa_helpers-1.0.6.dev0 → dsa_helpers-1.0.7.dev0}/dsa_helpers/ml/segformer_semantic_segmentation/__init__.py +0 -0
  36. {dsa_helpers-1.0.6.dev0 → dsa_helpers-1.0.7.dev0}/dsa_helpers/ml/segformer_semantic_segmentation/inference.py +0 -0
  37. {dsa_helpers-1.0.6.dev0 → dsa_helpers-1.0.7.dev0}/dsa_helpers/ml/transforms/__init__.py +0 -0
  38. {dsa_helpers-1.0.6.dev0 → dsa_helpers-1.0.7.dev0}/dsa_helpers/ml/transforms/segformer_transforms.py +0 -0
  39. {dsa_helpers-1.0.6.dev0 → dsa_helpers-1.0.7.dev0}/dsa_helpers/tile_utils.py +0 -0
  40. {dsa_helpers-1.0.6.dev0 → dsa_helpers-1.0.7.dev0}/dsa_helpers/tiling.py +0 -0
  41. {dsa_helpers-1.0.6.dev0 → dsa_helpers-1.0.7.dev0}/dsa_helpers.egg-info/SOURCES.txt +0 -0
  42. {dsa_helpers-1.0.6.dev0 → dsa_helpers-1.0.7.dev0}/dsa_helpers.egg-info/dependency_links.txt +0 -0
  43. {dsa_helpers-1.0.6.dev0 → dsa_helpers-1.0.7.dev0}/dsa_helpers.egg-info/requires.txt +0 -0
  44. {dsa_helpers-1.0.6.dev0 → dsa_helpers-1.0.7.dev0}/dsa_helpers.egg-info/top_level.txt +0 -0
  45. {dsa_helpers-1.0.6.dev0 → dsa_helpers-1.0.7.dev0}/setup.cfg +0 -0
  46. {dsa_helpers-1.0.6.dev0 → dsa_helpers-1.0.7.dev0}/tests/app/Dockerfile +0 -0
  47. {dsa_helpers-1.0.6.dev0 → dsa_helpers-1.0.7.dev0}/tests/app/app.py +0 -0
  48. {dsa_helpers-1.0.6.dev0 → dsa_helpers-1.0.7.dev0}/tests/docker-compose.yaml +0 -0
  49. {dsa_helpers-1.0.6.dev0 → dsa_helpers-1.0.7.dev0}/tests/test.ipynb +0 -0
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.4
2
2
  Name: dsa_helpers
3
- Version: 1.0.6.dev0
3
+ Version: 1.0.7.dev0
4
4
  Summary: Utility functions for working with the DSA girder client.
5
5
  Author-email: "Juan C. Vizcarra" <jvizcar@emory.edu>
6
6
  Maintainer-email: "Juan C. Vizcarra" <jvizcar@emory.edu>
@@ -1,4 +1,4 @@
1
- import pymongo
1
+ import json
2
2
  from os import getenv
3
3
  from gridfs import GridFS
4
4
  import numpy as np
@@ -7,6 +7,42 @@ from bson.objectid import ObjectId
7
7
  from PIL import Image
8
8
  from typing import Union
9
9
 
10
+ import pymongo
11
+ from pymongo.database import Database
12
+
13
+
14
+ def store_json_in_db(mongo_db, json_data: dict, _id: str | None = None) -> str:
15
+ """Store a dictionary as a JSON file in GridFS.
16
+
17
+ Args:
18
+ mongo_db: The MongoDB database.
19
+ json_data (dict): The dictionary to store.
20
+
21
+ Returns:
22
+ str: The ID of the stored JSON file.
23
+ """
24
+ fs = GridFS(mongo_db)
25
+
26
+ # Convert dictionary to JSON string
27
+ json_str = json.dumps(json_data)
28
+
29
+ # Store the JSON data in GridFS
30
+ if _id is not None:
31
+ file_id = fs.put(
32
+ json_str.encode("utf-8"),
33
+ encoding="utf-8",
34
+ content_type="application/json",
35
+ _id=ObjectId(_id),
36
+ )
37
+ else:
38
+ file_id = fs.put(
39
+ json_str.encode("utf-8"),
40
+ encoding="utf-8",
41
+ content_type="application/json",
42
+ )
43
+
44
+ return str(file_id)
45
+
10
46
 
11
47
  def get_mongo_client(
12
48
  protocol: str | None = None,
@@ -54,7 +90,9 @@ def get_mongo_client(
54
90
  if port is None:
55
91
  port = int(getenv("MONGO_HOST_PORT", 27017))
56
92
 
57
- return pymongo.MongoClient(f"{protocol}://{username}:{password}@{host}:{port}")
93
+ return pymongo.MongoClient(
94
+ f"{protocol}://{username}:{password}@{host}:{port}"
95
+ )
58
96
 
59
97
 
60
98
  def chunks(lst: list, n=500):
@@ -96,7 +134,9 @@ def add_many_to_collection(
96
134
  operations = []
97
135
 
98
136
  for _id, item in items.items():
99
- operations.append(pymongo.UpdateOne({"_id": _id}, {"$set": item}, upsert=True))
137
+ operations.append(
138
+ pymongo.UpdateOne({"_id": _id}, {"$set": item}, upsert=True)
139
+ )
100
140
 
101
141
  for chunk in chunks(operations):
102
142
  _ = mongo_collection.bulk_write(chunk)
@@ -164,3 +204,63 @@ def add_img_to_db(
164
204
  file_id = fs.put(byte_data)
165
205
 
166
206
  return file_id
207
+
208
+
209
+ def store_json_in_gridfs(
210
+ mongo_db: Database, json_data: dict, _id: str | None = None
211
+ ) -> str:
212
+ """Store a dictionary as a JSON file in GridFS.
213
+
214
+ Args:
215
+ mongo_db (pymongo.database.Database): The MongoDB database.
216
+ json_data (dict): The dictionary to store.
217
+ _id (str, optional): The ID to use for the stored JSON file. If None,
218
+ a new ID will be generated.
219
+
220
+ Returns:
221
+ str: The ID of the stored JSON file.
222
+ """
223
+ fs = GridFS(mongo_db)
224
+
225
+ # Convert dictionary to JSON string
226
+ json_str = json.dumps(json_data)
227
+
228
+ # Store the JSON data in GridFS
229
+ if _id is not None:
230
+ file_id = fs.put(
231
+ json_str.encode("utf-8"),
232
+ encoding="utf-8",
233
+ content_type="application/json",
234
+ _id=ObjectId(_id),
235
+ )
236
+ else:
237
+ file_id = fs.put(
238
+ json_str.encode("utf-8"),
239
+ encoding="utf-8",
240
+ content_type="application/json",
241
+ )
242
+
243
+ return str(file_id)
244
+
245
+
246
+ def get_json_from_gridfs(mongo_db: Database, json_id: str) -> dict:
247
+ """Retrieve a JSON dictionary from GridFS.
248
+
249
+ Args:
250
+ mongo_db (pymongo.database.Database): The MongoDB database.
251
+ json_id (str): The GridFS file ID.
252
+
253
+ Returns:
254
+ dict: The retrieved dictionary.
255
+
256
+ """
257
+ fs = GridFS(mongo_db)
258
+
259
+ grid_out = fs.get(ObjectId(json_id))
260
+
261
+ if grid_out:
262
+ # Read the JSON string and convert it back to a dictionary
263
+ json_str = grid_out.read().decode("utf-8")
264
+ return json.loads(json_str)
265
+
266
+ return None # If not found
@@ -45,10 +45,13 @@ def convert_to_json_serializable(
45
45
  version.
46
46
 
47
47
  Args:
48
- data: Object to convert to JSON serializable.
48
+ data (int | float | str | list | dict): Data to convert to
49
+ JSON serializable.
49
50
 
50
51
  Returns:
51
- JSON serializable version of the object.
52
+ int | float | str | list | dict: JSON serializable version of
53
+ the input data.
54
+
52
55
  """
53
56
  if isinstance(data, np.ndarray):
54
57
  return data.tolist()
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.4
2
2
  Name: dsa_helpers
3
- Version: 1.0.6.dev0
3
+ Version: 1.0.7.dev0
4
4
  Summary: Utility functions for working with the DSA girder client.
5
5
  Author-email: "Juan C. Vizcarra" <jvizcar@emory.edu>
6
6
  Maintainer-email: "Juan C. Vizcarra" <jvizcar@emory.edu>
@@ -35,7 +35,7 @@ classifiers = [
35
35
  "License :: OSI Approved :: MIT License",
36
36
  ]
37
37
  keywords = ["dsa", "girder", "girder_client"]
38
- version = "v1.0.6.dev0"
38
+ version = "v1.0.7.dev0"
39
39
 
40
40
  [project.urls]
41
41
  Repository = "https://github.com/Gutman-Lab/dsa-python-utils"