Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Dev #12

Merged
merged 28 commits into from
May 5, 2021
Merged

Dev #12

Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
28 commits
Select commit Hold shift + click to select a range
f00b9cf
switch to Foundation (ZURB) for html grid layout / radio buttons for …
fsumpa Mar 25, 2021
8ef87ba
Merge pull request #9 from rl-institut/combine_side_bars_and_use_foun…
fsumpa Mar 25, 2021
f406237
update requirements to avoid conflicts with packages from sgdot
fsumpa Mar 29, 2021
9d9ba20
refresh design of sidebar and remove footer
fsumpa Apr 21, 2021
98483f5
refresh header
fsumpa Apr 21, 2021
5e7eb57
update requirements.txt and Dockerfile
fsumpa Apr 21, 2021
5e8572e
resize font size of box titles in sidebar
fsumpa Apr 21, 2021
c52b385
create shs identification box in HTML/CSS
fsumpa Apr 21, 2021
238caac
set up shs_identification request so that it reaches FastAPI with dum…
fsumpa Apr 22, 2021
a596d98
use full names for objects of models.py
fsumpa Apr 22, 2021
5d76d84
removed unecessary parameters (north, south, east, west) from boundar…
fsumpa Apr 22, 2021
7e81ec5
update .gitignore
fsumpa Apr 22, 2021
818dd13
set default required_capacity and max_power to nodes that are added i…
fsumpa Apr 22, 2021
d2e8d77
copied python file from shs_identification package
fsumpa Apr 23, 2021
8bd286c
copy python file form shs_identification repo, add shs identification…
fsumpa Apr 26, 2021
08a420c
add shs marker
fsumpa Apr 26, 2021
04dd49c
Change wording in SHS identification box
fsumpa Apr 26, 2021
7a9cae5
working on new shs identification algo
fsumpa Apr 28, 2021
a443aa5
fix bugs in shs_identification function leading to errors, start to d…
fsumpa Apr 29, 2021
9e0d8ec
first version of reviewed nodes_to_discard function
fsumpa Apr 30, 2021
98da44a
update name of nodes_to_discard function
fsumpa Apr 30, 2021
7ca4821
remove butoon calling old shs identification feature, add algo paramt…
fsumpa Apr 30, 2021
7e1100c
display message in console when starting shs_identification
fsumpa May 3, 2021
fac0812
add shs characteristics dropdown to UI
fsumpa May 4, 2021
c06d71c
finalize first version of shs_characteristics input apprearing on hover
fsumpa May 5, 2021
a9ff08f
set up button to open shs price characterisitcs inputs area
fsumpa May 5, 2021
35cd333
Merge pull request #11 from rl-institut/add_shs_characteristics_with_…
fsumpa May 5, 2021
427abc0
refine design of set_shs_price_characteristics button
fsumpa May 5, 2021
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
.vscode/
fastapi_app/data/
fastapi_app/draft.ipynb
.ipynb_checkpoints/
*.db
run
venv/
2 changes: 1 addition & 1 deletion fastapi_app/Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -20,4 +20,4 @@ RUN pip install gunicorn
EXPOSE 5001

# run the app server, the last argument match the app variable in the webapp.py file
CMD ["uvicorn", "webapp:app", "--host", "0.0.0.0", "--port", "5001"]
CMD ["uvicorn", "main:app", "--host", "0.0.0.0", "--port", "5001"]
128 changes: 120 additions & 8 deletions fastapi_app/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@
from fastapi.staticfiles import StaticFiles
from database import SessionLocal, engine
import models
from models import Nodes, AddNodeRequest, OptimizeGridRequest, ValidateBoundariesRequest
from sqlalchemy.orm import Session
import sqlite3
from sgdot.grids import Grid
Expand All @@ -13,6 +12,11 @@
import urllib.request
import json
import tools.boundary_identification as bi
import tools.shs_identification as shs_ident
import pandas as pd
import numpy as np

import time


app = FastAPI()
Expand All @@ -36,7 +40,7 @@ def get_db():

@app.get("/")
def home(request: Request, db: Session = Depends(get_db)):
nodes = db.query(Nodes)
nodes = db.query(models.Nodes)
return templates.TemplateResponse("home.html", {
"request": request, "nodes": nodes
})
Expand Down Expand Up @@ -70,7 +74,7 @@ async def get_links(request: Request, db: Session = Depends(get_db)):

@app.post("/validate_boundaries")
async def validate_boundaries(
validateBoundariesRequest: ValidateBoundariesRequest,
validateBoundariesRequest: models.ValidateBoundariesRequest,
db: Session = Depends(get_db)):

boundary_coordinates = validateBoundariesRequest.boundary_coordinates
Expand Down Expand Up @@ -105,29 +109,34 @@ async def validate_boundaries(
if mask_building_within_boundaries[key]
}
for label, coordinates in building_coordidates_within_boundaries.items():
nodes = Nodes()
nodes = models.Nodes()

nodes.latitude = coordinates[0]
nodes.longitude = coordinates[1]
nodes.node_type = "undefined"
nodes.fixed_type = False

# nodes.required_capacity = validateBoundariesRequest.default_required_capacity
# nodes.max_power = validateBoundariesRequest.default_max_power
nodes.required_capacity = validateBoundariesRequest.default_required_capacity
nodes.max_power = validateBoundariesRequest.default_max_power
db.add(nodes)
db.commit()

return formated_geojson


@app.post("/add_node/")
async def add_node(add_node_request: AddNodeRequest,
async def add_node(add_node_request: models.AddNodeRequest,
background_tasks: BackgroundTasks,
db: Session = Depends(get_db)):
nodes = Nodes()
nodes = models.Nodes()

nodes.latitude = add_node_request.latitude
nodes.longitude = add_node_request.longitude
nodes.node_type = add_node_request.node_type
nodes.fixed_type = add_node_request.fixed_type
nodes.required_capacity = add_node_request.required_capacity
nodes.max_power = add_node_request.max_power

db.add(nodes)
db.commit()
Expand All @@ -139,7 +148,7 @@ async def add_node(add_node_request: AddNodeRequest,


@app.post("/optimize_grid/")
async def optimize_grid(optimize_grid_request: OptimizeGridRequest,
async def optimize_grid(optimize_grid_request: models.OptimizeGridRequest,
background_tasks: BackgroundTasks,
db: Session = Depends(get_db)):
# Create GridOptimizer object
Expand Down Expand Up @@ -252,6 +261,109 @@ async def optimize_grid(optimize_grid_request: OptimizeGridRequest,
}


@app.post("/shs_identification/")
def identify_shs(shs_identification_request: models.ShsIdentificationRequest,
db: Session = Depends(get_db)):

print("starting shs_identification...")

res = db.execute("select * from nodes")
nodes = res.fetchall()

r = 6371000 # Radius of the earth [m]
# use latitude of the node that is the most west to set origin of x coordinates
latitude_0 = math.radians(min([node[1] for node in nodes]))
# use latitude of the node that is the most south to set origin of y coordinates
longitude_0 = math.radians(min([node[2] for node in nodes]))

nodes_df = shs_ident.create_nodes_df()

cable_price_per_meter =\
shs_identification_request.cable_price_per_meter_for_shs_mst_identification
additional_price_for_connection_per_node =\
shs_identification_request.additional_connection_price_for_shs_mst_identification
shs_characteristics = pd.DataFrame(
{'price[$]': pd.Series([], dtype=float),
'capacity[Wh]': pd.Series([], dtype=np.dtype(float)),
'max_power[W]': pd.Series([], dtype=np.dtype(float))
}
)
shs_characteristics.loc[shs_characteristics.shape[0]] = [10, 100, 50000]
shs_characteristics.loc[shs_characteristics.shape[0]] = [20, 200, 150000]
shs_characteristics.loc[shs_characteristics.shape[0]] = [100, 1000, 5000000]

new_shs_characteristics = pd.DataFrame(
{'price[$]': pd.Series([], dtype=float),
'capacity[Wh]': pd.Series([], dtype=np.dtype(float)),
'max_power[W]': pd.Series([], dtype=np.dtype(float))
}
)

for shs_characteristic in shs_identification_request.shs_characteristics:
new_shs_characteristics.loc[new_shs_characteristics.shape[0]] = [
float(shs_characteristic['price']),
float(shs_characteristic['capacity']),
float(shs_characteristic['max_power'])]

for node in nodes:
latitude = math.radians(node[1])
longitude = math.radians(node[2])

x = r * (longitude - longitude_0) * math.cos(latitude_0)
y = r * (latitude - latitude_0)

node_label = node[0]
required_capacity = node[4]
max_power = node[4]

shs_ident.add_node(nodes_df, node_label, x, y, required_capacity, max_power)
links_df = shs_ident.mst_links(nodes_df)
start_time = time.time()

if shs_identification_request.algo == "mst1":
nodes_to_discard = shs_ident.nodes_to_discard(
nodes_df=nodes_df,
links_df=links_df,
cable_price_per_meter=cable_price_per_meter,
additional_price_for_connection_per_node=additional_price_for_connection_per_node,
shs_characteristics=new_shs_characteristics)
print(f"execution time for shs identification (mst1): {time.time() - start_time} s")
else:
print("issue with version parameter of shs_identification_request")
return 0

sqliteConnection = sqlite3.connect(grid_db)
conn = sqlite3.connect(grid_db)
cursor = conn.cursor()

for index in nodes_df.index:
if index in nodes_to_discard:
sql_delete_query = (
f"""UPDATE nodes
SET node_type = 'shs'
WHERE id = {index};
""")
else:
sql_delete_query = (
f"""UPDATE nodes
SET node_type = 'undefined'
WHERE id = {index};
""")
cursor.execute(sql_delete_query)
sqliteConnection.commit()
cursor.close()

# commit the changes to db
conn.commit()
# close the connection
conn.close()

return {
"code": "success",
"message": "shs identified"
}


@ app.post("/clear_node_db/")
async def clear_nodes():
sqliteConnection = sqlite3.connect(grid_db)
Expand Down
13 changes: 13 additions & 0 deletions fastapi_app/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@ class Nodes(Base):
longitude = Column(Numeric(10, 4))
node_type = Column(String)
fixed_type = Column(Boolean)
required_capacity = Column(Numeric(10, 4))
max_power = Column(Numeric(10, 4))


class Links(Base):
Expand All @@ -35,6 +37,8 @@ class AddNodeRequest(BaseModel):
longitude: float
node_type: str
fixed_type: bool
required_capacity: float
max_power: float


class OptimizeGridRequest(BaseModel):
Expand All @@ -44,5 +48,14 @@ class OptimizeGridRequest(BaseModel):
price_distribution_cable: float


class ShsIdentificationRequest(BaseModel):
cable_price_per_meter_for_shs_mst_identification: float
additional_connection_price_for_shs_mst_identification: float
algo: str
shs_characteristics: list


class ValidateBoundariesRequest(BaseModel):
boundary_coordinates: list
default_required_capacity: float
default_max_power: float
24 changes: 2 additions & 22 deletions fastapi_app/requirements.txt
Original file line number Diff line number Diff line change
@@ -1,45 +1,25 @@
aiofiles==0.6.0
autopep8==1.5.5
backcall==0.2.0
certifi==2020.12.5
chardet==4.0.0
click==7.1.2
decorator==4.4.2
fastapi==0.63.0
gunicorn==20.1.0
h11==0.12.0
idna==2.10
ipykernel==5.4.3
ipython==7.20.0
ipython-genutils==0.2.0
jedi==0.18.0
Jinja2==2.11.3
jupyter-client==6.1.11
jupyter-core==4.7.1
lxml==4.6.2
MarkupSafe==1.1.1
multitasking==0.0.9
numpy==1.20.1
pandas==1.2.1
parso==0.8.1
pexpect==4.8.0
pickleshare==0.7.5
prompt-toolkit==3.0.14
ptyprocess==0.7.0
pycodestyle==2.6.0
pydantic==1.7.3
Pygments==2.7.4
python-dateutil==2.8.1
pytz==2021.1
pyzmq==22.0.2
requests==2.25.1
six==1.15.0
sgdot==1.301
sgdot==1.303
SQLAlchemy==1.3.23
starlette==0.13.6
toml==0.10.2
tornado==6.1
traitlets==5.0.5
typing-extensions==3.7.4.3
urllib3==1.26.3
uvicorn==0.13.3
wcwidth==0.2.5
Binary file added fastapi_app/static/images/markers/marker-shs.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading