Matthias GOUPIL commited on
Commit
1c015d5
·
1 Parent(s): 8062743

feat(pathfinder): create CSVTravelGraph Class to create the graph with a csv

Browse files
app/travel_resolver/libs/pathfinder/CSVTravelGraph.py ADDED
@@ -0,0 +1,38 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import pandas as pd
2
+ from typing import Dict
3
+
4
+ class CSVTravelGraph():
5
+ def __init__(self, csv_file: str, mode: str):
6
+ """
7
+ Read csv and create the graph in function of the given mode
8
+ """
9
+ self.csv = pd.read_csv(csv_file, sep="\t")
10
+ self.mode = mode
11
+ if(mode =="Dijkstra"): self.data = self.generateDijkstra()
12
+
13
+ def generateDijkstra(self):
14
+ """
15
+ Create a Dijkstra graph by browsing the data retrieved in the csv
16
+ Returns:
17
+ (Dict[str, Dict[str, float]]): The Dijkstra Graph
18
+ """
19
+ graph: Dict[str, Dict[str, float]] = {}
20
+
21
+ def addTravelToTheGraph(depart, arrive, duree):
22
+ if depart in graph:
23
+ graph[depart][arrive] = duree
24
+ else:
25
+ graph[depart] = {arrive: duree}
26
+
27
+ for index, row in self.csv.iterrows():
28
+ trip_id, trajet, duree = row
29
+ points = trajet.split(' - ')
30
+ depart = ' - '.join(points[:-1])
31
+ arrive = points[-1]
32
+ duree = float(duree)
33
+
34
+ addTravelToTheGraph(depart, arrive, duree)
35
+ addTravelToTheGraph(arrive, depart, duree)
36
+
37
+ return graph
38
+