From f6e5dda2a71ca92d15c83d2be55f2cc3228ab147 Mon Sep 17 00:00:00 2001 From: Arthur Piat Date: Thu, 9 Feb 2023 16:42:42 +0000 Subject: [PATCH 1/2] Hotfix to ggobi export --- src/gemseo/utils/ggobi_export.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/gemseo/utils/ggobi_export.py b/src/gemseo/utils/ggobi_export.py index cc1dd11a67..d873530491 100644 --- a/src/gemseo/utils/ggobi_export.py +++ b/src/gemseo/utils/ggobi_export.py @@ -86,7 +86,7 @@ def save_data_arrays_to_xml( rec = SubElement( records, "record", attrib={"color": str(1), "label": "Iter_" + str(i_rec)} ) - rec.text = str(values_array[i_rec, :]).replace("[", "").replace("]", "") + rec.text = str(values_array[i_rec, :]).replace("[", "").replace("]", "").replace("\n", "") with open(file_path, "w") as xml_file: xml_file.write(prettify(root)) -- GitLab From 528c8ccab7ba4f603fb204403ccb627d775dce0b Mon Sep 17 00:00:00 2001 From: Arthur Piat Date: Tue, 14 Feb 2023 09:50:30 +0000 Subject: [PATCH 2/2] added pytest for ggobi export --- tests/utils/test_ggobi_export.py | 86 ++++++++++++++++++++++++++++++++ 1 file changed, 86 insertions(+) create mode 100644 tests/utils/test_ggobi_export.py diff --git a/tests/utils/test_ggobi_export.py b/tests/utils/test_ggobi_export.py new file mode 100644 index 0000000000..7991a9c8fe --- /dev/null +++ b/tests/utils/test_ggobi_export.py @@ -0,0 +1,86 @@ +# Copyright 2023 IRT Saint Exupéry, https://www.irt-saintexupery.com +# +# This program is free software; you can redistribute it and/or +# modify it under the terms of the GNU Lesser General Public +# License version 3 as published by the Free Software Foundation. +# +# This program is distributed in the hope that it will be useful, +# but WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU +# Lesser General Public License for more details. +# +# You should have received a copy of the GNU Lesser General Public License +# along with this program; if not, write to the Free Software Foundation, +# Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. +# Contributors: +# INITIAL AUTHORS - API and implementation and/or documentation +# :author : Arthur Piat +# OTHER AUTHORS - MACROSCOPIC CHANGES + +from pathlib import Path +import numpy as np +import xml.etree.ElementTree as ET +from gemseo.utils.ggobi_export import save_data_arrays_to_xml + +DIR_PATH = Path(__file__).parent + +NAME_FILE = 'dummy.xml' +VAR_NAME = np.array(['x_1', 'x_2', 'x_3', 'y_1', 'y_2']) +VAR_VALUE = np.array([[1,2,3,4,5], [1,2,4,6,7], [1,0,2,1,3]]) + + +def get_all_element(root, tagname): + """ + function to get specific node in xml file, recursively + + :param root: root node of xml file to be analysed + :param tagname: name of tag to be extracted recursively + """ + outlist=[] + for child in root: + if child.tag == tagname: + outlist = outlist +[child] + + list_children = get_all_element(child,tagname) + if list_children: + outlist = outlist+list_children + return outlist + +def test_generate_xml(): + save_data_arrays_to_xml(VAR_NAME,VAR_VALUE, NAME_FILE) + exp_ggobi = Path(NAME_FILE) + assert exp_ggobi.exists() + exp_ggobi.unlink() + +def test_saved_names(): + save_data_arrays_to_xml(VAR_NAME,VAR_VALUE, NAME_FILE) + + tree = ET.parse(NAME_FILE) + root = tree.getroot() + list_name = get_all_element(root, "realvariable") + variable_list = [] + for node in list_name: + variable_list.append(node.get('name')) + + assert VAR_NAME.tolist() == variable_list + + exp_ggobi = Path(NAME_FILE) + exp_ggobi.unlink() + + +def test_saved_values(): + save_data_arrays_to_xml(VAR_NAME,VAR_VALUE, NAME_FILE) + + tree = ET.parse(NAME_FILE) + root = tree.getroot() + list_values = get_all_element(root, "record") + value_list = [] + for node in list_values: + value_list.append(node.text.split(" ")) + + array_saved = np.array(value_list).astype(float) + assert VAR_VALUE.tolist() == array_saved.tolist() + + exp_ggobi = Path(NAME_FILE) + exp_ggobi.unlink() + -- GitLab