def get_flat_schema_view(annotation_type): Schema = get_flat_schema(annotation_type) ds = [] for col, field in Schema._declared_fields.items(): if isinstance(field, mm.fields.Nested): print(dir(field.schema), field.schema) schema = field.schema.__class__.__name__ else: schema = "" ds.append( { "field_name": col, "description": field.metadata.get("description", ""), "type": type(field).__name__, "schema": schema, } ) df = pd.DataFrame(ds) return render_template( "schema.html", df_table=df[["field_name", "type", "description", "schema"]].to_html(), schema_type=annotation_type, version=__version__, )
def get_linked_annotations(self, table_name: str, pcg_table_name: str, annotation_ids: List[int]) -> dict: """Get list of annotations from database by id. Parameters ---------- table_name : str name of annotation table pcg_table_name: str name of chunked graph reference table annotation_ids : int annotation id Returns ------- list list of annotation data dicts """ schema_type = self.get_table_schema(table_name) seg_table_name = build_segmentation_table_name(table_name, pcg_table_name) AnnotationModel = self._cached_table(table_name) SegmentationModel = self._cached_table(seg_table_name) annotations = (self.cached_session.query( AnnotationModel, SegmentationModel).join( SegmentationModel, SegmentationModel.id == AnnotationModel.id).filter( AnnotationModel.id.in_([x for x in annotation_ids])).all()) FlatSchema = get_flat_schema(schema_type) schema = FlatSchema(unknown=INCLUDE) data = [] for anno, seg in annotations: anno_data = anno.__dict__ seg_data = seg.__dict__ anno_data = { k: v for (k, v) in anno_data.items() if k != "_sa_instance_state" } seg_data = { k: v for (k, v) in seg_data.items() if k != "_sa_instance_state" } anno_data["created"] = str(anno_data.get("created")) anno_data["deleted"] = str(anno_data.get("deleted")) merged_data = {**anno_data, **seg_data} data.append(merged_data) return schema.load(data, many=True)
def test_synapse_flatten(): schema = SynapseSchema() result = schema.load(good_synapse) d = flatten_dict(result.data) print(d) assert (d['pre_pt_position'] == [31, 31, 0]) result = schema.load(supervoxel_synapse) assert (d['pre_pt_position'] == [31, 31, 0]) result = schema.load(supervoxel_rootId_synapse) assert (d['pre_pt_position'] == [31, 31, 0]) FlatSynapseSchema = get_flat_schema('synapse') schema = FlatSynapseSchema() result = schema.load(d) assert (len(result.errors) == 0)
def test_synapse_flatten(): schema = SynapseSchema() result = schema.load(good_synapse) d = flatten_dict(result) assert d["pre_pt_position"] == [31, 31, 0] result = schema.load(supervoxel_synapse) assert d["pre_pt_position"] == [31, 31, 0] result = schema.load(supervoxel_rootId_synapse) assert d["pre_pt_position"] == [31, 31, 0] FlatSynapseSchema = get_flat_schema("synapse") schema = FlatSynapseSchema() result = schema.load(d) assert len(result) == 8
def make_flat_model_from_schema(table_name: str, schema: str, segmentation_source: dict=None): if not annotation_models.contains_model(table_name, flat=True): flat_schema = get_flat_schema(schema) annotation_dict = create_table_dict( table_name=table_name, Schema=flat_schema, segmentation_source=segmentation_source, table_metadata=None, with_crud_columns=False, ) FlatAnnotationModel = type(table_name, (FlatBase,), annotation_dict) annotation_models.set_model(table_name, FlatAnnotationModel, flat=True) return annotation_models.get_model(table_name, flat=True)
def get_flat_schema_view(annotation_type): Schema = get_flat_schema(annotation_type) ds = [] for col, field in Schema._declared_fields.items(): if isinstance(field, mm.fields.Nested): print(dir(field.schema), field.schema) schema = field.schema.__class__.__name__ else: schema = '' ds.append({ 'field_name': col, 'description': field.metadata.get('description', ''), 'type': type(field).__name__, 'schema': schema }) df = pd.DataFrame(ds) return render_template('schema.html', df_table=df[['field_name', 'type', 'description', 'schema']].to_html(), schema_type=annotation_type, version=__version__)
def test_flatten_all(): types = get_types() for type_ in types: Schema = get_flat_schema(type_)
def test_bad_flatten(): with pytest.raises(UnknownAnnotationTypeException): get_flat_schema('NOT A REAL TYPE')