From 603182ea687d6910a9aa55bc57f4a638aeddc308 Mon Sep 17 00:00:00 2001 From: klounis Date: Tue, 19 Dec 2023 11:50:09 +0100 Subject: [PATCH] ENH : added a method to the Function Class named "data_preprocessing" to clear the CSV file and save a CSV without header --- rocketpy/mathutils/function.py | 110 +++++++++++++++++++++++++++++++++ 1 file changed, 110 insertions(+) diff --git a/rocketpy/mathutils/function.py b/rocketpy/mathutils/function.py index 251d58199..679f2c8e8 100644 --- a/rocketpy/mathutils/function.py +++ b/rocketpy/mathutils/function.py @@ -10,6 +10,7 @@ import matplotlib.pyplot as plt import numpy as np +import csv from scipy import integrate, linalg, optimize try: @@ -1117,6 +1118,49 @@ def low_pass_filter(self, alpha): interpolation=self.__interpolation__, extrapolation=self.__extrapolation__, ) + + def data_preprocessing(self, source): + """Clear data (in particular NaN objects) and returns a CSV file without header and its name. + + Parameters + ---------- + source : string + The file path to the CSV file. + + Returns + ------- + Function + The function with the incoming cleared CSV + """ + output_path = 'cleared_data.csv' + + with open(source, 'r') as file: + reader = csv.reader(file) + header = next(reader) # Read the header + + data = [row for row in reader] + + # Create a new list without the headers + data_no_headers = [] + + for row in data[1:]: + # Check if the row is not empty and if all values in the row can be converted to float + if row and all(self.is_float(value) for value in row): + data_no_headers.append(row) + + # Save the processed data to a new CSV file + with open(output_path, 'w', newline='') as output_file: + writer = csv.writer(output_file, delimiter=',') + writer.writerows(data_no_headers) + + return Function( + source=output_path, + interpolation=self.__interpolation__, + extrapolation=self.__extrapolation__, + ) + + + # Define all presentation methods def __call__(self, *args): @@ -2987,6 +3031,72 @@ def _check_user_input( + f"and output dimension ({in_out_dim})." ) return inputs, outputs, interpolation, extrapolation + + def is_float(self, element): + """ + Returns a boolean indicating us if an element is convertible to a float or not. + True : the element is convertible to a float + False : the element is not convertible to a float + + Parameters + ---------- + element : any + This is the element to test. + + Returns + ------- + result : boolean + The element is convertible or not. + """ + if element is None: + return False + try: + float(element) + return True + except ValueError: + return False + + def return_first_data(self, source): + """ + Returns the first data of a CSV file. + + Parameters + ---------- + source : string + This is the file path to the csv. + + Returns + ------- + result : any + The first data of the CSV file. + """ + native_data = open(source) + for row in native_data : + for value in row : + return value + + + def if_header(self, source): + """ + Returns if a CSV file has a header or not. + True : The CSV file has a header + False : The CSV file has no header + + Parameters + ---------- + source : string + This is the file path to the csv. + + Returns + ------- + result : boolean + The result of the CSV file containing a header or not. + """ + return not self.is_float(self.return_first_data(source)) + + + + class PiecewiseFunction(Function):