Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion datajoint/connection.py
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ def __init__(self, host, user, passwd, init_fun=None):
else:
port = config['database.port']
self.conn_info = dict(host=host, port=port, user=user, passwd=passwd)
self._conn = connector.connect(**self.conn_info)
self._conn = connector.connect(init_command=init_fun, **self.conn_info)
if self.is_connected:
logger.info("Connected {user}@{host}:{port}".format(**self.conn_info))
else:
Expand Down
10 changes: 3 additions & 7 deletions datajoint/relational_operand.py
Original file line number Diff line number Diff line change
Expand Up @@ -118,7 +118,9 @@ def __and__(self, restriction):
ret = copy(self)
ret._restrictions = list(ret.restrictions)
# apply restrictions, if any
if isinstance(restriction, RelationalOperand) or restriction:
if isinstance(restriction, RelationalOperand) \
or isinstance(restriction, np.void) \
or restriction:
restrictions = restriction \
if isinstance(restriction, list) or isinstance(restriction, tuple) \
else [restriction]
Expand Down Expand Up @@ -246,7 +248,6 @@ class Join(RelationalOperand):
"""
Relational join
"""
__counter = 0

def __init__(self, arg1, arg2, left=False):
if not isinstance(arg2, RelationalOperand):
Expand All @@ -266,11 +267,6 @@ def _repr_helper(self):
def connection(self):
return self._arg1.connection

@property
def counter(self):
self.__counter += 1
return self.__counter

@property
def heading(self):
return self._heading
Expand Down
33 changes: 23 additions & 10 deletions tests/test_relational_operand.py
Original file line number Diff line number Diff line change
Expand Up @@ -194,26 +194,39 @@ def test_join():
'incorrect join primary_key')

# test pairing
# Approach 1
x = A().project(a1='id_a', c1='cond_in_a') & 'c1=0'
y = A().project(a2='id_a', c2='cond_in_a') & 'c2=1'
# Approach 1: join then restrict
x = A().project(a1='id_a', c1='cond_in_a')
y = A().project(a2='id_a', c2='cond_in_a')
rel = x*y & 'c1=0' & 'c2=1'
assert_equal(len(x)+len(y), len(A()))
assert_equal(len(rel), len(x)*len(y), 'incorrect pairing')
# Approach 2
assert_equal(len(x & 'c1=0')+len(y & 'c2=1'), len(A()),
'incorrect restriction')
assert_equal(len(rel), len(x & 'c1=0')*len(y & 'c2=1'),
'incorrect pairing')
# Approach 2: restrict then join
x = (A() & 'cond_in_a=0').project(a1='id_a')
y = (A() & 'cond_in_a=1').project(a2='id_a')
assert_equal(len(rel), len(x*y))

@staticmethod
def test_project():
x = A().project(a='id_a') # rename
assert_equal(x.heading.names, ['a'], 'renaming does not work')
assert_equal(x.heading.names, ['a'],
'renaming does not work')
x = A().project(a='(id_a)') # extend
assert_equal(set(x.heading.names), set(('id_a', 'a')), 'extend does not work')
assert_equal(set(x.heading.names), set(('id_a', 'a')),
'extend does not work')

@staticmethod
def test_aggregate():
x = B().aggregate(C(), 'n', computed='count(id_c)')
x = B().aggregate(C(), 'n', count='count(id_c)', mean='avg(value)', max='max(value)')
assert_equal(len(x), len(B()))

for n, count, mean, max, key in zip(*x.fetch['n', 'count', 'mean', 'max', dj.key]):
assert_equal(n, count, 'aggregation failed (count)')
values = (C() & key).fetch['value']
assert_true(bool(len(values)) == bool(n),
'aggregation failed (restriction)')
if n:
assert_true(np.isclose(mean, values.mean(), rtol=1e-4, atol=1e-5),
"aggregation failed (mean)")
assert_true(np.isclose(max, values.max(), rtol=1e-4, atol=1e-5),
"aggregation failed (max)")