From c7930d3327f135ff5d5e737387868cea2f0394ed Mon Sep 17 00:00:00 2001
From: cmoineau <cyril.moineau@cea.fr>
Date: Tue, 2 Jul 2024 08:09:51 +0000
Subject: [PATCH] Add utils data conversion function.

---
 .../operator_export/producer.py               | 25 +++-------------
 aidge_core/export_utils/data_conversion.py    | 30 +++++++++++++++++++
 2 files changed, 34 insertions(+), 21 deletions(-)
 create mode 100644 aidge_core/export_utils/data_conversion.py

diff --git a/aidge_core/aidge_export_aidge/operator_export/producer.py b/aidge_core/aidge_export_aidge/operator_export/producer.py
index c04019043..bbeb0f1df 100644
--- a/aidge_core/aidge_export_aidge/operator_export/producer.py
+++ b/aidge_core/aidge_export_aidge/operator_export/producer.py
@@ -1,24 +1,11 @@
 from aidge_core.aidge_export_aidge.utils import operator_register
+from aidge_core.export_utils.data_conversion import aidge2c
+
 from aidge_core.aidge_export_aidge import ROOT_EXPORT
 from aidge_core import DataType, ExportNode, generate_file, generate_str
 import numpy as np
 from pathlib import Path
 
-# Convert aidge datatype to C++ type
-datatype_converter = {
-    DataType.Float64 : "double",
-    DataType.Float32 : "float",
-    DataType.Float16 : "half_float::half",
-    DataType.Int8    : "int8_t",
-    DataType.Int16   : "int16_t",
-    DataType.Int32   : "int32_t",
-    DataType.Int64   : "int64_t",
-    DataType.UInt8   : "uint8_t",
-    DataType.UInt16  : "uint16_t",
-    DataType.UInt32  : "uint32_t",
-    DataType.UInt64  : "uint64_t"
-}
-
 
 @operator_register("Producer")
 class Producer(ExportNode):
@@ -39,16 +26,12 @@ class Producer(ExportNode):
         filepath = export_folder / f"include/{include_path}"
 
         aidge_tensor = self.operator.get_output(0)
-        aidge_type = aidge_tensor.dtype()
-        if aidge_type in datatype_converter:
-            datatype = datatype_converter[aidge_type]
-        else:
-            raise RuntimeError(f"No conversion found for data type {aidge_type}.")
+        datatype = aidge2c(aidge_tensor.dtype())
         generate_file(
             filepath,
             ROOT_EXPORT / "templates/parameter.jinja",
             dims = aidge_tensor.dims(),
-            data_t = datatype, # TODO : get data from producer
+            data_t = datatype,
             name = self.tensor_name,
             values = str(aidge_tensor)
         )
diff --git a/aidge_core/export_utils/data_conversion.py b/aidge_core/export_utils/data_conversion.py
new file mode 100644
index 000000000..2b1b0664b
--- /dev/null
+++ b/aidge_core/export_utils/data_conversion.py
@@ -0,0 +1,30 @@
+import numpy as np
+import aidge_core
+
+
+datatype_converter_aide2c = {
+    aidge_core.DataType.Float64 : "double",
+    aidge_core.DataType.Float32 : "float",
+    aidge_core.DataType.Float16 : "half_float::half",
+    aidge_core.DataType.Int8    : "int8_t",
+    aidge_core.DataType.Int16   : "int16_t",
+    aidge_core.DataType.Int32   : "int32_t",
+    aidge_core.DataType.Int64   : "int64_t",
+    aidge_core.DataType.UInt8   : "uint8_t",
+    aidge_core.DataType.UInt16  : "uint16_t",
+    aidge_core.DataType.UInt32  : "uint32_t",
+    aidge_core.DataType.UInt64  : "uint64_t"
+}
+
+def aidge2c(datatype):
+    """Convert a aidge datatype to C type
+
+    :param datatype: Aidge datatype to convert
+    :type datatype: :py:object:`aidge_core.DataType`
+    :return: A string representing the C type
+    :rtype: string
+    """
+    if datatype in datatype_converter_aide2c:
+        return datatype_converter_aide2c[datatype]
+    else:
+        raise ValueError(f"Unsupported {datatype} aidge datatype")
-- 
GitLab