expression.py
Aggregation
¶
Bases: QueryExpression
Aggregation.create(arg, group, comp1='calc1', ..., compn='calcn') yields an entity set with primary key from arg. The computed arguments comp1, ..., compn use aggregation calculations on the attributes of group or simple projections and calculations on the attributes of arg. Aggregation is used QueryExpression.aggr and U.aggr. Aggregation is a private class in DataJoint, not exposed to users.
Source code in datajoint/expression.py
654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 |
|
QueryExpression
¶
QueryExpression implements query operators to derive new entity set from its input. A QueryExpression object generates a SELECT statement in SQL. QueryExpression operators are restrict, join, proj, aggr, and union.
A QueryExpression object has a support, a restriction (an AndList), and heading.
Property heading
(type dj.Heading) contains information about the attributes.
It is loaded from the database and updated by proj.
Property support
is the list of table names or other QueryExpressions to be joined.
The restriction is applied first without having access to the attributes generated by the projection. Then projection is applied by selecting modifying the heading attribute.
Application of operators does not always lead to the creation of a subquery.
A subquery is generated when
- A restriction is applied on any computed or renamed attributes
- A projection is applied remapping remapped attributes
- Subclasses: Join, Aggregation, and Union have additional specific rules.
Source code in datajoint/expression.py
23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 |
|
__add__(other)
¶
union e.g. q1 + q2
.
Source code in datajoint/expression.py
320 321 322 |
|
__and__(restriction)
¶
Restriction operator e.g. q1 & q2
.
:return: a restricted copy of the input argument
See QueryExpression.restrict for more detail.
Source code in datajoint/expression.py
219 220 221 222 223 224 225 |
|
__bool__()
¶
:return: True if the result is not empty. Equivalent to len(self) > 0 but often
faster e.g. bool(q1)
.
Source code in datajoint/expression.py
552 553 554 555 556 557 558 559 560 561 562 563 |
|
__contains__(item)
¶
returns True if the restriction in item matches any entries in self
e.g. restriction in q1
.
:param item: any restriction (item in query_expression) is equivalent to bool(query_expression & item) but may be executed more efficiently.
Source code in datajoint/expression.py
565 566 567 568 569 570 571 572 573 574 |
|
__iter__()
¶
returns an iterator-compatible QueryExpression object e.g. iter(q1)
.
:param self: iterator-compatible QueryExpression object
Source code in datajoint/expression.py
576 577 578 579 580 581 582 583 584 |
|
__len__()
¶
:return: number of elements in the result set e.g. len(q1)
.
Source code in datajoint/expression.py
534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 |
|
__matmul__(other)
¶
Permissive join of query expressions self
and other
ignoring compatibility check
e.g. q1 @ q2
.
Source code in datajoint/expression.py
261 262 263 264 265 266 267 268 |
|
__mul__(other)
¶
join of query expressions self
and other
e.g. q1 * q2
.
Source code in datajoint/expression.py
255 256 257 258 259 |
|
__neg__()
¶
Convert between restriction and inverted restriction e.g. -q1
.
:return: target restriction
See QueryExpression.restrict for more detail.
Source code in datajoint/expression.py
245 246 247 248 249 250 251 252 253 |
|
__next__()
¶
returns the next record on an iterator-compatible QueryExpression object
e.g. next(q1)
.
:param self: A query expression
:type self: :class:QueryExpression
:rtype: dict
Source code in datajoint/expression.py
586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 |
|
__repr__()
¶
returns the string representation of a QueryExpression object e.g. str(q1)
.
:param self: A query expression
:type self: :class:QueryExpression
:rtype: str
Source code in datajoint/expression.py
631 632 633 634 635 636 637 638 639 640 641 642 643 |
|
__sub__(restriction)
¶
Inverted restriction e.g. q1 - q2
.
:return: a restricted copy of the input argument
See QueryExpression.restrict for more detail.
Source code in datajoint/expression.py
237 238 239 240 241 242 243 |
|
__xor__(restriction)
¶
Permissive restriction operator ignoring compatibility check e.g. q1 ^ q2
.
Source code in datajoint/expression.py
227 228 229 230 231 232 233 234 235 |
|
aggr(group, *attributes, keep_all_rows=False, **named_attributes)
¶
Aggregation of the type U('attr1','attr2').aggr(group, computation="QueryExpression")
has the primary key ('attr1','attr2') and performs aggregation computations for all matching elements of group
.
:param group: The query expression to be aggregated. :param keep_all_rows: True=keep all the rows from self. False=keep only rows that match entries in group. :param named_attributes: computations of the form new_attribute="sql expression on attributes of group" :return: The derived query expression
Source code in datajoint/expression.py
482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 |
|
connection()
property
¶
a dj.Connection object
Source code in datajoint/expression.py
58 59 60 61 62 |
|
cursor(offset=0, limit=None, order_by=None, as_dict=False)
¶
See expression.fetch() for input description. :return: query cursor
Source code in datajoint/expression.py
616 617 618 619 620 621 622 623 624 625 626 627 628 629 |
|
head(limit=25, **fetch_kwargs)
¶
shortcut to fetch the first few entries from query expression. Equivalent to fetch(order_by="KEY", limit=25)
:param limit: number of entries :param fetch_kwargs: kwargs for fetch :return: query result
Source code in datajoint/expression.py
512 513 514 515 516 517 518 519 520 521 |
|
heading()
property
¶
a dj.Heading object, reflects the effects of the projection operator .proj
Source code in datajoint/expression.py
70 71 72 73 |
|
join(other, semantic_check=True, left=False)
¶
create the joined QueryExpression. a * b is short for A.join(B) a @ b is short for A.join(B, semantic_check=False) Additionally, left=True will retain the rows of self, effectively performing a left join.
Source code in datajoint/expression.py
270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 |
|
make_sql(fields=None)
¶
Make the SQL SELECT statement.
:param fields: used to explicitly set the select attributes
Source code in datajoint/expression.py
121 122 123 124 125 126 127 128 129 130 131 132 |
|
make_subquery()
¶
create a new SELECT statement where self is the FROM clause
Source code in datajoint/expression.py
135 136 137 138 139 140 141 |
|
original_heading()
property
¶
a dj.Heading object reflecting the attributes before projection
Source code in datajoint/expression.py
75 76 77 78 |
|
preview(limit=None, width=None)
¶
:return: a string of preview of the contents of the query.
Source code in datajoint/expression.py
645 646 647 |
|
proj(*attributes, **named_attributes)
¶
Projection operator.
:param attributes: attributes to be included in the result. (The primary key is already included). :param named_attributes: new attributes computed or renamed from existing attributes. :return: the projected expression. Primary key attributes cannot be excluded but may be renamed. If the attribute list contains an Ellipsis ..., then all secondary attributes are included too Prefixing an attribute name with a dash '-attr' removes the attribute from the list if present. Keyword arguments can be used to rename attributes as in name='attr', duplicate them as in name='(attr)', or self.proj(...) or self.proj(Ellipsis) -- include all attributes (return self) self.proj() -- include only primary key self.proj('attr1', 'attr2') -- include primary key and attributes attr1 and attr2 self.proj(..., '-attr1', '-attr2') -- include all attributes except attr1 and attr2 self.proj(name1='attr1') -- include primary key and 'attr1' renamed as name1 self.proj('attr1', dup='(attr1)') -- include primary key and attribute attr1 twice, with the duplicate 'dup' self.proj(k='abs(attr1)') adds the new attribute k with the value computed as an expression (SQL syntax) from other attributes available before the projection. Each attribute name can only be used once.
Source code in datajoint/expression.py
324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 |
|
restrict(restriction)
¶
Produces a new expression with the new restriction applied. rel.restrict(restriction) is equivalent to rel & restriction. rel.restrict(Not(restriction)) is equivalent to rel - restriction The primary key of the result is unaffected. Successive restrictions are combined as logical AND: r & a & b is equivalent to r & AndList((a, b)) Any QueryExpression, collection, or sequence other than an AndList are treated as OrLists (logical disjunction of conditions) Inverse restriction is accomplished by either using the subtraction operator or the Not class.
The expressions in each row equivalent:
rel & True rel rel & False the empty entity set rel & 'TRUE' rel rel & 'FALSE' the empty entity set rel - cond rel & Not(cond) rel - 'TRUE' rel & False rel - 'FALSE' rel rel & AndList((cond1,cond2)) rel & cond1 & cond2 rel & AndList() rel rel & [cond1, cond2] rel & OrList((cond1, cond2)) rel & [] rel & False rel & None rel & False rel & any_empty_entity_set rel & False rel - AndList((cond1,cond2)) rel & [Not(cond1), Not(cond2)] rel - [cond1, cond2] rel & Not(cond1) & Not(cond2) rel - AndList() rel & False rel - [] rel rel - None rel rel - any_empty_entity_set rel
When arg is another QueryExpression, the restriction rel & arg restricts rel to elements that match at least one element in arg (hence arg is treated as an OrList). Conversely, rel - arg restricts rel to elements that do not match any elements in arg. Two elements match when their common attributes have equal values or when they have no common attributes. All shared attributes must be in the primary key of either rel or arg or both or an error will be raised.
QueryExpression.restrict is the only access point that modifies restrictions. All other operators must ultimately call restrict()
:param restriction: a sequence or an array (treated as OR list), another QueryExpression, an SQL condition string, or an AndList.
Source code in datajoint/expression.py
143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 |
|
restriction()
property
¶
a AndList object of restrictions applied to input to produce the result
Source code in datajoint/expression.py
80 81 82 83 84 85 |
|
restriction_attributes()
property
¶
the set of attribute names invoked in the WHERE clause
Source code in datajoint/expression.py
87 88 89 90 91 92 |
|
support()
property
¶
A list of table names or subqueries to from the FROM clause
Source code in datajoint/expression.py
64 65 66 67 68 |
|
tail(limit=25, **fetch_kwargs)
¶
shortcut to fetch the last few entries from query expression. Equivalent to fetch(order_by="KEY DESC", limit=25)[::-1]
:param limit: number of entries :param fetch_kwargs: kwargs for fetch :return: query result
Source code in datajoint/expression.py
523 524 525 526 527 528 529 530 531 532 |
|
U
¶
dj.U objects are the universal sets representing all possible values of their attributes. dj.U objects cannot be queried on their own but are useful for forming some queries. dj.U('attr1', ..., 'attrn') represents the universal set with the primary key attributes attr1 ... attrn. The universal set is the set of all possible combinations of values of the attributes. Without any attributes, dj.U() represents the set with one element that has no attributes.
Restriction:
dj.U can be used to enumerate unique combinations of values of attributes from other expressions.
The following expression yields all unique combinations of contrast and brightness found in the stimulus
set:
dj.U('contrast', 'brightness') & stimulus
Aggregation:
In aggregation, dj.U is used for summary calculation over an entire set:
The following expression yields one element with one attribute s
containing the total number of elements in
query expression expr
:
dj.U().aggr(expr, n='count(*)')
The following expressions both yield one element containing the number n
of distinct values of attribute attr
in
query expressio expr
.
dj.U().aggr(expr, n='count(distinct attr)') dj.U().aggr(dj.U('attr').aggr(expr), 'n=count(*)')
The following expression yields one element and one attribute s
containing the sum of values of attribute attr
over entire result set of expression expr
:
dj.U().aggr(expr, s='sum(attr)')
The following expression yields the set of all unique combinations of attributes attr1
, attr2
and the number of
their occurrences in the result set of query expression expr
.
dj.U(attr1,attr2).aggr(expr, n='count(*)')
Joins:
If expression expr
has attributes 'attr1' and 'attr2', then expr * dj.U('attr1','attr2') yields the same result
as expr
but attr1
and attr2
are promoted to the the primary key. This is useful for producing a join on
non-primary key attributes.
For example, if attr
is in both expr1 and expr2 but not in their primary keys, then expr1 * expr2 will throw
an error because in most cases, it does not make sense to join on non-primary key attributes and users must first
rename attr
in one of the operands. The expression dj.U('attr') * rel1 * rel2 overrides this constraint.
Source code in datajoint/expression.py
812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 |
|
__mul__(other)
¶
shorthand for join
Source code in datajoint/expression.py
908 909 910 |
|
aggr(group, **named_attributes)
¶
Aggregation of the type U('attr1','attr2').aggr(group, computation="QueryExpression")
has the primary key ('attr1','attr2') and performs aggregation computations for all matching elements of group
.
:param group: The query expression to be aggregated. :param named_attributes: computations of the form new_attribute="sql expression on attributes of group" :return: The derived query expression
Source code in datajoint/expression.py
912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 |
|
join(other, left=False)
¶
Joining U with a query expression has the effect of promoting the attributes of U to the primary key of the other query expression.
:param other: the other query expression to join with. :param left: ignored. dj.U always acts as if left=False :return: a copy of the other query expression with the primary key extended.
Source code in datajoint/expression.py
881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 |
|
Union
¶
Bases: QueryExpression
Union is the private DataJoint class that implements the union operator.
Source code in datajoint/expression.py
728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 |
|
from_clause()
¶
The union does not use a FROM clause
Source code in datajoint/expression.py
790 791 792 |
|
where_clause()
¶
The union does not use a WHERE clause
Source code in datajoint/expression.py
794 795 796 |
|