Skip to content
GitLab
Explore
Sign in
Register
Primary navigation
Search or go to…
Project
C
caosdb-pylib
Manage
Activity
Members
Labels
Plan
Issues
Issue boards
Milestones
Iterations
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Snippets
Locked files
Build
Pipelines
Jobs
Pipeline schedules
Artifacts
Deploy
Releases
Container registry
Model registry
Operate
Environments
Monitor
Incidents
Analyze
Value stream analytics
Contributor analytics
CI/CD analytics
Repository analytics
Code review analytics
Issue analytics
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
Community forum
Contribute to GitLab
Provide feedback
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
caosdb
Software
caosdb-pylib
Commits
ab058456
Commit
ab058456
authored
2 years ago
by
florian
Browse files
Options
Downloads
Patches
Plain Diff
DOC: Explain reference properties better
parent
10ed8e16
Branches
Branches containing commit
Tags
Tags containing commit
2 merge requests
!107
ENH: add entity getters and cached functions
,
!97
F doc ref properties
Changes
1
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
src/doc/tutorials/Data-Insertion.rst
+71
-18
71 additions, 18 deletions
src/doc/tutorials/Data-Insertion.rst
with
71 additions
and
18 deletions
src/doc/tutorials/Data-Insertion.rst
+
71
−
18
View file @
ab058456
...
@@ -83,33 +83,86 @@ corresponding python class:
...
@@ -83,33 +83,86 @@ corresponding python class:
.. code:: python
.. code:: python
rec = db.Record()
rec = db.Record()
# rec.id is None
rec.add_parent(name="Experiment")
rec.add_parent(name="Experiment")
rec.add_property(name="date", value="2020-01-07")
rec.add_property(name="date", value="2020-01-07")
rec.insert()
rec.insert()
print(rec.id) # rec.id set by the server
Here, the record has a parent, the RecordType “Experiment”, and a Property date
with a value ``"2020-01-07"``. After the successful insertion, our new Record is
assigned an ``id`` by the server. In the following, let's assume this id to be
``256``.
Reference Properties
--------------------
Now suppose we want to insert an analysis that references the above experiment
record as its source data. Since we know that the id of the experiment record is
256, we can do the following:
.. code:: python
ana = db.Record().add_parent(name="Analysis") # Create record and assign parent in one line
ana.add_property(name="Experiment", value=256)
ana.add_propertt(name="date", value="2020-01-08")
# possibly, add more properties here ...
ana.insert()
The experiment record's id is used as the value of the ``Experiment`` property
of the analysis Record (note how we use the RecordType ``Experiment`` as a
``REFERENCE`` property here). Sending a CaosDB query like ``FIND RECORD
Experiment WHICH IS REFERENCED BY A Analysis WITH date=2020-01-08`` would now
return our original experiment record.
Here, the record has a parent: The RecordType “Experiment”. And a
Equivalently, we can also use the Python object of the experiment record, i.e.,
P
roperty:
date.
``rec`` as the value of the ``Experiment`` p
roperty:
Note, that if you want to use a property that is not a primitive
datatype like db.INTEGER and so on, you need to use the ID of the Entity
.. code:: python
that you are referencing.
ana = db.Record().add_parent(name="Analysis")
ana.add_property(name="Experiment", value=rec)
ana.add_propertt(name="date", value="2020-01-08")
# possibly, add more properties here ...
ana.insert()
Finally, we can also insert both records at the same time using a
``db.Container``:
.. code:: python
.. code:: python
rec = db.Record()
rec = db.Record()
rec.add_parent(name="Experiment")
rec.add_parent(name="Experiment")
rec.add_property(name="report", value=235507)
rec.add_property(name="date", value="2020-01-07")
rec.add_property(name="Analysis", value=230007)
ana = db.Record().add_parent(name="Analysis")
rec.insert()
ana.add_property(name="Experiment", value=rec)
ana.add_propertt(name="date", value="2020-01-08")
cont = db.Container().extend([rec, ana]) # Add experiment and analysis
# records to our container
cont.insert() # Insert both at the same time, the CaosDB server will
# resolve the reference upon insertion.
All three ways result in an Analysis record which references an Experiment
record.
.. note::
Instead of using the ``Experiment`` RecordType as a ``REFERENCE`` porperty,
we can also create an actual property with data type ``Experiment``:
``db.property(name="source", datatype="Experiment")``. Now you can add this
property to the analysis record with the experiment record as a value as
explained above. As a rule of thumbs, using a separate property for these
references is meaningful whenever you want to highlight that, e.g., this
particular experiment provided the source data for your analysis (as opposed
to another experiment that was used for validation).
Of course, the IDs 235507 and 230007 need to exist in CaosDB. The first
Advanced insertions
example shows how to use a db.REFERENCE Property (report) and the second
-------------------
shows that you can use any RecordType as Property to reference a Record
that has such a parent.
Most Records do not have name however it can absolutely make sense
. I
n
Most Records do not have
a
name
,
however it can absolutely make sense
to assig
n
that case use the name argument when creating it. Another useful feature
one. In
that case use the name argument when creating it. Another useful feature
is the fact that properties can have units:
is the fact that properties can have units:
.. code:: python
.. code:: python
...
@@ -134,7 +187,7 @@ container. E.g. if you have a python list ``analysis_results``:
...
@@ -134,7 +187,7 @@ container. E.g. if you have a python list ``analysis_results``:
cont.insert()
cont.insert()
Useful is also,
that you can insert directly tabular data.
It may also be usefull to know
that you can insert directly tabular data.
.. code:: python
.. code:: python
...
@@ -144,8 +197,8 @@ Useful is also, that you can insert directly tabular data.
...
@@ -144,8 +197,8 @@ Useful is also, that you can insert directly tabular data.
print(recs)
print(recs)
recs.insert()
recs.insert()
W
ith this example file
Try it yourself w
ith this example file
`test.csv <uploads/4f2c8756a26a3984c0af09d206d583e5/test.csv>`__
.
`test.csv <uploads/4f2c8756a26a3984c0af09d206d583e5/test.csv>`__
!
List Properties
List Properties
---------------
---------------
...
...
This diff is collapsed.
Click to expand it.
Preview
0%
Loading
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Save comment
Cancel
Please
register
or
sign in
to comment