
.. DO NOT EDIT.
.. THIS FILE WAS AUTOMATICALLY GENERATED BY SPHINX-GALLERY.
.. TO MAKE CHANGES, EDIT THE SOURCE PYTHON FILE:
.. "examples/data_collection/optitrack_export.py"
.. LINE NUMBERS ARE GIVEN BELOW.

.. only:: html

    .. note::
        :class: sphx-glr-download-link-note

        :ref:`Go to the end <sphx_glr_download_examples_data_collection_optitrack_export.py>`
        to download the full example code

.. rst-class:: sphx-glr-example-title

.. _sphx_glr_examples_data_collection_optitrack_export.py:


Optitrack Export
================

This example shows how to clean the .csv files outputted by Optitrack.

.. GENERATED FROM PYTHON SOURCE LINES 7-39

.. code-block:: default


    import rofunc as rf
    import os
    import numpy as np

    # If input_path points to a folder, each element of objs and meta is the data corresponding to one file.
    # In a folder, only the file with the following name format are considered: 'Take*.csv'
    # If input_file points to a file, objs and meta are lists with only one element.
    input_path = '../data/RAW_DEMO/optitrack/Take 2023-03-29 06.23.40 PM.csv'
    parent_dir = os.path.dirname(input_path)
    objs_list, meta_list = rf.optitrack.get_objects(input_path)

    # You can delete object from the dictionary, and they will not be considered in the data
    del_objects = ['left']
    for obj in del_objects:
        del objs_list[0][obj]

    # data is a numpy array of shape (n_samples, n_features)
    # labels is a list of strings corresponding to the name of the features
    data, labels = rf.optitrack.data_clean(parent_dir, legacy=False, objs=objs_list[0])[0]

    # Accessing the position and attitude of an object over all samples:
    # Coordinates names and order: ['x', 'y', 'z', 'qx', 'qy', 'qz', 'qw']
    data_ptr = labels.index('box.pose.x')
    assert data_ptr + 6 == labels.index('box.pose.qw')
    box_pos_x = data[:, data_ptr:data_ptr + 7]

    # TODO: export data as the format of objects
    root_dir = '/home/ubuntu/Data/optitrack_export/'
    rf.oslab.create_dir(root_dir)
    exp_name = 'rigid_body.npy'
    np.save(os.path.join(root_dir, exp_name), data)


.. rst-class:: sphx-glr-timing

   **Total running time of the script:** (0 minutes 0.000 seconds)


.. _sphx_glr_download_examples_data_collection_optitrack_export.py:

.. only:: html

  .. container:: sphx-glr-footer sphx-glr-footer-example




    .. container:: sphx-glr-download sphx-glr-download-python

      :download:`Download Python source code: optitrack_export.py <optitrack_export.py>`

    .. container:: sphx-glr-download sphx-glr-download-jupyter

      :download:`Download Jupyter notebook: optitrack_export.ipynb <optitrack_export.ipynb>`


.. only:: html

 .. rst-class:: sphx-glr-signature

    `Gallery generated by Sphinx-Gallery <https://sphinx-gallery.github.io>`_
