Exploration Local Notebook
Prerequisites
- Access to a CDF Project.
- Know how to install and setup
Python
. - Launch a
Python
notebook.
Generating SDK for Your Data Model¶
pygen
supports generating an SDK on the fly for easy exploration in a notebook
First, we have a local .toml
file on the following format to setup a CogniteClient
[cognite]
project = "<cdf-project>"
tenant_id = "<tenant-id>"
cdf_cluster = "<cdf-cluster>"
client_id = "<client-id>"
client_secret = "<client-secret>"
Then, we use the following function from pygen
to create a CogniteClient
from cognite.pygen import load_cognite_client_from_toml, generate_sdk_notebook
client = load_cognite_client_from_toml("config.toml")
We can now create a SDK for the following data model.
windmill = generate_sdk_notebook(
("power-models", "Windmill", "1"),
client,
)
Successfully retrieved data model(s) ('power-models', 'Windmill', '1') Writing SDK to C:\Users\ANDERS~1\AppData\Local\Temp\pygen\power-models_Windmill_1 Done! Added C:\Users\ANDERS~1\AppData\Local\Temp\pygen\power-models_Windmill_1 to sys.path to enable import Imported windmill You can now use the generated SDK in the current Python session. The data classes are available by importing, for example, `from windmill.data_classes import BladeApply`
The function above does the following:
- Downloads the data model.
- Generates an SDK.
- Puts it in the temporary folder /
pygen
- Adds that folder to the
sys.path
- Imports the client
- Instantiates then new client
Use Generated SDK¶
windmill.windmill.list(limit=5)
external_id | external_id | node_type | name | windfarm | capacity | rotor | nacelle | blades | metmast | |
---|---|---|---|---|---|---|---|---|---|---|
0 | hornsea_1_mill_3 | hornsea_1_mill_3 | None | hornsea_1_mill_3 | Hornsea 1 | 7.0 | {'space': 'windmill-instances', 'external_id':... | {'space': 'windmill-instances', 'external_id':... | [blade:1, blade:2, blade:3] | None |
1 | hornsea_1_mill_2 | hornsea_1_mill_2 | None | hornsea_1_mill_2 | Hornsea 1 | 7.0 | {'space': 'windmill-instances', 'external_id':... | {'space': 'windmill-instances', 'external_id':... | [blade:4, blade:5, blade:6] | None |
2 | hornsea_1_mill_1 | hornsea_1_mill_1 | None | hornsea_1_mill_1 | Hornsea 1 | 7.0 | {'space': 'windmill-instances', 'external_id':... | {'space': 'windmill-instances', 'external_id':... | [blade:7, blade:8, blade:9] | None |
3 | hornsea_1_mill_4 | hornsea_1_mill_4 | None | hornsea_1_mill_4 | Hornsea 1 | 7.0 | {'space': 'windmill-instances', 'external_id':... | {'space': 'windmill-instances', 'external_id':... | [blade:10, blade:11, blade:12] | None |
4 | hornsea_1_mill_5 | hornsea_1_mill_5 | None | hornsea_1_mill_5 | Hornsea 1 | 7.0 | {'space': 'windmill-instances', 'external_id':... | {'space': 'windmill-instances', 'external_id':... | [blade:13, blade:14, blade:15] | None |
windmill.sensor_position.list(limit=10)
external_id | external_id | node_type | position | flapwise_bend_mom | flapwise_bend_mom_offset | flapwise_bend_mom_crosstalk_corrected | flapwise_bend_mom_offset_crosstalk_corrected | edgewisewise_bend_mom | edgewise_bend_mom_offset | edgewise_bend_mom_crosstalk_corrected | edgewise_bend_mom_offset_crosstalk_corrected | |
---|---|---|---|---|---|---|---|---|---|---|---|---|
0 | sensorposition:1 | sensorposition:1 | None | 19.6 | V52-WindTurbine.MxA6 | None | None | None | None | None | None | None |
1 | sensorposition:2 | sensorposition:2 | None | 6.6 | V52-WindTurbine.MxA3 | V52-WindTurbine.MxA3_O | None | None | V52-WindTurbine.MyA3 | V52-WindTurbine.MyA3_O | None | None |
2 | sensorposition:3 | sensorposition:3 | None | 6.7 | V52-WindTurbine.MxA4 | V52-WindTurbine.MxA4_O | V52-WindTurbine.MxA4cr | V52-WindTurbine.MxA4_Ocr | V52-WindTurbine.MyA4 | V52-WindTurbine.MyA4_O | V52-WindTurbine.MyA4cr | V52-WindTurbine.MyA4_Ocr |
3 | sensorposition:4 | sensorposition:4 | None | 13.1 | V52-WindTurbine.MxA5 | None | None | None | None | None | None | None |
4 | sensorposition:5 | sensorposition:5 | None | 1.4 | V52-WindTurbine.MxA1 | V52-WindTurbine.MxA1_O | V52-WindTurbine.MxA1cr | V52-WindTurbine.MxA1_Ocr | V52-WindTurbine.MyA1 | V52-WindTurbine.MyA1_O | V52-WindTurbine.MyA1cr | V52-WindTurbine.MyA1_Ocr |
5 | sensorposition:6 | sensorposition:6 | None | 1.5 | V52-WindTurbine.MxA2 | V52-WindTurbine.MxA2_O | V52-WindTurbine.MxA2cr | V52-WindTurbine.MxA2_Ocr | V52-WindTurbine.MyA2 | V52-WindTurbine.MyA2_O | V52-WindTurbine.MyA2cr | V52-WindTurbine.MyA2_Ocr |
6 | sensorposition:7 | sensorposition:7 | None | 1.5 | V52-WindTurbine.MxB2 | None | V52-WindTurbine.MxB2cr | None | V52-WindTurbine.MyB2 | None | V52-WindTurbine.MyB2cr | None |
7 | sensorposition:8 | sensorposition:8 | None | 19.6 | None | None | None | None | None | None | None | None |
8 | sensorposition:9 | sensorposition:9 | None | 1.4 | V52-WindTurbine.MxB1 | None | V52-WindTurbine.MxB1cr | None | V52-WindTurbine.MyB1 | None | V52-WindTurbine.MyB1cr | None |
9 | sensorposition:10 | sensorposition:10 | None | 13.1 | None | None | None | None | None | None | None | None |
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.
from cognite.pygen import demo, load_cognite_client_from_toml
solar = demo.SolarFarmAPM()
client = load_cognite_client_from_toml()
%%time
farm = solar.create(client)
Data model DataModelId(space='pygen', external_id='SolarFarmAPM', version='1') already exists, skipping deployment ✅ Data Model Ready! Skipping TimeSeries creation, all 4 TimeSeries already exist Skipping FileMetadata creation, all 3 FileMetadata already exist All 17 nodes already exists All 21 edges already exists ✅ Population Complete! Writing SDK to C:\Users\ANDERS~1\AppData\Local\Temp\pygen Done! Added C:\Users\ANDERS~1\AppData\Local\Temp\pygen to sys.path to enable import Imported solar_farm_apm.client ✅ SDK Generated! CPU times: total: 469 ms Wall time: 3.41 s
Use Generated SDK¶
We can now inspect the data model using our newly generated SDK.
farm.assets.list()
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 07:48:17.799000+00:00 | 2023-08-20 07:48:17.799000+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 07:48:17.799000+00:00 | 2023-08-20 07:48:17.799000+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 07:48:17.799000+00:00 | 2023-08-20 07:48:17.799000+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 07:48:17.799000+00:00 | 2023-08-20 07:48:17.799000+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 07:48:17.799000+00:00 | 2023-08-20 07:48:17.799000+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 07:48:17.799000+00:00 | 2023-08-20 07:48:17.799000+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 07:48:17.799000+00:00 | 2023-08-20 07:48:17.799000+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 07:48:17.799000+00:00 | 2023-08-20 07:48:17.799000+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 07:48:17.799000+00:00 | 2023-08-20 07:48:17.799000+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 07:48:17.799000+00:00 | 2023-08-20 07:48:17.799000+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 07:48:17.799000+00:00 | 2023-08-20 07:48:17.799000+00:00 | None |
farm.work_orders.retrieve("planned:1001")
WorkOrder(external_id='planned:1001', version=2, last_updated_time=datetime.datetime(2023, 8, 20, 7, 48, 17, 799000, tzinfo=TzInfo(UTC)), created_time=datetime.datetime(2023, 8, 20, 7, 48, 17, 799000, tzinfo=TzInfo(UTC)), deleted_time=None, actual_hours=3, created_date=datetime.datetime(2023, 1, 1, 0, 0, tzinfo=TzInfo(UTC)), description='Inspect solar panels', due_date=datetime.datetime(2023, 1, 1, 0, 0, tzinfo=TzInfo(UTC)), duration_hours=5, end_time=datetime.datetime(2023, 1, 2, 0, 0, tzinfo=TzInfo(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=TzInfo(UTC)), priority_description='Medium Planned Priority', program_number='1', start_time=datetime.datetime(2023, 1, 1, 0, 0, tzinfo=TzInfo(UTC)), status='Done', title='Planned Inspection', work_items=['work:inspection:1001', 'work:cleaning:1001'], work_order_number='1', work_package_number='1')
Once we are finished exploting pygen
using the demo data we can remove it with the .clean()
method
solar.clean(client)
About to delete data model DataModelId(space='pygen', external_id='SolarFarmAPM', version='1') About to delete 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')] About to delete containers [ContainerId(space='pygen', external_id='Asset'), ContainerId(space='pygen', external_id='WorkOrder'), ContainerId(space='pygen', external_id='WorkItem')] along with all nodes and edges About to delete space pygen
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='Asset'), ContainerId(space='pygen', external_id='WorkOrder'), ContainerId(space='pygen', external_id='WorkItem')] Deleted space pygen