Skip to content
Merged
Show file tree
Hide file tree
Changes from 7 commits
Commits
Show all changes
65 commits
Select commit Hold shift + click to select a range
67e41ef
Fixed #367
dimitri-yatsenko Oct 12, 2017
f89bf0e
Merge branch 'master' of https://github.com/datajoint/datajoint-python
dimitri-yatsenko Oct 12, 2017
159cb51
fixed dependencies
dimitri-yatsenko Oct 12, 2017
bb88117
implemented the Union operator
dimitri-yatsenko Oct 13, 2017
9703126
bugfix in populate
dimitri-yatsenko Oct 13, 2017
4fa1abd
fixed a bug in server-side insert with missing attributes
dimitri-yatsenko Oct 13, 2017
d6b9f2a
work on cascading delete
dimitri-yatsenko Oct 19, 2017
c35ed7a
Merge branch 'master' of https://github.com/dimitri-yatsenko/datajoin…
dimitri-yatsenko Oct 19, 2017
7299058
fixed #375 -- added the max_calls argument to populate
dimitri-yatsenko Oct 19, 2017
3baf487
simplified insert from select
dimitri-yatsenko Oct 20, 2017
4969c36
consolidating the use of hashes
dimitri-yatsenko Oct 20, 2017
ce774be
added module `external` for handling external storage
dimitri-yatsenko Oct 20, 2017
ad650b0
minor fix in external, work in progress
dimitri-yatsenko Oct 20, 2017
860d04b
minor
dimitri-yatsenko Oct 20, 2017
2144b0f
changed how external storage is configured
dimitri-yatsenko Oct 20, 2017
cfc1e21
minor bug
dimitri-yatsenko Oct 20, 2017
0195cfc
minor formatting
dimitri-yatsenko Oct 20, 2017
d821bcf
removed the JobManager class -- it was never used
dimitri-yatsenko Oct 20, 2017
183b324
bugfix from previous commit
dimitri-yatsenko Oct 20, 2017
6164eb5
renamed the display_progress argument in populate()
dimitri-yatsenko Oct 20, 2017
96f0d20
Merge branch 'master' of https://github.com/dimitri-yatsenko/datajoin…
dimitri-yatsenko Oct 20, 2017
b37175b
rolled back unintended changes in delete
dimitri-yatsenko Oct 20, 2017
9688e5d
fixed typo JobsTable -> JobTable
dimitri-yatsenko Oct 20, 2017
bcc0048
fixed bugs introduced in recent commits
dimitri-yatsenko Oct 20, 2017
28208c2
Merge branch 'master' of https://github.com/datajoint/datajoint-python
dimitri-yatsenko Oct 20, 2017
04b1829
fixed test_requirements
dimitri-yatsenko Oct 20, 2017
1c8e071
fixed indentation
dimitri-yatsenko Oct 20, 2017
bb11c1b
minor
dimitri-yatsenko Oct 20, 2017
9fc36b4
minor
dimitri-yatsenko Oct 20, 2017
9fca90f
minor code refactor in schema.py
dimitri-yatsenko Oct 22, 2017
7069dad
improved the error message in autopopulate
dimitri-yatsenko Oct 22, 2017
3bc28c5
correction to previous commit
dimitri-yatsenko Oct 22, 2017
a3d1a53
Merge branch 'master' of https://github.com/datajoint/datajoint-python
dimitri-yatsenko Oct 24, 2017
d9cca7b
Merge branch 'master' of https://github.com/dimitri-yatsenko/datajoin…
dimitri-yatsenko Oct 24, 2017
1b37ee9
typo
dimitri-yatsenko Oct 24, 2017
cf3cf0c
made `make` and acceptable name for the populate callback (issue #387)
dimitri-yatsenko Oct 25, 2017
85b6587
small bugfix for rare cases with multiple inheritance
dimitri-yatsenko Oct 27, 2017
f936073
minor fix
dimitri-yatsenko Oct 27, 2017
2b622c1
Merge branch 'master' of https://github.com/dimitri-yatsenko/datajoin…
dimitri-yatsenko Oct 27, 2017
5270d80
minor fixes
dimitri-yatsenko Oct 29, 2017
85502b0
Merge branch 'master' of https://github.com/dimitri-yatsenko/datajoin…
dimitri-yatsenko Oct 30, 2017
bdba20d
undid an unintended change in delete
dimitri-yatsenko Oct 30, 2017
919efba
implemented declaration of external fields
dimitri-yatsenko Oct 30, 2017
e30dfb2
added tests for external storage
dimitri-yatsenko Oct 30, 2017
259950d
minor cleanup
dimitri-yatsenko Oct 30, 2017
3a7f416
minor cleanup
dimitri-yatsenko Oct 30, 2017
3594c67
added external storage tests
dimitri-yatsenko Nov 5, 2017
7eb114c
Merge branch 'master' of https://github.com/datajoint/datajoint-python
dimitri-yatsenko Nov 5, 2017
4d1af79
Completed basic implementation of external storage.
dimitri-yatsenko Nov 5, 2017
4a58a9a
ERD does not show dependencies on external storage
dimitri-yatsenko Nov 5, 2017
1ca0b09
again, the ERD no longer includes references to ~external
dimitri-yatsenko Nov 5, 2017
add950f
fixed #328: the jobs table now records the error stack
dimitri-yatsenko Nov 5, 2017
90c021e
fixes for #328
dimitri-yatsenko Nov 5, 2017
782a9a5
fixed #388 -- a more elegant way to skip duplicates in insert
dimitri-yatsenko Nov 5, 2017
383595d
followup to previous commit
dimitri-yatsenko Nov 5, 2017
794dc47
made insert from query more consistent with insert from variables
dimitri-yatsenko Nov 5, 2017
5ab3381
fixed issue #381 -- better error messages for syntax errors in declar…
dimitri-yatsenko Nov 5, 2017
4b2671e
typo from previous commit
dimitri-yatsenko Nov 6, 2017
9a0b902
set the strict mode at connection time
dimitri-yatsenko Nov 6, 2017
7302571
set sql_mode in connection
dimitri-yatsenko Nov 6, 2017
569881a
updated the sql_mode
dimitri-yatsenko Nov 6, 2017
86c2480
added tests for union and for external storage. Other minor fixes bas…
dimitri-yatsenko Nov 13, 2017
6f7c6bd
improved documentation and error messages for fetch and fetch1. Fixe…
dimitri-yatsenko Nov 14, 2017
8018a9d
added tests for external storage
dimitri-yatsenko Nov 15, 2017
7627569
changed the shape of the computed nodes in the ERD to elipse to avoid…
dimitri-yatsenko Nov 15, 2017
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
17 changes: 11 additions & 6 deletions datajoint/autopopulate.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,9 @@
import logging
import datetime
import random
from tqdm import tqdm
from pymysql import OperationalError
from .relational_operand import RelationalOperand, AndList
from .relational_operand import RelationalOperand, AndList, U
from . import DataJointError
from . import key as KEY
from .base_relation import FreeRelation
Expand Down Expand Up @@ -63,7 +64,7 @@ def _job_key(self, key):
"""
return key

def populate(self, *restrictions, suppress_errors=False, reserve_jobs=False, order="original", limit=None):
def populate(self, *restrictions, suppress_errors=False, reserve_jobs=False, order="original", limit=None, report_progress=False):
"""
rel.populate() calls rel._make_tuples(key) for every primary key in self.key_source
for which there is not already a tuple in rel.
Expand All @@ -73,6 +74,7 @@ def populate(self, *restrictions, suppress_errors=False, reserve_jobs=False, ord
:param reserve_jobs: if true, reserves job to populate in asynchronous fashion
:param order: "original"|"reverse"|"random" - the order of execution
:param limit: if not None, populates at max that many keys
:param report_progress: if True, report progress_bar
"""
if self.connection.in_transaction:
raise DataJointError('Populate cannot be called during a transaction.')
Expand All @@ -84,10 +86,12 @@ def populate(self, *restrictions, suppress_errors=False, reserve_jobs=False, ord
todo = self.key_source
if not isinstance(todo, RelationalOperand):
raise DataJointError('Invalid key_source value')
todo = todo.proj() & AndList(restrictions)
todo = (todo & AndList(restrictions)).proj()
if any(name not in self.target.heading for name in todo.heading):
raise DataJointError('The populated target must have all the attributes of the key source')
todo -= self.target

error_list = [] if suppress_errors else None

jobs = self.connection.jobs[self.target.database] if reserve_jobs else None

# define and setup signal handler for SIGTERM
Expand All @@ -97,15 +101,14 @@ def handler(signum, frame):
raise SystemExit('SIGTERM received')
old_handler = signal.signal(signal.SIGTERM, handler)

todo -= self.target
keys = todo.fetch(KEY, limit=limit)
if order == "reverse":
keys.reverse()
elif order == "random":
random.shuffle(keys)

logger.info('Found %d keys to populate' % len(keys))
for key in keys:
for key in (tqdm(keys) if report_progress else keys):
if not reserve_jobs or jobs.reserve(self.target.table_name, self._job_key(key)):
self.connection.start_transaction()
if key in self.target: # already populated
Expand Down Expand Up @@ -148,6 +151,8 @@ def progress(self, *restrictions, display=True):
:return: remaining, total -- tuples to be populated
"""
todo = self.key_source & AndList(restrictions)
if any(name not in self.target.heading for name in todo.heading):
raise DataJointError('The populated target must have all the attributes of the key source')
total = len(todo)
remaining = len(todo.proj() - self.target)
if display:
Expand Down
5 changes: 3 additions & 2 deletions datajoint/base_relation.py
Original file line number Diff line number Diff line change
Expand Up @@ -139,11 +139,11 @@ def insert(self, rows, replace=False, ignore_errors=False, skip_duplicates=False

if isinstance(rows, RelationalOperand):
# INSERT FROM SELECT - build alternate field-narrowing query (only) when needed
if ignore_extra_fields and not all(name in self.heading.names for name in rows.heading.names):
if ignore_extra_fields and not all(name in self.heading for name in rows.heading):
query = 'INSERT{ignore} INTO {table} ({fields}) SELECT {fields} FROM ({select}) as `__alias`'.format(
ignore=" IGNORE" if ignore_errors or skip_duplicates else "",
table=self.full_table_name,
fields='`'+'`,`'.join(self.heading.names)+'`',
fields='`'+'`,`'.join(name for name in self.heading if name in rows.heading) + '`',
select=rows.make_sql())
else:
query = 'INSERT{ignore} INTO {table} ({fields}) {select}'.format(
Expand All @@ -155,6 +155,7 @@ def insert(self, rows, replace=False, ignore_errors=False, skip_duplicates=False
self.connection.query(query)
except pymysql.err.InternalError as err:
if err.args[0] == server_error_codes['unknown column']:
print(query)
# args[1] -> Unknown column 'extra' in 'field list'
raise DataJointError('%s : To ignore extra fields, set ignore_extra_fields=True in insert.' % err.args[1])
else:
Expand Down
67 changes: 58 additions & 9 deletions datajoint/relational_operand.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import collections
from itertools import zip_longest
from itertools import zip_longest, count
import logging
import numpy as np
import re
Expand Down Expand Up @@ -237,6 +237,12 @@ def __mul__(self, other):
"""
return other * self if isinstance(other, U) else Join.create(self, other)

def __add__(self, other):
"""
union of relations
"""
return Union.create(self, other)

def proj(self, *attributes, **named_attributes):
"""
Relational projection operator.
Expand Down Expand Up @@ -606,9 +612,57 @@ def from_clause(self):
from2=self._arg2.from_clause)


class Union(RelationalOperand):
"""
Union is a private DataJoint class that implements relational union.
"""

__count = count()

def __init__(self, arg=None):
super().__init__(arg)
if arg is not None:
assert isinstance(arg, Union), "Union copy constructore requires a Union object"
self._connection = arg.connection
self._heading = arg.heading
self._arg1 = arg._arg1
self._arg2 = arg._arg2

@classmethod
def create(cls, arg1, arg2):
obj = cls()
if not isinstance(arg1, RelationalOperand) or not isinstance(arg2, RelationalOperand):
raise DataJointError('a relation can only be unioned with another relation')
if arg1.connection != arg2.connection:
raise DataJointError("Cannot operate on relations from different connections.")
if set(arg1.heading.names) != set(arg2.heading.names):
raise DataJointError('Union requires the same attributes in both arguments')
if not all(v.in_key for v in arg1.heading.attributes.values()):
raise DataJointError('The left argument of union must not have any secondary attributes')
obj._connection = arg1.connection
obj._heading = arg1.heading
obj._arg1 = arg1
obj._arg2 = arg2
return obj

def make_sql(self, select_fields=None):
return "SELECT {_fields} FROM {_from}{_where}".format(
_fields=self.get_select_fields(select_fields),
_from=self.from_clause,
_where=self.where_clause)

@property
def from_clause(self):
return ("(SELECT {fields} FROM {from1}{where1} UNION SELECT {fields} FROM {from2}{where2}) as `_u%x`".format(
fields=self.get_select_fields(None), from1=self._arg1.from_clause,
where1=self._arg1.where_clause,
from2=self._arg2.from_clause,
where2=self._arg2.where_clause)) % next(self.__count)


class Projection(RelationalOperand):
"""
Projection is an private DataJoint class that implements relational projection.
Projection is a private DataJoint class that implements relational projection.
See RelationalOperand.proj() for user interface.
"""

Expand Down Expand Up @@ -723,7 +777,7 @@ class Subquery(RelationalOperand):
The attribute list and the WHERE clause are resolved. Thus, a subquery no longer has any renamed attributes.
A subquery of a subquery is a just a copy of the subquery with no change in SQL.
"""
__counter = 0
__count = count()

def __init__(self, arg=None):
super().__init__(arg)
Expand All @@ -745,14 +799,9 @@ def create(cls, arg):
obj._arg = arg
return obj

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

@property
def from_clause(self):
return '(' + self._arg.make_sql() + ') as `_s%x`' % self.counter
return '(' + self._arg.make_sql() + ') as `_s%x`' % next(self.__count)

def get_select_fields(self, select_fields=None):
return '*' if select_fields is None else self.heading.project(select_fields).as_sql
Expand Down
1 change: 1 addition & 0 deletions requirements.txt
Original file line number Diff line number Diff line change
Expand Up @@ -2,5 +2,6 @@ numpy
pymysql>=0.7.2
pyparsing
ipython
tqdm
networkx~=1.11
pydotplus
1 change: 1 addition & 0 deletions test_requirements.txt
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
matplotlib
pygraphviz
tqdm