Commit 7e767cc1 authored by Alexander Lercher's avatar Alexander Lercher

Adapted time slicing for local cluster json files

parent 4d887b0a
# ignore this folder because it contains raw jsons for the community-prediction task
**
!.gitignore
\ No newline at end of file
...@@ -13,7 +13,7 @@ from typing import Tuple, Dict, Any, List ...@@ -13,7 +13,7 @@ from typing import Tuple, Dict, Any, List
TimeSliceKey = Tuple[int, int] TimeSliceKey = Tuple[int, int]
# TODO extract information about time features (maybe from table mapping) # TODO extract information about time features (maybe from table mapping)
TIME_PROPERTY_NAMES = ['Timestamp'] TIME_PROPERTY_NAMES = ['timestamp']
repo = Repository() repo = Repository()
...@@ -21,7 +21,8 @@ repo = Repository() ...@@ -21,7 +21,8 @@ repo = Repository()
def convert_to_time_slice_key(timestamp: str) -> TimeSliceKey: def convert_to_time_slice_key(timestamp: str) -> TimeSliceKey:
'''Returns the tuple (year, week_of_year) from a timestamp. This is used as the key for the slicing.''' '''Returns the tuple (year, week_of_year) from a timestamp. This is used as the key for the slicing.'''
# time = datetime.utcfromtimestamp(float(timestamp[0:10])) # time = datetime.utcfromtimestamp(float(timestamp[0:10]))
time = datetime.strptime(timestamp, '%Y-%m-%d %H:%M:%S') # time = datetime.strptime(timestamp, '%Y-%m-%d %H:%M:%S')
time = datetime.utcfromtimestamp(float(timestamp))
(y, w, _) = time.isocalendar() (y, w, _) = time.isocalendar()
return (y, w) return (y, w)
...@@ -55,17 +56,39 @@ def split_clusterset_by_time(layer: Layer, clusters: List[Cluster]) -> Dict[Time ...@@ -55,17 +56,39 @@ def split_clusterset_by_time(layer: Layer, clusters: List[Cluster]) -> Dict[Time
return time_slices return time_slices
def get_layers(): def get_layers() -> List[Layer]:
return repo.get_layers() return repo.get_layers()
def get_clusters_for_layer(use_case, use_case_table, layer_name): def get_clusters_for_layer(use_case, use_case_table, layer_name)-> List[Cluster]:
return repo.get_clusters_for_layer(use_case, use_case_table, layer_name) # return repo.get_clusters_for_layer(use_case, use_case_table, layer_name)
json_path = f'_predictions/clusters/{layer_name}.json'
def get_layer_nodes(use_case, use_case_table, layer_name): if os.path.exists(json_path):
return repo.get_layer_nodes(use_case, use_case_table, layer_name) with open(json_path, 'r') as file:
return [Cluster(cluster_dict=e, from_db=False) for e in json.loads(file.read())]
return []
def get_layer_nodes(use_case, use_case_table, layer_name)-> List[dict]:
# return repo.get_layer_nodes(use_case, use_case_table, layer_name)
return []
def add_time_slice(timeslice): def add_time_slice(timeslice):
repo.add_time_slice(timeslice) try:
# repo.add_time_slice(timeslice)
pass
except:
print(f"Error while storing time slice in db for {timeslice.layer_name}")
try:
json_path = f'_predictions/timeslices/{timeslice.layer_name}/{timeslice.time}.json'.replace(', ', '_').replace('(', '').replace(')', '')
if not os.path.exists(os.path.dirname(json_path)):
os.makedirs(os.path.dirname(json_path))
with open(json_path, 'w') as file:
file.write(json.dumps(timeslice.to_serializable_dict(for_db=False)))
except Exception as e:
print(f"Error while writing json for {timeslice.layer_name}: {e}")
def run_time_slicing(selected_use_cases: List[str] = None, selected_use_case_tables: List[str] = None, selected_layer_names: List[str] = None): def run_time_slicing(selected_use_cases: List[str] = None, selected_use_case_tables: List[str] = None, selected_layer_names: List[str] = None):
layers = get_layers() layers = get_layers()
...@@ -98,5 +121,5 @@ def run_time_slicing(selected_use_cases: List[str] = None, selected_use_case_tab ...@@ -98,5 +121,5 @@ def run_time_slicing(selected_use_cases: List[str] = None, selected_use_case_tab
if __name__ == "__main__": if __name__ == "__main__":
repo.remove_all_time_slices() # repo.remove_all_time_slices()
run_time_slicing([],[],[]) run_time_slicing(selected_use_cases=['community-prediction-taxi'])
\ No newline at end of file \ No newline at end of file
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment