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

backend unit tests #42

Closed
wants to merge 7 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
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
8 changes: 6 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,8 +1,12 @@
# Usage

## To start the Flask Backend:
- cd into /birdmig/backend/app
- enter ```uvicorn base:app --reload``` in your terminal
- ```cd``` into ```/birdmig/backend/app```
- Run the backend
- To run the backend for showcase and documentation purposes, enter ```fastapi dev base.py``` in your terminal.
- Access docs by navigating to ```localhost:8000/docs``` on your web browser
- To run the backend for debugging purposes, run ```uvicorn base:app --reload``` in your terminal.
- Note that this only runs the backend. It can not be navigated to on the web browser, nor will documentation be available.

## To start the React Frontend:

Expand Down
2 changes: 0 additions & 2 deletions backend/.flaskenv

This file was deleted.

38 changes: 24 additions & 14 deletions backend/app/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -184,33 +184,29 @@ def get_precipitation_data(selectedYear: int):
return monthly_avg.to_dict(orient='records')


'''app.get('/bird-data/{bird_name}')
def get_bird_data(bird_name):
if bird_name in bird_data:
return bird_data'''


class PredictionInputs(BaseModel):
bird: str
year: int
emissions: str

@app.put('/prediction')
async def predict(prediction_input: PredictionInputs):
async def get_predictions(prediction_input: PredictionInputs):
selected_bird = prediction_input.bird
selected_year = str(prediction_input.year)
emission_Type = prediction_input.emissions

# For image data:
output_path = f"../../model/outputs/png-images/{birdsModelDirs[selected_bird]}/{emission_Type}/{selected_year}.png"
dataImg = Image.open(output_path)
buffer = BytesIO()
dataImg.save(buffer, format="png")

# Artificial delay to simulate model prediction time
#sleep(2.5)
# FastAPI requests can handle asynchronous predictions.
# In practice, this would be waiting for a machine learning model to generate
# predictions.
# Simualted here with a sleep() function.

# sleep(2.5)

#If we'll need to encapsulate a file, use this:
return {
"prediction": base64.b64encode(buffer.getvalue()).decode(),
"resFormat": dataImg.format
Expand All @@ -232,6 +228,16 @@ def get_bird_info(bird_name):
detail= f"data for bird {bird_name} does not exist.")


@app.get('/bird-sdm-data/{bird_name}')
def get_bird_sdm_data(bird_name):
bird = bird_data.get(bird_name)
if bird:
return {
'name': bird_name,
'sdmData': bird['sdmData']
}
else:
raise HTTPException(status_code=404, detail='Bird not found')
@app.get('/json/{filename}')
def send_json(filename):
climate_file_loc = os.path.join('climate_data/json_data', filename)
Expand All @@ -244,6 +250,7 @@ def send_json(filename):

@app.get('/get_trajectory_data')
def get_trajectory_data(bird: str, birdID: str):

filename = f'./data/{bird}.csv'
try:
df = pd.read_csv(filename)
Expand All @@ -253,34 +260,37 @@ def get_trajectory_data(bird: str, birdID: str):
if bird_data.empty:
raise HTTPException(
status_code= 404,
details= 'No trajectory data found for given bird ID')
detail= 'No trajectory data found for given bird ID')

# Convert data to dictionary format
trajectory_data = bird_data[['LATITUDE', 'LONGITUDE', 'TIMESTAMP']].to_dict(orient='records')
return trajectory_data
except FileNotFoundError:
raise HTTPException(
status_code= 404,
details= f'CSV file for {bird} not found')
detail= f'CSV file for {filename} not found')


@app.get('/get_bird_ids')
def get_bird_ids(bird: str):
filename = f'./data/{bird}.csv'

try:
df = pd.read_csv(filename)
bird_ids = df['ID'].unique().tolist()
return bird_ids
except FileNotFoundError:
raise HTTPException(
status_code=404,
detail= f'CSV file for {bird} not found')
detail= f'CSV file for {filename} not found')


@app.get('/get_general_migration')
def get_general_migration(selected_bird: str):
filename = f'./data/{selected_bird}.csv'

print(filename)

try:
df = pd.read_csv(filename, low_memory=False)

Expand Down
1 change: 1 addition & 0 deletions backend/requirements.txt
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ python-dotenv
jinja2
pandas
Shapely
pillow

pytest
requests
Expand Down
242 changes: 0 additions & 242 deletions backend/tests/test_basic.py

This file was deleted.

Loading
Loading