Proj¶
The proj
operator represents projection and is used to select attributes (columns) from a table, to rename them, or to create new calculated attributes.
Simple projection¶
The simple projection selects a subset of attributes of the original table. However, the primary key attributes are always included.
Using the example schema, let table department
have attributes dept, dept_name, dept_address, and dept_phone.
The primary key attribute is in bold.
Then department.proj()
will have attribute dept.
department.proj('dept')
will have attribute dept.
department.proj('dept_name', 'dept_phone')
will have attributes dept, dept_name, and dept_phone.
Renaming¶
In addition to selecting attributes, proj
can rename them.
Any attribute can be renamed, including primary key attributes.
Renaming is done using a string:
tab('old_attr->new_attr')
.
For example, let table tab
have attributes mouse, session, session_date, stimulus, and behavior.
The primary key attributes are in bold.
Then
tab.proj('mouse->animal', 'stimulus')
will have attributes animal, session, and stimulus.
Renaming is often used to control the outcome of a join.
For example, let tab
have attributes slice, and cell.
Then tab * tab
will simply yield tab
.
However,
tab * tab.proj('cell->other')
yields all ordered pairs of all cells in each slice.
Calculations¶
In addition to selecting or renaming attributes, proj
can compute new attributes from existing ones.
For example, let tab
have attributes mouse
, scan
, surface_z
, and scan_z
.
To obtain the new attribute depth
computed as scan_z - surface_z
and then to restrict to
depth > 500
:
tab.proj('scan_z-surface_z -> depth') & 'depth > 500'
Calculations are passed to SQL and are not parsed by DataJoint. For available functions, you may refer to the MySQL documentation.