Skip to content

Commit 43f47d9

Browse files
authored
Merge pull request #24 from pierGit7/feature/csv-reader
Feature/csv reader
2 parents df64fc6 + 0f84fcc commit 43f47d9

8 files changed

Lines changed: 84 additions & 2 deletions

File tree

src/__init__.py

Whitespace-only changes.

src/common/architecture.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
from dataclasses import dataclass
22

3-
from src.common.scheduler import Scheduler
3+
from common.scheduler import Scheduler
44

55
@dataclass
66
class Architecture:

src/common/budget.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
from dataclasses import dataclass
22

3-
from assignment.simulator.src.scheduler import Scheduler
3+
from common.scheduler import Scheduler
44

55
@dataclass
66
class Budget:

src/common/csvreader.py

Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
import pandas as pd
2+
import os
3+
4+
from common.architecture import Architecture
5+
from common.budget import Budget
6+
from common.scheduler import Scheduler
7+
from common.utils import get_project_root
8+
9+
def read_architectures(csv:str)-> list[Architecture]:
10+
"""
11+
Reads the architecture information from a CSV file and returns a list of Architecture objects.
12+
13+
Args:
14+
csv (str): Path to the CSV file. Can be either an absolute path or a relative path from the project root.
15+
16+
Returns:
17+
list[Architecture]: List of Architecture objects parsed from the CSV file.
18+
"""
19+
csv = _get_csv_path(csv)
20+
21+
df = pd.read_csv(csv)
22+
23+
architectures = []
24+
for _, row in df.iterrows():
25+
architecture = Architecture(
26+
core_id=row['core_id'],
27+
speed_factor=row['speed_factor'],
28+
scheduler=Scheduler[row['scheduler']]
29+
)
30+
architectures.append(architecture)
31+
32+
return architectures
33+
34+
def read_budgets(csv:str)-> list[Budget]:
35+
csv = _get_csv_path(csv)
36+
37+
df = pd.read_csv(csv)
38+
39+
for _,row in df.iterrows():
40+
budget = Budget(
41+
component_id=row['component_id'],
42+
scheduler=Scheduler[row['scheduler']],
43+
budget=row['budget'],
44+
period=row['period'],
45+
core_id=row['core_id'],
46+
priority=row['priority']
47+
)
48+
budgets.append(budget)
49+
50+
def _get_csv_path(csv:str) -> str:
51+
if os.path.exists(csv):
52+
return csv
53+
54+
csv = os.path.join(get_project_root(),csv)
55+
if os.path.exists(csv):
56+
return csv
57+
58+
raise FileNotFoundError(
59+
f"File {csv} does not exist. "
60+
"Pass to the function an absolute path or a relative path from the project root. "
61+
"For example 'data/testcases/1-tiny-test-case/architecture.csv"
62+
)

src/common/utils.py

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
import os
2+
3+
def get_project_root() -> str:
4+
"""
5+
Get the root directory of the project.
6+
"""
7+
return os.path.dirname(os.path.dirname(os.path.abspath(__file__)))

src/simulator.py

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
from common.csvreader import read_architectures
2+
3+
def main():
4+
# Example usage
5+
csv_path = 'data/testcases/1-tiny-test-case/architecture.csv'
6+
architectures = read_architectures(csv_path)
7+
for architecture in architectures:
8+
print(architecture)
9+
10+
11+
12+
if __name__ == "__main__":
13+
main()

src/simulator/__init__.py

Whitespace-only changes.

src/simulator/main.py

Whitespace-only changes.

0 commit comments

Comments
 (0)