DIAdem

cancel
Showing results for 
Search instead for 
Did you mean: 

Python dataplugin method "read_channel_values" select rows to load

Hi,

im trying to use the python dataplugin and i would like to know if exists some more information than whats in help on page "Creating a Python DataPlugin"? 

 

I got the method "read_channel_values" and inside i got a condition holding info if to use the row from the variable self.data

But the method have parameters from outside with 0 and number of all rows from csv, but i need to set it to different number as im using just few rows. but it gives me error:

Cannot import all channels or channel data from this data store.

 

Could you please tell me how to use it right?

 

pythonprob.png

0 Kudos
Message 1 of 4
(2,596 Views)

The Python plugin interface has two access modes.

I assume you want to switch to the first one.

Please make sure you use at least DIAdem 2020 SP1 to develop python plugins.

 

Load on Open

If your file has just a few MegaByte or use this method

 

import os
from pathlib import Path

class Plugin:

    def read_store(self, parameter):
        file_path = os.path.realpath(parameter["file"])

        # fill this dictionary following the given example using your python script
        tdm_tree = {
            "author": "Me",
            "description": "My content",
            "groups": [
                {
                    "name": "Group_1",
                    "description": "The first group",
                    "channels": [
                        {
                            "name": "Vals_1",
                            "description": "",
                            "unit_string": "km/h",

                            "type": "DataTypeChnFloat64",
                            "values": [1.1, 2.1, 3.1]
                        }, {
                            "name": "Str_1",
                            "description": "",

                            "type": "DataTypeChnString",
                            "values": ["abc", "def", "hij"]
                        }
                    ]
                }, {
                    "name": "Group_2",
                    "description": "The second group",
                    "channels": [
                        {
                            "name": "Index",
                            "description": "",
                            "info": "Going up",
                            "unit_string": "s",

                            "type": "DataTypeChnFloat64",
                            "values": [1, 2, 3, 4]
                        }
                    ]
                }
            ]
        }

        return {Path(file_path).stem: tdm_tree}

if __name__ == "__main__":
    print("For testing your plugin first, you can run that python file directly from command line")
    p = Plugin()
    parameter = {
        "file": "C:\\data\\file_does_not_exist.py_example"
    }
    print("\n %s" % p.read_store(parameter))

Just make sure the returned dictionary contains all your data. 

The result in DIAdem

AndreasK_0-1603813558784.png

 

 

Load if channel if accessed

If you have huge files and your implementation allows you to access the channel values later on.

This mode is senseless if you store the values in members of your Plugin class.

 

import os
from pathlib import Path

class Plugin:

    def read_store(self, parameter):
        file_path = os.path.realpath(parameter["file"])

        # fill this dictionary following the given example using your python script
        tdm_tree = {
            "author": "Me",
            "description": "My content",
            "groups": [
                {
                    "name": "Group_1",
                    "description": "The first group",
                    "channels": [
                        {
                            "name": "Many_Vals_1",
                            "description": "",
                            "unit_string": "km/h",

                            "type": "DataTypeChnFloat64",
                        }, {
                            "name": "Many_Vals_2",
                            "description": "",

                            "type": "DataTypeChnInt32"
                        }
                    ]
                }
            ]
        }

        return {Path(file_path).stem: tdm_tree}

    def read_channel_length(self, grp_index, chn_index):
        # use code to detrmine channel length when asked
        if 0 == grp_index and 0 == chn_index:
            return 5000000
        if 0 == grp_index and 1 == chn_index:
            return 2500000

    def read_channel_values(self, grp_index, chn_index, numberToSkip, numberToTake):
        
        channelLength = self.read_channel_length(grp_index, chn_index)
        if numberToSkip > channelLength:
            raise Exception('skip is invalid')

        numAvailable = min(channelLength - numberToSkip, numberToTake)
        if 0 == numAvailable:
            return

        # use code to access values on access
        values = [] 

        if 0 == grp_index and 0 == chn_index:
            for i in range(numberToSkip, numberToSkip + numAvailable):
                values.append(float(i) + 0.0001)   

        if 0 == grp_index and 1 == chn_index:
            for i in range(numberToSkip, numberToSkip + numAvailable):
                    values.append(int(i ))   

        return values

if __name__ == "__main__":
    print("For testing your plugin first, you can run that python file directly from command line")
    p = Plugin()
    parameter = {
        "file": "C:\\data\\file_does_not_exist.py_example"
    }
    print("\n %s" % p.read_store(parameter))
    print("\nChannel values: %s" % p.read_channel_values(0, 0, 0, 3))
    print("\nChannel values: %s" % p.read_channel_values(0, 1, 0, 3))

In this case you could use DIAdem to register the file and data will not be loaded on open but when needed.

You plugin will be called when scrolling ...

AndreasK_1-1603814341426.png

 

0 Kudos
Message 2 of 4
(2,545 Views)

Thanks for the information mysainsburys

0 Kudos
Message 3 of 4
(2,521 Views)

Direct answer to your question:

 

When

def read_channel_values(self, grp_index, chn_index, numberToSkip, numberToTake):

is called the channel length can ot be changed any more.

def read_channel_length(self, grp_index, chn_index):

is needed to return the real channel length.

 

read_channel_values is used by the application to read the channel chunkwise.

numberToTake is no return value.  

 

0 Kudos
Message 4 of 4
(2,518 Views)