Jan commited on
Commit
9528692
·
1 Parent(s): c71f556

wrap scoring functionality in a class to be called by frontend

Browse files
data_processing/data_provider.py ADDED
@@ -0,0 +1,94 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ from get_smallAreaInfo import get_smallAreas
2
+ from point_scoring import score_current
3
+ from get_station_coverage import get_station_coverage
4
+ from aggregate_data import get_feature_df
5
+ import os
6
+ import pandas as pd
7
+ from pyproj import Transformer
8
+
9
+
10
+ class data_provider():
11
+
12
+ def __init__(self):
13
+ self.smsv_list = get_smallAreas()
14
+ self.df = get_feature_df()
15
+ self.transformer_to_3057 = Transformer.from_crs("EPSG:4326", "EPSG:3057", always_xy=True)
16
+ self.transformer_to_4326 = Transformer.from_crs("EPSG:3057", "EPSG:4326", always_xy=True)
17
+
18
+ def get_station_score(self, station_coord, w_density=1, w_income=1, w_age=1, radius=400, EPSG_4326=True):
19
+ """
20
+ Calculate the score for a station based on the given weights and radius.
21
+
22
+ Parameters:
23
+ ----------
24
+ station_coord : tuple
25
+ Coordinates of the station in EPSG:4326 format (north, east) = (lat, long).
26
+ w_density : float
27
+ Weight for the density factor.
28
+ w_income : float
29
+ Weight for the income factor.
30
+ w_age : float
31
+ Weight for the age factor.
32
+ radius : float
33
+ The radius (in meters) around the station to consider.
34
+ EPSG_4326 : bool
35
+ if a true, station coord of format EPSG:4326 is expected, else EPSG:3057
36
+
37
+ Returns:
38
+ -------
39
+ dict
40
+ Dictionary that contains total score and subscores
41
+ {"total_score": total_score, "income_score": income_score, "age_score": age_score, "density_score": density_score}
42
+ """
43
+ if EPSG_4326:
44
+ station_coord = self.__convert_to_3057(station_coord)
45
+ cov = get_station_coverage(self.smsv_list, station_coord, radius)
46
+ scores = score_current(station_coord, self.df, cov, w_density, w_income, w_age)
47
+ return scores
48
+
49
+ def __convert_to_3057(self, station_coord):
50
+ """
51
+ Convert coordinates from EPSG:4326 (lon, lat) to EPSG:3057 (x, y).
52
+
53
+ Parameters:
54
+ ----------
55
+ station_coord : tuple
56
+ Coordinates in EPSG:4326 format (lon, lat).
57
+
58
+ Returns:
59
+ -------
60
+ tuple
61
+ Coordinates in EPSG:3057 format (x, y).
62
+ """
63
+ return self.transformer_to_3057.transform(*station_coord)
64
+
65
+ def __convert_to_4326(self, station_coord):
66
+ """
67
+ Convert coordinates from EPSG:3057 (x, y) to EPSG:4326 (lon, lat).
68
+
69
+ Parameters:
70
+ ----------
71
+ station_coord : tuple
72
+ Coordinates in EPSG:3057 format (x, y).
73
+
74
+ Returns:
75
+ -------
76
+ tuple
77
+ Coordinates in EPSG:4326 format (lon, lat).
78
+ """
79
+ return self.transformer_to_4326.transform(*station_coord)
80
+
81
+
82
+
83
+
84
+ if __name__ == '__main__':
85
+ dummy_coord1 = (356250.0, 408250.0) # EPSG:3057 coordinates
86
+ dummy_coord2 = (-21.910388, 64.144947) # EPSG:4326 coordinates
87
+ dummy_coord3 = (358374.26032876654, 407938.72289760906) # ISN93/Lambert
88
+ # dummy_coord3 =
89
+ backend = data_provider()
90
+ print("dummy_coord1: ", backend.get_station_score(dummy_coord1, EPSG_4326=False))
91
+ print("dummy_coord2: ", backend.get_station_score(dummy_coord2))
92
+ print("dummy_coord3: ", backend.get_station_score(dummy_coord3, EPSG_4326=False))
93
+
94
+
data_processing/point_scoring.py CHANGED
@@ -35,7 +35,10 @@ def score_current(station_coord, df_features, cov_smsv, w_density, w_income, w_a
35
  # Score = [for all areas in range((density) * coverage percentage)]
36
 
37
  # TODO: take into account fjoldi starfandi, if there are more people who work than live => many people need to get there, also works the other way around.
38
- final_score = 0
 
 
 
39
  for smsv in cov_smsv:
40
 
41
 
@@ -50,8 +53,8 @@ def score_current(station_coord, df_features, cov_smsv, w_density, w_income, w_a
50
  income_score = get_income_score(income_dist) * w_income
51
 
52
  density_score = smsv_info["density"].iloc[0] * w_density
53
- final_score += (age_score + income_score + density_score) * smsv["coverage_percentage"] # TODO: Area of the cricle * percent covered / total area of the small area
54
- return final_score
55
 
56
  def get_age_score(age_distribution):
57
  """
 
35
  # Score = [for all areas in range((density) * coverage percentage)]
36
 
37
  # TODO: take into account fjoldi starfandi, if there are more people who work than live => many people need to get there, also works the other way around.
38
+ total_score = 0
39
+ income_score = 0
40
+ density_score = 0
41
+ age_score = 0
42
  for smsv in cov_smsv:
43
 
44
 
 
53
  income_score = get_income_score(income_dist) * w_income
54
 
55
  density_score = smsv_info["density"].iloc[0] * w_density
56
+ total_score += (age_score + income_score + density_score) * smsv["coverage_percentage"] # TODO: Area of the cricle * percent covered / total area of the small area
57
+ return {"total_score": total_score, "income_score": income_score, "age_score": age_score, "density_score": density_score}
58
 
59
  def get_age_score(age_distribution):
60
  """
data_processing/test.py DELETED
@@ -1,25 +0,0 @@
1
- from get_smallAreaInfo import get_smallAreas
2
- from point_scoring import score_current
3
- from get_station_coverage import get_station_coverage
4
- from aggregate_data import get_feature_df
5
- import os
6
- import pandas as pd
7
-
8
-
9
- smsv_list = get_smallAreas()
10
- df = get_feature_df()
11
-
12
-
13
- station_coords = (356250.0, 408250.0) # EPSG:3057 coordinates
14
- w_density = 1
15
- w_income = 1
16
- w_age = 1
17
- radius = 400
18
-
19
- if __name__ == '__main__':
20
-
21
- cov = get_station_coverage(smsv_list, station_coords, radius)
22
- # print(cov)
23
- final_score = score_current(station_coords, df, cov, w_density, w_income, w_age)
24
-
25
- print(f'final score: {final_score}')