Exploration in CDF Notebook¶
Prerequisites
- Access to a CDF Project.
- Some very basic Python understanding or feel up for a challenge.
If you have ZERO Python knowledge and are up for a challenge¶
- You can copy the cells below and past them into notebook.
- You execute cells by pressing
shift+enter - Auto complete with
tab. - You can check available commands by first type a variable and press
shift+tab. For example, after running the linepygen = generate_sdk_notebook(data_model_id, client)you can in subsequent cells writepygen.and pressshift+tabto see that there are options such aswind_turbineandmetmastavailable .
Installing pygen in the notebook environment.¶
In [1]:
Copied!
%pip install cognite-pygen
%pip install cognite-pygen
In case you have issues with the installation, check out the troubleshooting in the installation guide.
Generating SDK for Your Data Model¶
In [2]:
Copied!
from cognite.client import CogniteClient
from cognite.pygen import __version__, generate_sdk_notebook
from cognite.client import CogniteClient
from cognite.pygen import __version__, generate_sdk_notebook
In [3]:
Copied!
# Show version, if you have an older you can run
# the installation cell above with:
# %pip install cognite-pygen==0.32.5
__version__
# Show version, if you have an older you can run
# the installation cell above with:
# %pip install cognite-pygen==0.32.5
__version__
Out[3]:
'0.99.49'
In [4]:
Copied!
client = CogniteClient()
client = CogniteClient()
In [5]:
Copied!
space = "sp_pygen_power"
data_model_external_id = "WindTurbine"
data_model_version = "1"
data_model_id = (space, data_model_external_id, data_model_version)
space = "sp_pygen_power"
data_model_external_id = "WindTurbine"
data_model_version = "1"
data_model_id = (space, data_model_external_id, data_model_version)
In [6]:
Copied!
pygen = generate_sdk_notebook(data_model_id, client)
pygen = generate_sdk_notebook(data_model_id, client)
Successfully retrieved data model(s) ('sp_pygen_power', 'WindTurbine', '1')
Name collision detected in ViewId(space='sp_pygen_power', external_id='SensorTimeSeries', version='1'): 'type'. An underscore will be added to the 'type' to avoid name collision.
Writing SDK to /tmp/pygen/sp_pygen_power_WindTurbine_1/wind_turbine
Done!
Added /tmp/pygen/sp_pygen_power_WindTurbine_1 to sys.path to enable import
Imported wind_turbine
You can now use the generated SDK in the current Python session.
The data classes are available by importing, for example, `from wind_turbine.data_classes import BladeWrite`
Using the Generated SDK¶
In [7]:
Copied!
pygen.wind_turbine.list(limit=5)
pygen.wind_turbine.list(limit=5)
Out[7]:
| space | external_id | name | capacity | windfarm | rotor | nacelle | blades | datasheets | data_record | |
|---|---|---|---|---|---|---|---|---|---|---|
| 0 | sp_wind | hornsea_1_mill_3 | hornsea_1_mill_3 | 7.0 | Hornsea 1 | {'space': 'sp_wind', 'external_id': 'hornsea_1... | {'space': 'sp_wind', 'external_id': 'hornsea_1... | [{'space': 'sp_wind', 'external_id': 'hornsea_... | [{'space': 'sp_wind', 'external_id': 'windmill... | {'version': 4, 'last_updated_time': 2024-11-16... |
| 1 | sp_wind | hornsea_1_mill_2 | hornsea_1_mill_2 | 7.0 | Hornsea 1 | {'space': 'sp_wind', 'external_id': 'hornsea_1... | {'space': 'sp_wind', 'external_id': 'hornsea_1... | [{'space': 'sp_wind', 'external_id': 'hornsea_... | [{'space': 'sp_wind', 'external_id': 'windmill... | {'version': 4, 'last_updated_time': 2024-11-16... |
| 2 | sp_wind | hornsea_1_mill_1 | hornsea_1_mill_1 | 7.0 | Hornsea 1 | {'space': 'sp_wind', 'external_id': 'hornsea_1... | {'space': 'sp_wind', 'external_id': 'hornsea_1... | [{'space': 'sp_wind', 'external_id': 'hornsea_... | [{'space': 'sp_wind', 'external_id': 'windmill... | {'version': 4, 'last_updated_time': 2024-11-16... |
| 3 | sp_wind | hornsea_1_mill_4 | hornsea_1_mill_4 | 7.0 | Hornsea 1 | {'space': 'sp_wind', 'external_id': 'hornsea_1... | {'space': 'sp_wind', 'external_id': 'hornsea_1... | [{'space': 'sp_wind', 'external_id': 'hornsea_... | [{'space': 'sp_wind', 'external_id': 'windmill... | {'version': 4, 'last_updated_time': 2024-11-16... |
| 4 | sp_wind | hornsea_1_mill_5 | hornsea_1_mill_5 | 7.0 | Hornsea 1 | {'space': 'sp_wind', 'external_id': 'hornsea_1... | {'space': 'sp_wind', 'external_id': 'hornsea_1... | [{'space': 'sp_wind', 'external_id': 'hornsea_... | [{'space': 'sp_wind', 'external_id': 'windmill... | {'version': 4, 'last_updated_time': 2024-11-16... |
In [8]:
Copied!
pygen.metmast.list(limit=2)
pygen.metmast.list(limit=2)
Out[8]:
| space | external_id | position | wind_speed | tilt_angle | temperature | data_record | |
|---|---|---|---|---|---|---|---|
| 0 | sp_wind | utsira_station | 62.0 | utsira_station_wind_speed | utsira_station_tilt_angle | utsira_station_temperature | {'version': 1, 'last_updated_time': 2024-11-16... |
| 1 | sp_wind | hitra_station | 63.0 | hitra_station_wind_speed | hitra_station_tilt_angle | hitra_station_temperature | {'version': 1, 'last_updated_time': 2024-11-16... |
To see a more complete example of functionality for the generated client see the Usage section in the documentation.
Generating SDK using Demo Data Model¶
In case you want to try out pygen, but do not have a data model with data available, pygen comes with a demo data model included in the package.
In [9]:
Copied!
from cognite.client import CogniteClient
from cognite.pygen import demo
from cognite.client import CogniteClient
from cognite.pygen import demo
In [10]:
Copied!
solar = demo.SolarFarmAPM()
client = CogniteClient()
solar = demo.SolarFarmAPM()
client = CogniteClient()
In [11]:
Copied!
%%time
farm = solar.create(client)
%%time
farm = solar.create(client)
Created space pygen Deployed data model DataModelId(space='pygen', external_id='SolarFarmAPM', version='1') ✅ Data Model Ready! Created 4 TimeSeries Created 3 FileMetadata Created 17 nodes Created 21 edges ✅ Population Complete! Writing SDK to /tmp/pygen Done! /tmp/pygen already in sys.path Imported solar_farm_apm.client ✅ SDK Generated! CPU times: user 0 ns, sys: 0 ns, total: 0 ns Wall time: 5.32 s
Use Generated SDK¶
We can now inspect the data model using our newly generated SDK.
In [12]:
Copied!
farm.assets.list()
farm.assets.list()
Out[12]:
| external_id | area_id | category_id | created_date | description | documents | is_active | is_critical_line | linked_assets | metrics | name | parent | source_db | tag | updated_date | version | last_updated_time | created_time | deleted_time | |
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
| 0 | solarfarm:fornebu | 1 | 1 | 2017-01-01 00:00:00+00:00 | The most sunny place in Norway | [] | True | True | [] | [] | Fornebu | None | AlbertCerebrum | 2023-01-01 00:00:00+00:00 | 2 | 2023-08-20 10:41:56.033000+00:00 | 2023-08-20 10:41:56.033000+00:00 | None | |
| 1 | solarpanel:sunny | 2 | 2 | 2017-01-01 00:00:00+00:00 | The panel on the first building | [] | True | False | [inverter:sunny] | [] | Solar Panel Sunny | solarfarm:fornebu | AlbertCerebrum | 2022-01-01 00:00:00+00:00 | 1 | 2023-08-20 10:41:56.033000+00:00 | 2023-08-20 10:41:56.033000+00:00 | None | |
| 2 | solarpanel:cloudy | 3 | 2 | 2020-01-01 00:00:00+00:00 | The panel on the second building | [] | True | True | [inverter:cloudy] | [] | Solar Panel Cloudy | solarfarm:fornebu | AlbertCerebrum | 2022-01-01 00:00:00+00:00 | 1 | 2023-08-20 10:41:56.033000+00:00 | 2023-08-20 10:41:56.033000+00:00 | None | |
| 3 | transformer:sunny | 2 | 3 | 2017-01-01 00:00:00+00:00 | The transformer connected to sunny | [] | True | False | [substation:fornebu] | [] | Transformer Sunny | solarfarm:fornebu | AlbertCerebrum | 2022-01-01 00:00:00+00:00 | 1 | 2023-08-20 10:41:56.033000+00:00 | 2023-08-20 10:41:56.033000+00:00 | None | |
| 4 | transformer:cloudy | 3 | 3 | 2017-01-01 00:00:00+00:00 | The transformer connected to cloudy | [] | True | False | [substation:fornebu] | [] | Transformer Cloudy | solarfarm:fornebu | AlbertCerebrum | 2021-01-01 00:00:00+00:00 | 1 | 2023-08-20 10:41:56.033000+00:00 | 2023-08-20 10:41:56.033000+00:00 | None | |
| 5 | transformer:saver | 4 | 3 | 2022-01-01 00:00:00+00:00 | The transformer connected to battery | [] | True | True | [substation:fornebu] | [] | Transformer Saver | solarfarm:fornebu | AlbertCerebrum | 2023-03-01 00:00:00+00:00 | 1 | 2023-08-20 10:41:56.033000+00:00 | 2023-08-20 10:41:56.033000+00:00 | None | |
| 6 | inverter:sunny | 2 | 4 | 2017-01-01 00:00:00+00:00 | The inverter connected to sunny | [] | True | False | [transformer:sunny] | [] | Inverter Sunny | solarfarm:fornebu | AlbertCerebrum | 2022-01-01 00:00:00+00:00 | 1 | 2023-08-20 10:41:56.033000+00:00 | 2023-08-20 10:41:56.033000+00:00 | None | |
| 7 | inverter:cloudy | 3 | 4 | 2017-01-01 00:00:00+00:00 | The inverter connected to cloudy | [] | True | False | [transformer:cloudy] | [] | Inverter Cloudy | solarfarm:fornebu | AlbertCerebrum | 2021-01-01 00:00:00+00:00 | 1 | 2023-08-20 10:41:56.033000+00:00 | 2023-08-20 10:41:56.033000+00:00 | None | |
| 8 | inverter:saver | 4 | 4 | 2022-01-01 00:00:00+00:00 | The inverter connected to battery | [] | True | True | [transformer:saver] | [] | Inverter Saver | solarfarm:fornebu | AlbertCerebrum | 2023-03-01 00:00:00+00:00 | 1 | 2023-08-20 10:41:56.033000+00:00 | 2023-08-20 10:41:56.033000+00:00 | None | |
| 9 | substation:fornebu | 1 | 4 | 2017-01-01 00:00:00+00:00 | The substation for the solar farm | [] | True | True | [] | [] | Fornebu station | solarfarm:fornebu | AlbertCerebrum | 2023-03-01 00:00:00+00:00 | 1 | 2023-08-20 10:41:56.033000+00:00 | 2023-08-20 10:41:56.033000+00:00 | None | |
| 10 | battery:winter | 4 | 5 | 2020-01-01 00:00:00+00:00 | The battery for the solar farm | [] | True | True | [inverter:saver] | [] | Saver | solarfarm:fornebu | AlbertCerebrum | 2020-06-01 00:00:00+00:00 | 1 | 2023-08-20 10:41:56.033000+00:00 | 2023-08-20 10:41:56.033000+00:00 | None |
In [13]:
Copied!
farm.work_orders.retrieve("planned:1001").dict()
farm.work_orders.retrieve("planned:1001").dict()
Out[13]:
{'external_id': 'planned:1001',
'version': 2,
'last_updated_time': datetime.datetime(2023, 8, 20, 10, 41, 56, 33000, tzinfo=datetime.timezone.utc),
'created_time': datetime.datetime(2023, 8, 20, 10, 41, 56, 33000, tzinfo=datetime.timezone.utc),
'deleted_time': None,
'actual_hours': 3,
'created_date': datetime.datetime(2023, 1, 1, 0, 0, tzinfo=datetime.timezone.utc),
'description': 'Inspect solar panels',
'due_date': datetime.datetime(2023, 1, 1, 0, 0, tzinfo=datetime.timezone.utc),
'duration_hours': 5,
'end_time': datetime.datetime(2023, 1, 2, 0, 0, tzinfo=datetime.timezone.utc),
'is_active': False,
'is_cancelled': False,
'is_completed': True,
'is_safety_critical': False,
'linked_assets': ['solarpanel:sunny', 'solarpanel:cloudy'],
'percentage_progress': 100,
'planned_start': datetime.datetime(2023, 1, 1, 0, 0, tzinfo=datetime.timezone.utc),
'priority_description': 'Medium Planned Priority',
'program_number': '1',
'start_time': datetime.datetime(2023, 1, 1, 0, 0, tzinfo=datetime.timezone.utc),
'status': 'Done',
'title': 'Planned Inspection',
'work_items': ['work:inspection:1001', 'work:cleaning:1001'],
'work_order_number': '1',
'work_package_number': '1'}
Cleaning Up¶
Once you are finished with testing and want to remove the data model and data, you can use the .clean() method.
In [14]:
Copied!
solar.clean(client, auto_confirm=True)
solar.clean(client, auto_confirm=True)
Deleted 4 TimeSeries Deleted 3 FileMetadata Deleted 17 nodes Deleted 0 edges Deleted 4 type nodes Deleted data model DataModelId(space='pygen', external_id='SolarFarmAPM', version='1') Deleted views [ViewId(space='pygen', external_id='WorkItem', version='e3b9053e1dd01e'), ViewId(space='pygen', external_id='WorkOrder', version='27f589c97cdb01'), ViewId(space='pygen', external_id='Asset', version='a974167b65241a')] Deleted containers [ContainerId(space='pygen', external_id='WorkOrder'), ContainerId(space='pygen', external_id='Asset'), ContainerId(space='pygen', external_id='WorkItem')] Deleted space pygen
In [ ]:
Copied!