From 35d71bf7ddaf42320fd459b74853041a0f11fcf9 Mon Sep 17 00:00:00 2001 From: helene-t Date: Mon, 10 Jan 2022 14:21:01 +0100 Subject: [PATCH] [NF] clean strings from latex characters in export --- SciDataTool/Methods/DataND/export_along.py | 24 ++++++++++++++++--- Tests/Validation/test_export.py | 28 +++++++++++++++++++++- 2 files changed, 48 insertions(+), 4 deletions(-) diff --git a/SciDataTool/Methods/DataND/export_along.py b/SciDataTool/Methods/DataND/export_along.py index bbcb89b7..1558cd2a 100644 --- a/SciDataTool/Methods/DataND/export_along.py +++ b/SciDataTool/Methods/DataND/export_along.py @@ -4,6 +4,8 @@ import numpy as np from os.path import join +CHAR_LIST = ["$", "{", "}"] + def export_along( self, @@ -147,8 +149,13 @@ def export_along( + axes_list_new[1].unit + "]" ) - second_line = np.insert( - results[axes_list_new[1].name].astype("object"), 0, A2_cell + second_line = format_matrix( + np.insert( + results[axes_list_new[1].name].astype("str"), + 0, + A2_cell, + ), + CHAR_LIST, ) csvWriter.writerow(second_line) @@ -161,8 +168,19 @@ def export_along( field = np.take(results[self.symbol], i, axis=2) else: field = results[self.symbol] - matrix = np.column_stack((results[axes_list_new[0].name].T, field)) + matrix = format_matrix( + np.column_stack((results[axes_list_new[0].name].T, field)).astype( + "str" + ), + CHAR_LIST, + ) csvWriter.writerows(matrix) else: raise Exception("export format not supported") + + +def format_matrix(a, char_list): + for char in char_list: + a = np.char.replace(a, char, "") + return a diff --git a/Tests/Validation/test_export.py b/Tests/Validation/test_export.py index 758e0d7b..293043f7 100644 --- a/Tests/Validation/test_export.py +++ b/Tests/Validation/test_export.py @@ -1,5 +1,5 @@ import pytest -from SciDataTool import DataTime, DataLinspace +from SciDataTool import DataTime, DataLinspace, Data1D from Tests import save_validation_path import numpy as np from os.path import isfile, join @@ -68,6 +68,32 @@ def test_export_3D(): assert isfile(join(save_validation_path, "B_r_withoutargs_z-1.0.csv")) +def test_export_latex(): + """Test export""" + X = DataLinspace(name="time", unit="s", initial=0, final=10, number=11) + Y = Data1D( + name="order", + unit="", + values=["H24 ($3f_{e}$)", "H48 ($6f_{e}$)"], + is_components=True, + ) + field = np.zeros((11, 2)) + Field = DataTime( + name="Airgap flux density", + symbol="B_r", + unit="T", + axes=[X, Y], + values=field, + ) + Field.export_along( + "time", + "order", + save_path=save_validation_path, + file_name="B_r_Data3D_latex", + ) + + if __name__ == "__main__": test_export_2D() test_export_3D() + test_export_latex()