Commit 0839a52c authored by Alexander Lercher's avatar Alexander Lercher

clustering with agi dataset

parent f11c4d13
......@@ -72,7 +72,7 @@ paths:
schema:
$ref: "#/definitions/Cluster"
/cluster.png:
/cluster/cluster.png:
get:
operationId: "rest.cluster.get_image"
tags:
......@@ -84,7 +84,33 @@ paths:
responses:
200:
description: "Successful operation"
/agi/cluster:
get:
operationId: "rest.agi_cluster.get"
tags:
- "Clusters"
summary: "Get clustered data"
parameters: []
responses:
200:
description: "Successful operation"
schema:
$ref: "#/definitions/Cluster"
/agi/cluster/cluster.png:
get:
operationId: "rest.agi_cluster.get_image"
tags:
- "Clusters"
summary: "Get clustered data as image"
parameters: []
produces:
- "image/png"
responses:
200:
description: "Successful operation"
definitions:
Location:
......
import json
from typing import List, Dict
class AgiRepository:
def getLocations(self) -> List:
locations = []
travels = self.readDataFromFile()
# only take started travels
travels = [t for t in travels if t['status'] >= 2]
for travel in travels:
locations.append(self.location(travel["id"], travel['startPlace.latitude'], travel['startPlace.longitude'], 0, ''))
continue # todo work on locations
# todo number of complete travels with startlocation and user data
num_complete_travels = min(len(travel['startedBy']), len(travel['users']))
for i in range(num_complete_travels):
cur_location = travel['startedBy'][i]
cur_user = travel['users'][i]
locations.append(
self.location(f'{travel["id"]}-{cur_location["moment"]}',
cur_location['coordinate']['latitude'],
cur_location['coordinate']['longitude'],
cur_location['moment'],
# todo user in travel startedBy not available from dataset - currently using user list
cur_user['userId']
))
return locations
def readDataFromFile(self) -> List[Dict]:
with open('./db/agi/travels.json', 'r') as f_travels:
travels = json.loads(f_travels.read())
return travels
def location(self, id_, lat, long_, timestamp, username) -> dict:
return {
"id": id_,
'latitude': lat,
'longitude': long_,
"timestamp": timestamp,
"username": username
}
......@@ -17,4 +17,4 @@ def api_root():
# start app
if __name__ == '__main__':
app.run(host='0.0.0.0', port=5000, debug=True, use_reloader=True)
app.run(host='0.0.0.0', port=5000, debug=True)
import io
from flask import request, Response
from db.agi.agi_repository import AgiRepository
from processing.clusterer import Clusterer
from matplotlib.backends.backend_agg import FigureCanvasAgg as FigureCanvas
repo = AgiRepository()
clusterer = Clusterer()
def get():
locations = repo.getLocations()
clusters = clusterer.run(locations)
return clusters
def get_image():
locations = repo.getLocations()
fig = clusterer.draw_locations(locations)
output = io.BytesIO()
FigureCanvas(fig).print_png(output)
return Response(output.getvalue(), mimetype="image/png")
\ 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