Skip to content

Commit 048aa62

Browse files
committed
supports adding datasets of different extension types
1 parent a3fc482 commit 048aa62

File tree

11 files changed

+162
-51
lines changed

11 files changed

+162
-51
lines changed

README.md

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -196,6 +196,20 @@ Below is a list of commands. The commands have obligatory and optional parameter
196196
*/
197197
```
198198

199+
### Command (Data)
200+
```bash
201+
>> list_cols
202+
```
203+
204+
```javascript
205+
/**
206+
* Lists the columns of the dataset of the current project.
207+
*
208+
* @description
209+
* Use this function to list the columns of the dataset. The column names will be displayed in the console.
210+
*/
211+
```
212+
199213
### Command (Data)
200214
```bash
201215
>> clean_data

requirements.txt

32 Bytes
Binary file not shown.

src/cliresult.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ def _parse_models(*args: Any) -> tuple[InplaceModel, InplaceModel]:
3333
model = args[0]
3434
try:
3535
project = model.projects[model.current_project]
36-
except (AttributeError, IndexError):
36+
except (AttributeError, IndexError, KeyError):
3737
project = InplaceModel()
3838
except IndexError:
3939
model = InplaceModel()

src/commands/command_factory.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
add_data, read_data, make_X_y,
55
clean_data, summary,
66
save, load_project_from_file,
7-
plot, show, stats
7+
plot, show, stats, list_cols
88
)
99
from src.commands.ml_cmds import (linreg, mlpreg, naivebayes, mlpclas,
1010
logisticreg, decisiontree, randomforest,
@@ -42,6 +42,7 @@ def list_cmds(*args, **kwargs) -> str:
4242
"pcp" : pcp,
4343
"help" : list_cmds,
4444
"add_data": add_data,
45+
"list_cols": list_cols,
4546
"read_data": read_data,
4647
"make_x_y": make_X_y,
4748
"clean_data": clean_data,

src/commands/command_utils.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ def convert_to_type(type_str: str) -> ProjectType:
3737
type_str = short_type[type_str]
3838
try:
3939
return ProjectType[type_str.upper()]
40-
except ValueError:
40+
except (ValueError, KeyError):
4141
raise ValueError(f"Invalid project type: {type_str}")
4242

4343
def convert_to_ml_type(type_str: str) -> MlModel:

src/commands/proj_cmds.py

Lines changed: 76 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,10 @@
11
from src.commands.project_store_protocol import Model
22
from src.commands.command_utils import convert_to_type, convert_to_ml_type
3+
from src.cliresult import chain, add_warning
34

45
import numpy as np
56

7+
@chain
68
def create(model: Model, alias: str, type: str, *args, **kwargs) -> str:
79
"""Creates a new project with the given alias and type.
810
@@ -15,13 +17,14 @@ def create(model: Model, alias: str, type: str, *args, **kwargs) -> str:
1517
str: Optional message to display to the user.
1618
"""
1719
if args:
18-
print(f"Warning: extra arguments {args} will be ignored.")
20+
add_warning(model, f"Warning: extra arguments {args} will be ignored.")
1921
elif kwargs:
20-
print(f"Warning: extra arguments {kwargs} will be ignored.")
22+
add_warning(model, f"Warning: extra arguments {kwargs} will be ignored.")
2123

2224
new_type = convert_to_type(type)
2325
return model.create(alias, new_type)
2426

27+
@chain
2528
def delete(model: Model, alias: str, from_dir: bool = False, *args, **kwargs) -> str:
2629
"""
2730
Deletes the project with the given alias.
@@ -34,12 +37,13 @@ def delete(model: Model, alias: str, from_dir: bool = False, *args, **kwargs) ->
3437
str: Optional message to display to the user.
3538
"""
3639
if args:
37-
print(f"Warning: extra arguments {args} will be ignored.")
40+
add_warning(model, f"Warning: extra arguments {args} will be ignored.")
3841
elif kwargs:
39-
print(f"Warning: extra arguments {kwargs} will be ignored.")
42+
add_warning(model, f"Warning: extra arguments {kwargs} will be ignored.")
4043

4144
return model.delete(alias, from_dir = from_dir)
4245

46+
@chain
4347
def list_projects(model: Model, *args, **kwargs) -> str:
4448
"""
4549
Lists all projects in the model.
@@ -51,12 +55,13 @@ def list_projects(model: Model, *args, **kwargs) -> str:
5155
list[str]: A list of project aliases.
5256
"""
5357
if args:
54-
print(f"Warning: extra arguments {args} will be ignored.")
58+
add_warning(model, f"Warning: extra arguments {args} will be ignored.")
5559
elif kwargs:
56-
print(f"Warning: extra arguments {kwargs} will be ignored.")
60+
add_warning(model, f"Warning: extra arguments {kwargs} will be ignored.")
5761

5862
return model.list_projects()
5963

64+
@chain
6065
def set_current_project(model: Model, alias: str, *args, **kwargs) -> str:
6166
"""
6267
Sets the current project to the one with the given alias.
@@ -69,9 +74,9 @@ def set_current_project(model: Model, alias: str, *args, **kwargs) -> str:
6974
str: Optional message to display to the user.
7075
"""
7176
if args:
72-
print(f"Warning: extra arguments {args} will be ignored.")
77+
add_warning(model, f"Warning: extra arguments {args} will be ignored.")
7378
elif kwargs:
74-
print(f"Warning: extra arguments {kwargs} will be ignored.")
79+
add_warning(model, f"Warning: extra arguments {kwargs} will be ignored.")
7580

7681
return model.set_current_project(alias)
7782

@@ -87,24 +92,32 @@ def pcp(model: Model) -> str:
8792
"""
8893
return model.pcp()
8994

90-
def add_data(model: Model, df_name: str, *args, **kwargs) -> str:
95+
@chain
96+
def add_data(model: Model, df_name: str, delimiter: str = ',', *args, **kwargs) -> str:
9197
"""
9298
Adds a DataFrame to the current project.
99+
100+
Supported file extensions:
101+
- .csv
102+
- .txt
103+
- .xls, .xlsx
104+
- .json
105+
- .xml
106+
- .html
93107
94108
Args:
95109
model (Model): Parsed automatically by the command parser.
96-
df_name (str): Name of the DataFrame to add.
97-
98-
Returns:
99-
None
110+
alias (str): Name of the DataFrame to add.
111+
delimiter (str): Delimiter used in the DataFrame (ignored if format is not '.txt' or '.csv').
100112
"""
101113
if args:
102-
print(f"Warning: extra arguments {args} will be ignored.")
114+
add_warning(model, f"Warning: extra arguments {args} will be ignored.")
103115
elif kwargs:
104-
print(f"Warning: extra arguments {kwargs} will be ignored.")
116+
add_warning(model, f"Warning: extra arguments {kwargs} will be ignored.")
105117

106-
return model.add_data(df_name)
118+
return model.add_data(df_name, delimiter = delimiter)
107119

120+
@chain
108121
def read_data(model: Model, head: int = 5, *args, **kwargs) -> str:
109122
"""
110123
Reads the data from the current project.
@@ -117,12 +130,31 @@ def read_data(model: Model, head: int = 5, *args, **kwargs) -> str:
117130
str: A string representation of the DataFrame.
118131
"""
119132
if args:
120-
print(f"Warning: extra arguments {args} will be ignored.")
133+
add_warning(model, f"Warning: extra arguments {args} will be ignored.")
121134
elif kwargs:
122-
print(f"Warning: extra arguments {kwargs} will be ignored.")
135+
add_warning(model, f"Warning: extra arguments {kwargs} will be ignored.")
123136

124137
return model.read_data(head)
125138

139+
@chain
140+
def list_cols(model: Model, *args, **kwargs) -> str:
141+
"""
142+
Lists the columns of dataframe in the current project.
143+
144+
Args:
145+
model (Model): Parsed automatically by the command parser.
146+
147+
Returns:
148+
str: A list of columns in the current project.
149+
"""
150+
if args:
151+
add_warning(model, f"Warning: extra arguments {args} will be ignored.")
152+
elif kwargs:
153+
add_warning(model, f"Warning: extra arguments {kwargs} will be ignored.")
154+
155+
return model.list_cols()
156+
157+
@chain
126158
def make_X_y(model: Model, target: str, *args, **kwargs) -> str:
127159
"""
128160
Creates the X and y arrays from the current project.
@@ -135,12 +167,13 @@ def make_X_y(model: Model, target: str, *args, **kwargs) -> str:
135167
str: Optional message to display to the user.
136168
"""
137169
if args:
138-
print(f"Warning: extra arguments {args} will be ignored.")
170+
add_warning(model, f"Warning: extra arguments {args} will be ignored.")
139171
elif kwargs:
140-
print(f"Warning: extra arguments {kwargs} will be ignored.")
172+
add_warning(model, f"Warning: extra arguments {kwargs} will be ignored.")
141173

142174
return model.make_X_y(target)
143175

176+
@chain
144177
def clean_data(model: Model, *args, **kwargs) -> str:
145178
"""
146179
Cleans the data in the current project.
@@ -152,12 +185,13 @@ def clean_data(model: Model, *args, **kwargs) -> str:
152185
str: Optional message to display to the user.
153186
"""
154187
if args:
155-
print(f"Warning: extra arguments {args} will be ignored.")
188+
add_warning(model, f"Warning: extra arguments {args} will be ignored.")
156189
elif kwargs:
157-
print(f"Warning: extra arguments {kwargs} will be ignored.")
190+
add_warning(model, f"Warning: extra arguments {kwargs} will be ignored.")
158191

159192
return model.clean_data()
160193

194+
@chain
161195
def log_model(model: Model, model_name: str, predictions: np.ndarray, params: dict[str, float | int | str], *args, **kwargs) -> str:
162196
"""
163197
Logs a model in the current project.
@@ -172,13 +206,14 @@ def log_model(model: Model, model_name: str, predictions: np.ndarray, params: di
172206
str: Optional message to display to the user.
173207
"""
174208
if args:
175-
print(f"Warning: extra arguments {args} will be ignored.")
209+
add_warning(model, f"Warning: extra arguments {args} will be ignored.")
176210
elif kwargs:
177-
print(f"Warning: extra arguments {kwargs} will be ignored.")
211+
add_warning(model, f"Warning: extra arguments {kwargs} will be ignored.")
178212

179213
model_name = convert_to_ml_type(model_name)
180214
return model.log_model(model_name, predictions, params)
181215

216+
@chain
182217
def summary(model: Model, *args, **kwargs) -> str:
183218
"""
184219
Summarizes the current project.
@@ -190,12 +225,13 @@ def summary(model: Model, *args, **kwargs) -> str:
190225
str: A summary of the current project.
191226
"""
192227
if args:
193-
print(f"Warning: extra arguments {args} will be ignored.")
228+
add_warning(model, f"Warning: extra arguments {args} will be ignored.")
194229
elif kwargs:
195-
print(f"Warning: extra arguments {kwargs} will be ignored.")
230+
add_warning(model, f"Warning: extra arguments {kwargs} will be ignored.")
196231

197232
return model.summary()
198233

234+
@chain
199235
def save(model: Model, overwrite: bool = False, *args, **kwargs) -> str:
200236
"""
201237
Saves the current project.
@@ -208,12 +244,13 @@ def save(model: Model, overwrite: bool = False, *args, **kwargs) -> str:
208244
str: Optional message to display to the user.
209245
"""
210246
if args:
211-
print(f"Warning: extra arguments {args} will be ignored.")
247+
add_warning(model, f"Warning: extra arguments {args} will be ignored.")
212248
elif kwargs:
213-
print(f"Warning: extra arguments {kwargs} will be ignored.")
249+
add_warning(model, f"Warning: extra arguments {kwargs} will be ignored.")
214250

215251
return model.save(overwrite=overwrite)
216252

253+
@chain
217254
def load_project_from_file(model: Model, alias: str, *args, **kwargs) -> str:
218255
"""
219256
Loads a project from a file.
@@ -226,12 +263,13 @@ def load_project_from_file(model: Model, alias: str, *args, **kwargs) -> str:
226263
str: Optional message to display to the user.
227264
"""
228265
if args:
229-
print(f"Warning: extra arguments {args} will be ignored.")
266+
add_warning(model, f"Warning: extra arguments {args} will be ignored.")
230267
elif kwargs:
231-
print(f"Warning: extra arguments {kwargs} will be ignored.")
268+
add_warning(model, f"Warning: extra arguments {kwargs} will be ignored.")
232269

233270
return model.load_project_from_file(alias)
234271

272+
@chain
235273
def plot(model: Model, cmd: str, labels: str | list[str] | None = None, show: bool = False, *args, **kwargs) -> str:
236274
"""
237275
Generates a plot based on the given command and labels.
@@ -248,12 +286,13 @@ def plot(model: Model, cmd: str, labels: str | list[str] | None = None, show: bo
248286
if labels is None:
249287
labels = []
250288
if args:
251-
print(f"Warning: extra arguments {args} will be ignored.")
289+
add_warning(model, f"Warning: extra arguments {args} will be ignored.")
252290
elif kwargs:
253-
print(f"Warning: extra arguments {kwargs} will be ignored.")
291+
add_warning(model, f"Warning: extra arguments {kwargs} will be ignored.")
254292

255293
return model.plot(cmd, labels, show)
256294

295+
@chain
257296
def show(model: Model, *args, **kwargs) -> str:
258297
"""
259298
Displays the plot for the current project.
@@ -265,12 +304,13 @@ def show(model: Model, *args, **kwargs) -> str:
265304
str: The result of the show command.
266305
"""
267306
if args:
268-
print(f"Warning: extra arguments {args} will be ignored.")
307+
add_warning(model, f"Warning: extra arguments {args} will be ignored.")
269308
elif kwargs:
270-
print(f"Warning: extra arguments {kwargs} will be ignored.")
309+
add_warning(model, f"Warning: extra arguments {kwargs} will be ignored.")
271310

272311
return model.show()
273312

313+
@chain
274314
def stats(model: Model, *args, **kwargs) -> str:
275315
"""
276316
Displays the statistics for the current project.
@@ -282,8 +322,8 @@ def stats(model: Model, *args, **kwargs) -> str:
282322
str: The result of the stats command.
283323
"""
284324
if args:
285-
print(f"Warning: extra arguments {args} will be ignored.")
325+
add_warning(model, f"Warning: extra arguments {args} will be ignored.")
286326
elif kwargs:
287-
print(f"Warning: extra arguments {kwargs} will be ignored.")
327+
add_warning(model, f"Warning: extra arguments {kwargs} will be ignored.")
288328

289329
return model.stats()

src/commands/project_store_protocol.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,9 +22,11 @@ def set_current_project(self, alias: str) -> str: ...
2222

2323
def pcp(self) -> str: ...
2424

25-
def add_data(self, df_name: str) -> str: ...
25+
def add_data(self, df_name: str, delimiter: str = ',') -> str: ...
2626

2727
def read_data(self, head: int = 5) -> str: ...
28+
29+
def list_cols(self) -> str: ...
2830

2931
def make_X_y(self, target: str) -> str: ...
3032

0 commit comments

Comments
 (0)