@@ -59,7 +59,8 @@ def __init__(
59
59
and 'z' is the output.
60
60
61
61
- string: Path to a CSV file. The file is read and converted into an
62
- ndarray. The file can optionally contain a single header line.
62
+ ndarray. The file can optionally contain a single header line, see
63
+ notes below for more information.
63
64
64
65
- Function: Copies the source of the provided Function object,
65
66
creating a new Function with adjusted inputs and outputs.
@@ -94,12 +95,19 @@ def __init__(
94
95
95
96
Notes
96
97
-----
97
- (I) CSV files can optionally contain a single header line. If present,
98
- the header is ignored during processing.
99
- (II) Fields in CSV files may be enclosed in double quotes. If fields are
100
- not quoted, double quotes should not appear inside them.
98
+ (I) CSV files may include an optional single header line. If this
99
+ header line is present and contains names for each data column, those
100
+ names will be used to label the inputs and outputs unless specified
101
+ otherwise by the `inputs` and `outputs` arguments.
102
+ If the header is specified for only a few columns, it is ignored.
103
+
104
+ Commas in a header will be interpreted as a delimiter, which may cause
105
+ undesired input or output labeling. To avoid this, specify each input
106
+ and output name using the `inputs` and `outputs` arguments.
107
+
108
+ (II) Fields in CSV files may be enclosed in double quotes. If fields
109
+ are not quoted, double quotes should not appear inside them.
101
110
"""
102
- # Set input and output
103
111
if inputs is None :
104
112
inputs = ["Scalar" ]
105
113
if outputs is None :
@@ -184,10 +192,18 @@ def set_source(self, source):
184
192
185
193
Notes
186
194
-----
187
- (I) CSV files can optionally contain a single header line. If present,
188
- the header is ignored during processing.
189
- (II) Fields in CSV files may be enclosed in double quotes. If fields are
190
- not quoted, double quotes should not appear inside them.
195
+ (I) CSV files may include an optional single header line. If this
196
+ header line is present and contains names for each data column, those
197
+ names will be used to label the inputs and outputs unless specified
198
+ otherwise. If the header is specified for only a few columns, it is
199
+ ignored.
200
+
201
+ Commas in a header will be interpreted as a delimiter, which may cause
202
+ undesired input or output labeling. To avoid this, specify each input
203
+ and output name using the `inputs` and `outputs` arguments.
204
+
205
+ (II) Fields in CSV files may be enclosed in double quotes. If fields
206
+ are not quoted, double quotes should not appear inside them.
191
207
192
208
Returns
193
209
-------
@@ -3019,7 +3035,7 @@ def _check_user_input(
3019
3035
if isinstance (inputs , str ):
3020
3036
inputs = [inputs ]
3021
3037
3022
- elif len (outputs ) > 1 :
3038
+ if len (outputs ) > 1 :
3023
3039
raise ValueError (
3024
3040
"Output must either be a string or have dimension 1, "
3025
3041
+ f"it currently has dimension ({ len (outputs )} )."
@@ -3036,8 +3052,19 @@ def _check_user_input(
3036
3052
try :
3037
3053
source = np .loadtxt (source , delimiter = "," , dtype = float )
3038
3054
except ValueError :
3039
- # Skip header
3040
- source = np .loadtxt (source , delimiter = "," , dtype = float , skiprows = 1 )
3055
+ with open (source , "r" ) as file :
3056
+ header , * data = file .read ().splitlines ()
3057
+
3058
+ header = [
3059
+ label .strip ("'" ).strip ('"' ) for label in header .split ("," )
3060
+ ]
3061
+ source = np .loadtxt (data , delimiter = "," , dtype = float )
3062
+
3063
+ if len (source [0 ]) == len (header ):
3064
+ if inputs == ["Scalar" ]:
3065
+ inputs = header [:- 1 ]
3066
+ if outputs == ["Scalar" ]:
3067
+ outputs = [header [- 1 ]]
3041
3068
except Exception as e :
3042
3069
raise ValueError (
3043
3070
"The source file is not a valid csv or txt file."
@@ -3055,7 +3082,7 @@ def _check_user_input(
3055
3082
3056
3083
## single dimension
3057
3084
if source_dim == 2 :
3058
- # possible interpolation values: llinear , polynomial, akima and spline
3085
+ # possible interpolation values: linear , polynomial, akima and spline
3059
3086
if interpolation is None :
3060
3087
interpolation = "spline"
3061
3088
elif interpolation .lower () not in [
@@ -3106,7 +3133,7 @@ def _check_user_input(
3106
3133
in_out_dim = len (inputs ) + len (outputs )
3107
3134
if source_dim != in_out_dim :
3108
3135
raise ValueError (
3109
- "Source dimension ({source_dim}) does not match input "
3136
+ f "Source dimension ({ source_dim } ) does not match input "
3110
3137
+ f"and output dimension ({ in_out_dim } )."
3111
3138
)
3112
3139
return inputs , outputs , interpolation , extrapolation
0 commit comments