Skip to content

Commit

Permalink
removes pickle (#257)
Browse files Browse the repository at this point in the history
  • Loading branch information
mekhlakapoor authored Sep 4, 2024
1 parent cda4c85 commit 7d791cb
Show file tree
Hide file tree
Showing 9 changed files with 51 additions and 317 deletions.
16 changes: 4 additions & 12 deletions src/aind_metadata_service/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -36,44 +36,36 @@ def __init__(self, domain: str):
self.procedures_url = f"{self.domain}/procedures"
self.injection_materials_url = f"{self.domain}/injection_materials"

def get_subject(
self, subject_id: str, pickle: bool = False
) -> requests.Response:
def get_subject(self, subject_id: str) -> requests.Response:
"""
Retrieve a subject response from the server
Parameters
----------
subject_id : str
id of the subject
pickle: bool
option to return pickled data instead of json
Returns
-------
requests.Response
"""
url = "/".join([self.subject_url, subject_id])
with requests.get(url, params={"pickle": pickle}) as response:
with requests.get(url, params={}) as response:
return response

def get_procedures(
self, subject_id: str, pickle: bool = False
) -> requests.Response:
def get_procedures(self, subject_id: str) -> requests.Response:
"""
Retrieve a procedures response from the server
Parameters
----------
subject_id : str
id of the subject
pickle: bool
option to return pickled data instead of json
Returns
-------
requests.Response
"""
url = "/".join([self.procedures_url, subject_id])
with requests.get(url, params={"pickle": pickle}) as response:
with requests.get(url, params={}) as response:
return response
51 changes: 12 additions & 39 deletions src/aind_metadata_service/response_handler.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
"""Module to handle responses"""

import json
import pickle
from typing import Generic, List, Optional, TypeVar, Union

from aind_data_schema.core.data_description import Funding
Expand Down Expand Up @@ -91,7 +90,7 @@ def no_data_found_error_response(cls):
)

def _map_data_response( # noqa: C901
self, pickled: bool = False, validate: bool = True
self, validate: bool = True
) -> Union[Response, JSONResponse]:
"""Map ModelResponse with StatusCodes.DB_RESPONDED to a JSONResponse.
Perform validations, bypasses validation if flag is set to False."""
Expand All @@ -101,12 +100,9 @@ def _map_data_response( # noqa: C901
message = "No Data Found."
elif len(self.aind_models) == 1:
aind_model = self.aind_models[0]
if pickled:
content_data = aind_model
else:
content_data = jsonable_encoder(
json.loads(aind_model.model_dump_json())
)
content_data = jsonable_encoder(
json.loads(aind_model.model_dump_json())
)
if validate:
validation_error = None
try:
Expand Down Expand Up @@ -139,24 +135,14 @@ def _map_data_response( # noqa: C901
else:
status_code = StatusCodes.MULTIPLE_RESPONSES.value
message = "Multiple Items Found."
if pickled:
content_data = self.aind_models
else:
content_data = [
jsonable_encoder(json.loads(model.model_dump_json()))
for model in self.aind_models
]
if pickled:
return Response(
status_code=status_code,
content=pickle.dumps(content_data),
media_type="application/octet-stream",
)
else:
return JSONResponse(
status_code=status_code,
content=({"message": message, "data": content_data}),
)
content_data = [
jsonable_encoder(json.loads(model.model_dump_json()))
for model in self.aind_models
]
return JSONResponse(
status_code=status_code,
content=({"message": message, "data": content_data}),
)

def map_to_json_response(self, validate: bool = True) -> JSONResponse:
"""Map a ModelResponse to a JSONResponse."""
Expand All @@ -174,19 +160,6 @@ def map_to_json_response(self, validate: bool = True) -> JSONResponse:
response = self._map_data_response(validate=validate)
return response

def map_to_pickled_response(self) -> Response:
"""Map a ModelResponse to a pickled response."""

if self.status_code != StatusCodes.DB_RESPONDED:
response = Response(
status_code=self.status_code.value,
content=pickle.dumps(None),
media_type="application/octet-stream",
)
else:
response = self._map_data_response(pickled=True)
return response


class EtlResponse:
"""Handle responses from EtlJobs"""
Expand Down
79 changes: 20 additions & 59 deletions src/aind_metadata_service/server.py
Original file line number Diff line number Diff line change
Expand Up @@ -110,35 +110,22 @@ async def retrieve_bergamo_session(job_settings: BergamoJobSettings):


@app.get("/instrument/{instrument_id}")
async def retrieve_instrument(instrument_id, pickle: bool = False):
"""
Retrieves instrument from slims
Returns pickled data if URL parameter pickle=True, else returns json
"""
async def retrieve_instrument(instrument_id):
"""Retrieves instrument from slims"""
model_response = slims_client.get_instrument_model_response(instrument_id)
if pickle:
return model_response.map_to_pickled_response()
else:
return model_response.map_to_json_response(validate=False)
return model_response.map_to_json_response(validate=False)


@app.get("/rig/{rig_id}")
async def retrieve_rig(rig_id, pickle: bool = False):
"""
Retrieves rig from slims
Returns pickled data if URL parameter pickle=True, else returns json
"""
async def retrieve_rig(rig_id):
"""Retrieves rig from slims"""
model_response = slims_client.get_rig_model_response(rig_id)
if pickle:
return model_response.map_to_pickled_response()
else:
return model_response.map_to_json_response(validate=False)
return model_response.map_to_json_response(validate=False)


@app.get("/protocols/{protocol_name}")
async def retrieve_protocols(
protocol_name,
pickle: bool = False,
):
"""Retrieves perfusion information from smartsheet"""

Expand All @@ -150,15 +137,11 @@ async def retrieve_protocols(
smart_sheet_response=smart_sheet_response, input_id=protocol_name
)
model_response = mapper.get_model_response()

if pickle:
return model_response.map_to_pickled_response()
else:
return model_response.map_to_json_response()
return model_response.map_to_json_response()


@app.get("/perfusions/{subject_id}")
async def retrieve_perfusions(subject_id, pickle: bool = False):
async def retrieve_perfusions(subject_id):
"""Retrieves perfusion information from smartsheet"""

# TODO: We can probably cache the response if it's 200
Expand All @@ -169,15 +152,11 @@ async def retrieve_perfusions(subject_id, pickle: bool = False):
smart_sheet_response=smart_sheet_response, input_id=subject_id
)
model_response = mapper.get_model_response()

if pickle:
return model_response.map_to_pickled_response()
else:
return model_response.map_to_json_response()
return model_response.map_to_json_response()


@app.get("/funding/{project_name}")
async def retrieve_funding(project_name, pickle: bool = False):
async def retrieve_funding(project_name):
"""Retrieves funding information from smartsheet"""

# TODO: We can probably cache the response if it's 200
Expand All @@ -188,10 +167,7 @@ async def retrieve_funding(project_name, pickle: bool = False):
smart_sheet_response=smart_sheet_response, input_id=project_name
)
model_response = mapper.get_model_response()
if pickle:
return model_response.map_to_pickled_response()
else:
return model_response.map_to_json_response()
return model_response.map_to_json_response()


@app.get("/project_names")
Expand All @@ -210,43 +186,34 @@ async def retrieve_project_names():


@app.get("/subject/{subject_id}")
async def retrieve_subject(subject_id, pickle: bool = False):
async def retrieve_subject(subject_id):
"""
Retrieves subject from Labtracks server
Returns pickled data if URL parameter pickle=True, else returns json
"""
lb_client = LabTracksClient.from_settings(labtracks_settings)
model_response = await run_in_threadpool(
lb_client.get_subject_info, subject_id=subject_id
)
if pickle:
return model_response.map_to_pickled_response()
else:
return model_response.map_to_json_response()
return model_response.map_to_json_response()


@app.get("/tars_injection_materials/{prep_lot_number}")
async def retrieve_injection_materials(prep_lot_number, pickle: bool = False):
async def retrieve_injection_materials(prep_lot_number):
"""
Retrieves injection materials from TARS server
Returns pickled data if URL parameter pickle=True, else returns json
"""
model_response = await run_in_threadpool(
tars_client.get_injection_materials_info,
prep_lot_number=prep_lot_number,
)
# TODO: move molecules call to here
if pickle:
return model_response.map_to_pickled_response()
else:
return model_response.map_to_json_response()
return model_response.map_to_json_response()


@app.get("/procedures/{subject_id}")
async def retrieve_procedures(subject_id, pickle: bool = False):
async def retrieve_procedures(subject_id):
"""
Retrieves procedure info from SharePoint and Labtracks servers
Returns pickled data if URL parameter pickle=True, else returns json
"""
sharepoint_client = SharePointClient.from_settings(sharepoint_settings)
lb_client = LabTracksClient.from_settings(labtracks_settings)
Expand Down Expand Up @@ -297,19 +264,13 @@ async def retrieve_procedures(subject_id, pickle: bool = False):
smart_sheet_response=smart_sheet_response, input_id=protocol_name
)
model_response = mapper.get_model_response()
# smartsheet_response = await retrieve_protocols(
# protocol_name=protocol_name
# )
protocols_mapping[
protocol_name
] = model_response.map_to_json_response()
protocols_mapping[protocol_name] = (
model_response.map_to_json_response()
)
integrated_response = protocols_integrator.integrate_protocols(
response=integrated_response, protocols_mapping=protocols_mapping
)
if pickle:
return integrated_response.map_to_pickled_response()
else:
return integrated_response.map_to_json_response()
return integrated_response.map_to_json_response()


@app.get(
Expand Down
6 changes: 3 additions & 3 deletions src/aind_metadata_service/sharepoint/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -268,9 +268,9 @@ def _merge_two_responses(
)
else:
left_procedures: List[Procedures] = left_model_response.aind_models
right_procedures: List[
Procedures
] = right_model_response.aind_models
right_procedures: List[Procedures] = (
right_model_response.aind_models
)
procedures = self._merge_procedures(
left_procedures=left_procedures,
right_procedures=right_procedures,
Expand Down
1 change: 1 addition & 0 deletions src/aind_metadata_service/sharepoint/las2020/mapping.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
"""Module template was autogenerated from sharepoint list schema."""

from dataclasses import dataclass
from datetime import date, datetime
from decimal import Decimal, DecimalException
Expand Down
8 changes: 4 additions & 4 deletions src/aind_metadata_service/smartsheet/protocols/mapping.py
Original file line number Diff line number Diff line change
Expand Up @@ -91,10 +91,10 @@ def _get_protocol_list(
"""
rows_associated_with_protocol_name: List[ProtocolInformation] = []
for row in self.model.rows:
opt_protocol: Optional[
ProtocolInformation
] = self._map_row_to_protocol(
row=row, input_protocol_name=protocol_name
opt_protocol: Optional[ProtocolInformation] = (
self._map_row_to_protocol(
row=row, input_protocol_name=protocol_name
)
)
if opt_protocol is not None:
rows_associated_with_protocol_name.append(opt_protocol)
Expand Down
6 changes: 3 additions & 3 deletions src/aind_metadata_service/tars/mapping.py
Original file line number Diff line number Diff line change
Expand Up @@ -270,9 +270,9 @@ def integrate_injection_materials(
new_material.titer = (
injection_material.titer
)
procedure.injection_materials[
idx
] = new_material
procedure.injection_materials[idx] = (
new_material
)
elif (
tars_response.status_code
== StatusCodes.NO_DATA_FOUND.value
Expand Down
Loading

0 comments on commit 7d791cb

Please sign in to comment.