Skip to content
This repository has been archived by the owner on Aug 15, 2018. It is now read-only.

Commit

Permalink
Update queries for new table distribution model
Browse files Browse the repository at this point in the history
  • Loading branch information
HACKERMD committed Jun 21, 2017
1 parent fc9bb1c commit 8723d4a
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 67 deletions.
55 changes: 8 additions & 47 deletions tmserver/api/feature.py
Original file line number Diff line number Diff line change
Expand Up @@ -287,31 +287,6 @@ def get_feature_values(experiment_id, mapobject_type_id):
mapobject_type_name = mapobject_type.name
mapobject_type_ref_type = mapobject_type.ref_type

if mapobject_type_ref_type in {'Plate', 'Well'}:
if well_pos_y is not None:
raise MalformedRequestError(
'Invalid query parameter "well_pos_y" for mapobjects of type '
'"{0}"'.format(mapobject_type_name)
)
if well_pos_x is not None:
raise MalformedRequestError(
'Invalid query parameter "well_pos_x" for mapobjects of type '
'"{0}"'.format(mapobject_type_name)
)
if mapobject_type_ref_type == 'Plate':
ref_type = 'Plate'
if well_name is not None:
raise MalformedRequestError(
'Invalid query parameter "well_name" for mapobjects of type '
'"{0}"'.format(mapobject_type_name)
)
elif mapobject_type_ref_type == 'Well':
ref_type = 'Plate'
elif mapobject_type_ref_type == 'Site':
ref_type = 'Well'
else:
ref_type = 'Site'

filename_formatstring = '{experiment}'
if plate_name is not None:
filename_formatstring += '_{plate}'
Expand Down Expand Up @@ -358,7 +333,7 @@ def generate_feature_matrix(mapobject_type_id, ref_type):
feature_names = [f.name for f in features]

ref_mapobject_type = session.query(tm.MapobjectType.id).\
filter_by(ref_type=ref_type).\
filter_by(ref_type=ref_type, id=mapobject_type_id).\
one()

w.writerow(tuple(feature_names))
Expand All @@ -370,8 +345,7 @@ def generate_feature_matrix(mapobject_type_id, ref_type):
logger.debug('collect feature values for %s %d', ref_type, ref_id)
with tm.utils.ExperimentSession(experiment_id) as session:
mapobjects = _get_mapobjects_at_ref_position(
session, mapobject_type_id, ref_mapobject_type.id,
ref_id, layer_lut.keys()
session, mapobject_type_id, ref_id, layer_lut.keys()
)
mapobject_ids = [m.id for m in mapobjects]

Expand Down Expand Up @@ -422,7 +396,7 @@ def generate_feature_matrix(mapobject_type_id, ref_type):
data.truncate(0)

return Response(
generate_feature_matrix(mapobject_type_id, ref_type),
generate_feature_matrix(mapobject_type_id, mapobject_type_ref_type),
mimetype='text/csv',
headers={
'Content-Disposition': 'attachment; filename={filename}'.format(
Expand Down Expand Up @@ -509,19 +483,6 @@ def get_metadata(experiment_id, mapobject_type_id):
'"{0}"'.format(mapobject_type_name)
)

if mapobject_type_ref_type == 'Plate':
ref_type = 'Plate'
if well_name is not None:
raise MalformedRequestError(
'Invalid query parameter "well_name" for mapobjects of type '
'"{0}"'.format(mapobject_type_name)
)
elif mapobject_type_ref_type == 'Well':
ref_type = 'Well'
elif mapobject_type_ref_type == 'Site':
ref_type = 'Site'
else:
ref_type = 'Site'

def generate_feature_matrix(mapobject_type_id, ref_type):
data = StringIO()
Expand Down Expand Up @@ -579,19 +540,19 @@ def generate_feature_matrix(mapobject_type_id, ref_type):

ref_mapobject_type = session.query(tm.MapobjectType.id).\
filter_by(ref_type=ref_type).\
one()
order_by(tm.MapobjectType.id).\
first()

w.writerow(tuple(metadata_names + tool_result_names))
yield data.getvalue()
data.seek(0)
data.truncate(0)

for ref_id in ref_position_lut:
logger.debug('collect metadata for %s %d', ref_type, ref_id)
logger.info('collect metadata for %s %d', ref_type, ref_id)
with tm.utils.ExperimentSession(experiment_id) as session:
mapobjects = _get_mapobjects_at_ref_position(
session, mapobject_type_id, ref_mapobject_type.id,
ref_id, layer_lut.keys()
session, mapobject_type_id, ref_id, layer_lut.keys()
)
mapobject_ids = [m.id for m in mapobjects]

Expand Down Expand Up @@ -668,7 +629,7 @@ def generate_feature_matrix(mapobject_type_id, ref_type):
data.truncate(0)

return Response(
generate_feature_matrix(mapobject_type_id, ref_type),
generate_feature_matrix(mapobject_type_id, mapobject_type_ref_type),
mimetype='text/csv',
headers={
'Content-Disposition': 'attachment; filename={filename}'.format(
Expand Down
32 changes: 12 additions & 20 deletions tmserver/api/mapobject.py
Original file line number Diff line number Diff line change
Expand Up @@ -146,16 +146,7 @@ def _get_matching_layers(session, tpoint):


def _get_mapobjects_at_ref_position(session, mapobject_type_id,
ref_mapobject_type_id, ref_id, segmentation_layer_ids):
ref_segmentation = session.query(
tm.MapobjectSegmentation.geom_polygon
).\
join(tm.Mapobject).\
filter(
tm.Mapobject.ref_id == ref_id,
tm.Mapobject.mapobject_type_id == ref_mapobject_type_id
).\
one()
ref_id, segmentation_layer_ids):

return session.query(
tm.Mapobject.id, tm.MapobjectSegmentation.label,
Expand All @@ -164,9 +155,7 @@ def _get_mapobjects_at_ref_position(session, mapobject_type_id,
join(tm.MapobjectSegmentation).\
filter(
tm.Mapobject.mapobject_type_id == mapobject_type_id,
tm.MapobjectSegmentation.geom_centroid.ST_Intersects(
ref_segmentation.geom_polygon
),
tm.Mapobject.partition_key == ref_id,
tm.MapobjectSegmentation.segmentation_layer_id.in_(
segmentation_layer_ids
)
Expand All @@ -176,14 +165,14 @@ def _get_mapobjects_at_ref_position(session, mapobject_type_id,


def _get_border_mapobjects_at_ref_position(session, mapobject_ids,
ref_mapobject_type_id, ref_id):
ref_type_id, ref_id):
ref_segmentation = session.query(
tm.MapobjectSegmentation.geom_polygon
).\
join(tm.Mapobject).\
filter(
tm.Mapobject.ref_id == ref_id,
tm.Mapobject.mapobject_type_id == ref_mapobject_type_id
tm.Mapobject.partition_key == ref_id,
tm.Mapobject.mapobject_type_id == ref_type_id
).\
one()

Expand Down Expand Up @@ -305,7 +294,7 @@ def update_mapobject_type(experiment_id, mapobject_type_id):
)
@jwt_required()
@decode_query_ids('write')
def delete_mapobject_typed(experiment_id, mapobject_type_id):
def delete_mapobject_type(experiment_id, mapobject_type_id):
"""
.. http:delete:: /api/experiments/(string:experiment_id)/mapobject_types/(string:mapobject_type_id)
Expand Down Expand Up @@ -451,18 +440,21 @@ def add_segmentations(experiment_id, mapobject_type_id):
).\
one()
y_offset, y_offset = site.offset
site_id = site.id

metadata = SegmentationImageMetadata(
mapobject_type_id, site.id, tpoint, zplane
mapobject_type_id, site_id, tpoint, zplane
)
image = SegmentationImage(array, metadata)

# We need to use a raw connection, since we insert into a distributed table.
with tm.utils.ExperimentConnection(experiment_id) as connection:
for label, polygon in image.extract_polygons(y_offset, x_offset):
mapobject_id = tm.Mapobject.add(connection, mapobject_type_id)
mapobject_id = tm.Mapobject.add(
connection, site_id, mapobject_type_id
)
tm.MapobjectSegmentation.add(
connection, mapobject_id, segmentation_layer_id,
connection, site_id, mapobject_id, segmentation_layer_id,
polygon=polygon, label=label
)

Expand Down

0 comments on commit 8723d4a

Please sign in to comment.