Skip to content

Commit 6184aba

Browse files
authored
Merge pull request #231 from dimitri-yatsenko/master
ERD.add_parts
2 parents cd241a8 + 9c37146 commit 6184aba

4 files changed

Lines changed: 45 additions & 14 deletions

File tree

‎CHANGELOG.md‎

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,3 +9,7 @@
99
* Added ERD.from_sequence as a shortcut to combining the ERDs of multiple sources
1010
* ERD() no longer text the context argument.
1111
* ERD.draw() now takes an optional context argument. By default uses the caller's locals.
12+
13+
### 0.3.4
14+
* Added method the `ERD.add_parts` method, which adds the part tables of all tables currently in the ERD.
15+
* `ERD() + arg` and `ERD() - arg` can now accept relation classes as arg.

‎datajoint/__init__.py‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,8 +16,8 @@
1616
import os
1717

1818
__author__ = "Dimitri Yatsenko, Edgar Walker, and Fabian Sinz at Baylor College of Medicine"
19-
__version__ = "0.3.3"
20-
__date__ = "July 20, 2016"
19+
__version__ = "0.3.4"
20+
__date__ = "July 21, 2016"
2121
__all__ = ['__author__', '__version__',
2222
'config', 'conn', 'kill', 'BaseRelation',
2323
'Connection', 'Heading', 'FreeRelation', 'Not', 'schema',

‎datajoint/erd.py‎

Lines changed: 37 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414
def _get_tier(table_name):
1515
try:
1616
return next(tier for tier in user_relation_classes
17-
if re.fullmatch(tier.tier_regexp, table_name))
17+
if re.fullmatch(tier.tier_regexp, table_name.split('`')[-2]))
1818
except StopIteration:
1919
return None
2020

@@ -84,6 +84,25 @@ def from_sequence(cls, sequence):
8484
"""
8585
return functools.reduce(lambda x, y: x+y, map(ERD, sequence))
8686

87+
def add_parts(self):
88+
"""
89+
Adds to the diagram the part tables of tables already included in the diagram
90+
:return:
91+
"""
92+
def is_part(part, master):
93+
"""
94+
:param part: `database`.`table_name`
95+
:param master: `database`.`table_name`
96+
:return: True if part is part of master,
97+
"""
98+
part = [s.strip('`') for s in part.split('.')]
99+
master = [s.strip('`') for s in master.split('.')]
100+
return master[0] == part[0] and master[1] + '__' == part[1][:len(master[1])+2]
101+
102+
self = ERD(self) # copy
103+
self.nodes_to_show.update(n for n in self.nodes() if any(is_part(n, m) for m in self.nodes_to_show))
104+
return self
105+
87106
def __add__(self, arg):
88107
"""
89108
:param arg: either another ERD or a positive integer.
@@ -93,11 +112,14 @@ def __add__(self, arg):
93112
try:
94113
self.nodes_to_show.update(arg.nodes_to_show)
95114
except AttributeError:
96-
for i in range(arg):
97-
new = nx.algorithms.boundary.node_boundary(self, self.nodes_to_show)
98-
if not new:
99-
break
100-
self.nodes_to_show.update(new)
115+
try:
116+
self.nodes_to_show.add(arg.full_table_name)
117+
except AttributeError:
118+
for i in range(arg):
119+
new = nx.algorithms.boundary.node_boundary(self, self.nodes_to_show)
120+
if not new:
121+
break
122+
self.nodes_to_show.update(new)
101123
return self
102124

103125
def __sub__(self, arg):
@@ -109,11 +131,14 @@ def __sub__(self, arg):
109131
try:
110132
self.nodes_to_show.difference_update(arg.nodes_to_show)
111133
except AttributeError:
112-
for i in range(arg):
113-
new = nx.algorithms.boundary.node_boundary(nx.DiGraph(self).reverse(), self.nodes_to_show)
114-
if not new:
115-
break
116-
self.nodes_to_show.update(new)
134+
try:
135+
self.nodes_to_show.remove(arg.full_table_name)
136+
except AttributeError:
137+
for i in range(arg):
138+
new = nx.algorithms.boundary.node_boundary(nx.DiGraph(self).reverse(), self.nodes_to_show)
139+
if not new:
140+
break
141+
self.nodes_to_show.update(new)
117142
return self
118143

119144
def __mul__(self, arg):
@@ -131,7 +156,7 @@ def _make_graph(self, context):
131156
Make the self.graph - a graph object ready for drawing
132157
"""
133158
graph = nx.DiGraph(self).subgraph(self.nodes_to_show)
134-
nx.set_node_attributes(graph, 'node_type', {n: _get_tier(n.split('`')[-2]) for n in graph})
159+
nx.set_node_attributes(graph, 'node_type', {n: _get_tier(n) for n in graph})
135160
# relabel nodes to class names
136161
mapping = {node: lookup_class_name(node, context) for node in graph.nodes()}
137162
new_names = [mapping.values()]

‎tests/test_erd.py‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,9 @@ def test_erd_algebra():
4343
erd1 = erd0 + 3
4444
erd2 = dj.ERD(E) - 3
4545
erd3 = erd1 * erd2
46+
erd4 = (erd0 + E).add_parts() - B - E
4647
assert_true(erd0.nodes_to_show == set(cls.full_table_name for cls in [B]))
4748
assert_true(erd1.nodes_to_show == set(cls.full_table_name for cls in (B, B.C, E, E.F)))
4849
assert_true(erd2.nodes_to_show == set(cls.full_table_name for cls in (A, B, D, E, L)))
4950
assert_true(erd3.nodes_to_show == set(cls.full_table_name for cls in (B, E)))
51+
assert_true(erd4.nodes_to_show == set(cls.full_table_name for cls in (B.C, E.F)))

0 commit comments

Comments
 (0)