Skip to main content

Pygen: When does it return value and when does it return NodeId? Why does the cell seem empty in web view?

  • July 19, 2024
  • 6 replies
  • 54 views

APSHANKAR Sagar
Committed

We have a DM with many direct relations. A processing is directly liked to an asset with the name of the property being ‘equipment’.  

list shows the field as either just text or a dictionary

We use the Cognite data modeling API to generate models and use pygen to run programs on it. I have noticed something strange while using pygen. 

Properties which are direct relations to other types sometimes are shown as text and other times are shown as NodeId objects.

On close inspection, they appear as NodeId objects

In the former case, while observing them in the Data Model view, the properties appear empty. Actually, they contain the external_id of the instance as a string which can be seen while querying with pygen.
 

DM view showing seemingly empty cells

 

Sometimes, instances spontaneously transition from one type to the ohter (NodeId → string or vice versa). In my pygen programs thus, I have to use the following function to keep the program running.

 

from cognite.client.data_classes.data_modeling import NodeId
#Sometimes a text value is returned. Some other times, a NodeId is returned.
get_external_id = lambda x: x.external_id if isinstance(x, NodeId) else x

I don’t really understand what is going on. Does anyone have a solution?

 

6 replies

Anders  Albert
Seasoned Practitioner
Forum|alt.badge.img
  • Seasoned Practitioner
  • July 19, 2024

When you generate an SDK with pygen, you set a `default_instance_space`. When you retrieve a NodeId if the space matches the default instance space, it will replace the NodeId with simply the string of the external id.

The motivation for this behavior is that if you work in one space, then, you do not have to think about spaces at all with pygen.

Does this make sense to you? 


APSHANKAR Sagar
Committed

Hello @Anders Albert , thanks for your quick reply. 

In theory, it makes sense to me. However, in the example you see above, the instances with properties as NodeIds have identical spaces to the instances with properties returned as strings. 

We deploy the data model using the Cognite SDK (not pygen) and use pygen for querying it. When we deploy the model, we specify the space for each instance deployed. We are currently developing the model so we are working with a single space for the instances shown above. The ‘Asset’ instance is also in space Site_Instances_1000639x and the ‘Processing’ instance is also in space Site_Instances_1000639x.

  1. What I observe, is that this behavior that you mention, i.e the on-the-fly conversion of the property value from NodeId into string, given the condition that the space of the instances on both sides of the direct relation is the same, is not consistent. 
  2. It sometimes happens spontaneously, changing between NodeId and string with no changes to the DM.
  3. If it is an expected behavior, why does the web-view show it as being empty?
  4. I’d like to add that regardless of whether the property is displayed as a NodeId or a string, retrieving the instance using pygen necessitates specifying the space. Without the space, nothing is returned. 

 


Anders  Albert
Seasoned Practitioner
Forum|alt.badge.img
  • Seasoned Practitioner
  • July 19, 2024
  1. Hmm, that sound like a bug. However, based on your feedback, I think we need to consider just always returning NodeId, irrespective of whether the space matches the instance space. What do yo think about that?
  2. That sounds like a bug.
  3. The web has had some issues will report to that team.
  4. This means that the default space is not `Site_instances_1000639x’.  I would guess the model space have been set as the instance space upon creation of SDK.

APSHANKAR Sagar
Committed
  1. As long as it is consistent, it makes sense, especially since the pygen retrieve command requires space. It would be better if one could directly pass this NodeId object to the retrieve command like so:
    `pygen_client.type_name.retrieve(NodeIdObject)`. It would be very helpful if this were possible across multiple levels of direct relations/edges but I am not sure it can be done for edges. I beleive the GraphQL interface allows traversing direct relations.
  2. I’ll let you know if I find out any trigger
  3. Please do, I have contacted support for multiple problems linked to the web view already:
    1. Sometimes all direct relations don’t show up in the web-view, even as they are visible in pygen. 
    2. Cells show as blank depending on whther they are returned as NodeIds in pygen or not
    3. Populating lists doesn’t work on the interface
    4. Options for filtering are quite limited
  4. In our set-up, we hold data from different OEMs and Clients in our Data Model. We also hold our own proprietery data which we want to isolate from the first two.
    1. We have the impression that this complex setup requires using the SDK for model deployment. Do you forsee any problems declaring such a complex model with only pygen?
    2. I will still check if we declare a ‘default_instance_space’ anywhere. It will likely not be ‘Site_instances_1000639x’ as this corresponds to the data of a particular client. 

Thank you for your replies @Anders Albert 


Anders  Albert
Seasoned Practitioner
Forum|alt.badge.img
  • Seasoned Practitioner
  • July 24, 2024

I will follow up on points 1-3.

On 4.1. No the only difference with using pygen for a complex setup is that you then have to specify space in most contexts as it is not enough to assume that all nodes are in the same space.


Anders  Albert
Seasoned Practitioner
Forum|alt.badge.img
  • Seasoned Practitioner
  • August 17, 2024

@APSHANKAR Sagar Version `0.99.32` of pygen is now out that allows you to skip the default instance space. I hope this solves your problem. 

More information about the change here: