Skip to content

Commit 31d7d01

Browse files
authored
Merge pull request #405 from aperture-data/release-0.4.23
Release 0.4.23
2 parents 0f28a1a + 61e89d9 commit 31d7d01

File tree

5 files changed

+97
-2
lines changed

5 files changed

+97
-2
lines changed

aperturedb/PyTorchDataset.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,7 @@ def __init__(self, db, query, label_prop=None, batch_size=1):
4646
self.query[self.find_image_idx]["FindImage"]["results"] = {}
4747

4848
self.query[self.find_image_idx]["FindImage"]["batch"] = {}
49+
self.query[self.find_image_idx]["FindImage"]["blobs"] = True
4950

5051
try:
5152
r, b = self.db.query(self.query)

aperturedb/Utils.py

Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@
55
import sys
66
from typing import List
77

8+
from graphviz import Source, Digraph
9+
810
from aperturedb.Connector import Connector
911
from aperturedb.ConnectorRest import ConnectorRest
1012
from aperturedb import ProgressBar
@@ -181,6 +183,78 @@ def get_schema(self, refresh=False):
181183

182184
return schema
183185

186+
def visualize_schema(self, filename: str = None, format: str = "png") -> Source:
187+
"""
188+
Visualize the schema of the database. Optionally save the visualization to a file in the specified format.
189+
190+
The returned object can be rendered to a file as follows:
191+
```python
192+
s = utils.visualize_schema()
193+
s.render("schema", format="png")
194+
```
195+
196+
It can also be displayed inline in a Jupyter notebook:
197+
```python
198+
from IPython.display import display
199+
s = utils.visualize_schema()
200+
display(s)
201+
```
202+
203+
Relies on graphviz to be installed.
204+
205+
Args:
206+
filename (str, optional): The filename to save the visualization to. Default is None.
207+
format (str, optional): The format to save the visualization to. Default is "png".
208+
209+
Returns:
210+
source: The visualization of the schema.
211+
"""
212+
r = self.get_schema()
213+
214+
dot = Digraph(comment='ApertureDB Schema Diagram', node_attr={
215+
'shape': 'none'}, graph_attr={'rankdir': 'LR'})
216+
217+
# Add entities as nodes and connections as edges
218+
entities = r['entities']['classes']
219+
connections = r['connections']['classes']
220+
221+
for entity, data in entities.items():
222+
matched = data["matched"]
223+
# dictionary from name to (matched, indexed, type)
224+
properties = data["properties"]
225+
table = f'''<
226+
<TABLE BORDER="0" CELLBORDER="1" CELLSPACING="0">
227+
<TR><TD BGCOLOR="lightgrey">{entity} ({matched:,})</TD></TR>
228+
'''
229+
for prop, (matched, indexed, typ) in properties.items():
230+
table += f'<TR><TD BGCOLOR="lightblue">{prop} ({matched:,}): {"Indexed" if indexed else "Unindexed"}, {typ}</TD></TR>'
231+
for connection, data in connections.items():
232+
if data['src'] == entity:
233+
matched = data["matched"]
234+
# dictionary from name to (matched, indexed, type)
235+
properties = data["properties"]
236+
table += f'<TR><TD BGCOLOR="lightgreen"><TABLE BORDER="0" CELLBORDER="1" CELLSPACING="0">'
237+
table += f'<TR><TD PORT="{connection}">{connection} ({matched:,})</TD></TR>'
238+
if properties:
239+
for prop, (matched, indexed, typ) in properties.items():
240+
table += f'<TR><TD>{prop} ({matched:,}): {"Indexed" if indexed else "Unindexed"}, {typ}</TD></TR>'
241+
table += '</TABLE></TD></TR>'
242+
243+
table += '</TABLE>>'
244+
dot.node(entity, label=table)
245+
246+
for connection, data in connections.items():
247+
dot.edge(f'{data["src"]}:{connection}',
248+
f'{data["dst"]}:{connection}')
249+
250+
# Render the diagram inline
251+
s = Source(dot.source, filename="schema_diagram.gv", format="png")
252+
253+
if filename is not None:
254+
s.render(filename, format=format)
255+
256+
return s
257+
184258
def _object_summary(self, name, object):
185259

186260
total_elements = object["matched"]

aperturedb/__init__.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77

88
logger = logging.getLogger(__name__)
99

10-
__version__ = "0.4.22"
10+
__version__ = "0.4.23"
1111

1212
# set log level
1313
logger.setLevel(logging.DEBUG)

aperturedb/cli/utilities.py

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -62,3 +62,22 @@ def log(
6262
"""
6363
utils = Utils(create_connector())
6464
utils.user_log_message(message, level=level.value)
65+
66+
67+
@app.command()
68+
def visualize_schema(
69+
filename: str = "schema",
70+
format: str = "png"
71+
):
72+
"""
73+
Visualize the schema of the database.
74+
75+
This will create a file with the schema of the database in the specified format.
76+
77+
Relies on graphviz to be installed.
78+
"""
79+
80+
utils = Utils(create_connector())
81+
s = utils.visualize_schema()
82+
result = s.render(filename, format=format)
83+
print(result)

pyproject.toml

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,8 @@ dependencies = [
2727
'grpcio-status==1.48.2',
2828
# Pinning this to resolve test errors temporarily
2929
'ipywidgets==8.0.4',
30-
'keepalive-socket==0.0.1'
30+
'keepalive-socket==0.0.1',
31+
'graphviz==0.20.2'
3132
]
3233

3334
[tool.setuptools.package-dir]

0 commit comments

Comments
 (0)