:py:mod:`pocketpose.utils`
==========================

.. py:module:: pocketpose.utils

.. autoapi-nested-parse::

   The utils module contains utility functions and classes. 



Subpackages
-----------
.. toctree::
   :titlesonly:
   :maxdepth: 3

   stats/index.rst


Submodules
----------
.. toctree::
   :titlesonly:
   :maxdepth: 1

   io/index.rst
   registry/index.rst


Package Contents
----------------

Classes
~~~~~~~

.. autoapisummary::

   pocketpose.utils.Registry



Functions
~~~~~~~~~

.. autoapisummary::

   pocketpose.utils.download_file
   pocketpose.utils.write_to_table
   pocketpose.utils.build_from_cfg
   pocketpose.utils.estimate_flops_tflite
   pocketpose.utils.estimate_parameters_tflite
   pocketpose.utils.get_stats_tflite
   pocketpose.utils.tabulate_stats



.. py:function:: download_file(url, save_path, google_drive=False)

   Download a file from the specified URL.

   Args:
       url (str): The URL to download the file from.
       save_path (str): The path of the file to save the downloaded file to.
       google_drive (bool): Whether the file is hosted on Google Drive. If True,
                            the url must be a Google Drive sharing link and the
                            file will be downloaded using the gdown package.
                            Defaults to False.

   Returns:
       True if the file was downloaded successfully, False otherwise.


.. py:function:: write_to_table(data, save_path, tablefmt=None)

   Save the data as a table in the specified format.

   Args:
       data (list of dict): The data to be saved as key-value pairs.
       save_path (str): The path of the file to save the data to.
       tablefmt (str): The format of the table. If None, the format is
                       determined from the extension of the save path.


.. py:class:: Registry(name)


   A registry to map names to classes. 

   This is a generic registry that can be used to register any class,
   e.g. skeletons, datasets, models, etc. It is used by different modules
   to register their classes.

   Args:
       name (str): The name of the registry.

   Attributes:
       name (str): The name of the registry.
       _registry (dict): The dictionary mapping names to classes.

   Example:
       >>> model_registry = Registry("model")
       >>> @model_registry.register("movenet")

   .. py:method:: register(name)

      Register a class using the name.

      Args:
          name (str): The name of the class.


   .. py:method:: get(name)

      Get the class corresponding to the name. 

      Args:
          name (str): The name of the class.

      Returns:
          The corresponding class.

      Raises:
          ValueError: If the name is not registered in the registry.


   .. py:method:: list()

      List all the registered names. 



.. py:function:: build_from_cfg(cfg, registry, default_args=None)

   Build a class from the config.

   Args:
       cfg (dict): The config.
       registry (Registry): The registry to look up the class.
       default_args (dict): The default arguments to the class.

   Returns:
       The built class.


.. py:function:: estimate_flops_tflite(model_path: str) -> float

   Estimates the number of floating point operations in a TFLite model.

   Note: This is a rough estimate which only considers CONV_2D, DEPTHWISE_CONV_2D,
   FULLY_CONNECTED, ADD, and MUL operators.

   For FULLY_CONNECTED layer, we assume that the number of FLOPs is approximately 2
   times the number of elements in the weight matrix, which assumes that each weight
   participates in one multiplication and one addition.

   For ADD and MUL layers, we assume that each operation is performed element-wise,
   and so the number of FLOPs is equal to the total number of elements in the
   input tensor.

   Args:
       model_path (str): Path to TFLite model

   Returns:
       float: Number of estimated GFLOPs in the model


.. py:function:: estimate_parameters_tflite(model_path: str) -> int

   Estimates the number of parameters in a TFLite model.

   Args:
       model_path (str): Path to TFLite model

   Returns:
       int: Number of parameters in the model


.. py:function:: get_stats_tflite(model_path: str) -> tuple[str, dict[str, Any]]


.. py:function:: tabulate_stats(statistics)

   Converts raw statistics into list of dictionaries.

   Args:
       statistics (dict): The raw statistics.

   Returns:
       list of dict: The statistics in a tabular format. These can be
                     directly written to a table using the `tabulate` package
                     or the `write_to_table` function in `io` module.


