Skip to content

Commit

Permalink
ENH : added a method to the Function Class named "data_preprocessing"…
Browse files Browse the repository at this point in the history
… to clear the CSV file and save a CSV without header
  • Loading branch information
klounis committed Dec 19, 2023
1 parent 6347371 commit 603182e
Showing 1 changed file with 110 additions and 0 deletions.
110 changes: 110 additions & 0 deletions rocketpy/mathutils/function.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@

import matplotlib.pyplot as plt
import numpy as np
import csv
from scipy import integrate, linalg, optimize

try:
Expand Down Expand Up @@ -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):
Expand Down Expand Up @@ -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):
Expand Down

0 comments on commit 603182e

Please sign in to comment.