Accessing Metadata using Python

Introduction:

Nowadays the data file generated by scientific instruments usually contains the information such as the instrument model, operator name, measurement date, measurement parameters, etc. This information is often saved in the data file as header lines (meta data) and  it can be useful later on either as the dataset identifier or as parameters for data analysis. In this blog, I am going to show an example of using Python code to handle these header lines (meta data) during data import — save the header line (meta data) into the user-tree in worksheet and use it for analysis.(See figure below) Make sure you have installed Origin 2021b before proceeding. 

Details:

Please first download the zip file, decompress it to the location of your choosing. It contains three data files. The subfolder contains an import filter file (.oif) and a Python file (.py) which you will create by yourself following this blog.  

Note: If you open one of the data files in any text editor, you should see that the main header lines (see the figure above) contain the measurement information such as operator name, experiment data, etc. We will write Python code to parse these header lines and store it into the worksheet user-tree node.

Step1:

First I am going to create a Python-based import filter. Launch Origin and then open the Import Wizard (menu Data:Import From File:Import Wizard…) and follow these steps.

  1. Select F1.dat in the downloaded folder to import.
  2. Set Data Type to: User-Defined.
  3. Set Target Window to: Worksheet.
  4. Set Import Mode as Start New Sheets.
  5. Click Next button.

Do these things on the User Defined Filters page (second page) of the Import Wizard:

  1. Specify Python Code as the type of code used.
  2. Copy and paste the Python code below into the box to read the meta data from the first 4 lines and save to worksheet tree. 
  3. Click Next button.

Do these things on the Save Filters page (third page) of the Import Wizard:

  1. Check Save Filter.
  2. Save the Filter in the data file folder.
  3. Give the filter a file name, in this case “MyFilter”.
  4. Click Finish button. 

Date gets imported and the worksheet is renamed with file name. To check the metadata, you can left-click on the edge of the grey area on the worksheet and choose Show Organizer. Navigate to F1.dat node on the left panel. 

Step2:

Now go to the data folder and make sure the filter file is created with name MyFilter.oif.  Next I am going to import all three data files  under the folder with Multiple Files Connector tool.

  1. Create a new workbook in Origin, then select from menu Data: Connect Multiple Files…
  2. Set Data Connector to Import Filter. Origin will automatically locate the filter in the data folder. Set Source to Files in Specified Folder. Set as the files folder. Check the Same Book box. Set Column Designations to XYE. Click OK.

Three worksheets with imported data are created, each has a data connector icon on it connecting to individual data file. Turn on the Worksheet Organizer of each sheet, make sure the metadata of each data file is properly imported. 

Step3:

In this step, I am going to run a piece of Python code to perform logistic regression on all three datasets and create a result sheet. Each record contains the data file name, metadata and the fitted parameters. 

  1. Select menu Connectivity:Open Untitled.py…
  2. Copy and paste the code below into the code editor window and save as a new file with name for example CurveFit.py.
  3. Hit F5 to execute.

The data workbook and the result workbook are shown below:

 

Leave a Reply

Your email address will not be published. Required fields are marked *