Ejemplo n.º 1
0
def lookup_by_old_uuid(old_uuid, client):
    query_filter = {"path": "nsg:providerId", "op": "eq", "value": old_uuid}
    context = {
        "nsg":
        "https://bbp-nexus.epfl.ch/vocabs/bbp/neurosciencegraph/core/v0.1.0/"
    }
    query = KGQuery(ModelProject, query_filter, context)
    return query.resolve(client)
def lookup_test_script(test_script_old_uuid, client):
    query_filter = {
        "path": "nsg:providerId",
        "op": "eq",
        "value": test_script_old_uuid
    }
    context = {
        "nsg":
        "https://bbp-nexus.epfl.ch/vocabs/bbp/neurosciencegraph/core/v0.1.0/",
        "oldUUID": "nsg:providerId"
    }
    query = KGQuery(ValidationScript, query_filter, context)
    return query.resolve(client)
async def query_results_extended(
        passed: List[bool] = Query(None),
        project_id: List[int] = Query(None),
        model_instance_id: List[UUID] = Query(
            None),  # todo: rename this 'model_instance_id' for consistency
        test_instance_id: List[UUID] = Query(None),
        model_id: List[UUID] = Query(None),
        test_id: List[UUID] = Query(None),
        model_alias: List[str] = Query(None),
        test_alias: List[str] = Query(None),
        score_type: List[ScoreType] = None,
        size: int = Query(100),
        from_index: int = Query(0),
        # from header
        token: HTTPAuthorizationCredentials = Depends(auth),
):
    filter_query, context = build_result_filters(
        model_instance_id,
        test_instance_id,
        model_id,
        test_id,
        model_alias,
        test_alias,
        score_type,
        passed,
        project_id,
        kg_client,
    )
    if len(filter_query["value"]) > 0:
        logger.info(
            f"Searching for ValidationResult with the following query: {filter_query}"
        )
        # note that from_index is not currently supported by KGQuery.resolve
        query = KGQuery(ValidationResultKG, {"nexus": filter_query}, context)
        results = query.resolve(kg_client, api="nexus", size=size)
    else:
        results = ValidationResultKG.list(kg_client,
                                          api="nexus",
                                          size=size,
                                          from_index=from_index)
    response = []
    for result in results:
        try:
            obj = await ValidationResultWithTestAndModel.from_kg_object(
                result, kg_client, token)
        except ConsistencyError as err:  # todo: count these and report them in the response
            logger.warning(str(err))
        else:
            response.append(obj)
    return response
Ejemplo n.º 4
0
def get_uniminds_person_list(neuroshapes_person_list, client):
    people = []
    for ns_person in as_list(neuroshapes_person_list):
        ns_person = ns_person.resolve(client)
        filter = {
            "op": "and",
            "value": [
                {
                    "path": "schema:familyName",
                    "op": "eq",
                    "value": ns_person.family_name
                },
                {
                    "path": "schema:givenName",
                    "op": "eq",
                    "value": ns_person.given_name
                }
            ]
        }
        context = {"schema": "http://schema.org/"}
        u_person = KGQuery(uPerson, filter, context).resolve(client)
        if u_person:
            people.append(u_person)
        else:
            #raise Exception("cannot find {}".format(ns_person))
            print("Warning: cannot find {}".format(ns_person))
            people = []
    return people
def lookup_model_project(model_name, date_created, client):
    context = {"schema": "http://schema.org/"}
    query_filter = {
        "op":
        "and",
        "value": [{
            "path": "schema:name",
            "op": "eq",
            "value": model_name
        }, {
            "path": "schema:dateCreated",
            "op": "eq",
            "value": date_created.isoformat()
        }]
    }
    query = KGQuery(ModelProject, query_filter, context)
    #try:
    return query.resolve(client)
def _query_results(passed, project_id, model_instance_id, test_instance_id,
                   model_id, test_id, model_alias, test_alias, score_type,
                   size, from_index, token):
    filter_query, context = build_result_filters(
        model_instance_id,
        test_instance_id,
        model_id,
        test_id,
        model_alias,
        test_alias,
        score_type,
        passed,
        project_id,
        kg_client,
    )
    if len(filter_query["value"]) > 0:
        logger.info(
            f"Searching for ValidationResult with the following query: {filter_query}"
        )
        # note that from_index is not currently supported by KGQuery.resolve
        query = KGQuery(ValidationResultKG, {"nexus": filter_query}, context)
        results = query.resolve(kg_client, api="nexus", size=size)
    else:
        results = ValidationResultKG.list(kg_client,
                                          api="nexus",
                                          size=size,
                                          from_index=from_index)
    response = []
    for result in results:
        try:
            obj = ValidationResult.from_kg_object(result, kg_client)
        except ConsistencyError as err:  # todo: count these and report them in the response
            logger.warning(str(err))
        else:
            response.append(obj)
    return response
def _are_test_code_editable_kg(testcode_json, KG_client):
    """
    Check if tests code are editable (are not associated with any results)
    :param testcode_json: datas of test code
    :type testcode_json: dict
    :returns: response
    :rtype: boolean
    """
    filter_query = {
        "path": "prov:used",
        "op": "eq",
        "value": testcode_json["uri"]
    }
    context = {"prov": "http://www.w3.org/ns/prov#"}
    return not bool(KGQuery(ValidationActivity, filter_query, context).resolve(KG_client))
def lookup_model_instance(model_instance_old_uuid, client):
    query_filter = {
        "path": "nsg:providerId",
        "op": "eq",
        "value": model_instance_old_uuid
    }
    context = {
        "nsg":
        "https://bbp-nexus.epfl.ch/vocabs/bbp/neurosciencegraph/core/v0.1.0/",
        "oldUUID": "nsg:providerId"
    }
    query = KGQuery(ModelInstance, query_filter, context)
    result = query.resolve(client)
    if not (result):
        query = KGQuery(MEModel, query_filter, context)
        result = query.resolve(client)
    return result
Ejemplo n.º 9
0
def query_tests(
        alias: List[str] = Query(None),
        id: List[UUID] = Query(None),
        name: List[str] = Query(None),
        implementation_status: str = Query(None),
        brain_region: List[BrainRegion] = Query(None),
        species: List[Species] = Query(None),
        cell_type: List[CellType] = Query(None),
        data_type: List[str] = Query(None),
        recording_modality: List[RecordingModality] = Query(None),
        test_type: List[ValidationTestType] = Query(None),
        score_type: List[ScoreType] = Query(None),
        author: List[str] = Query(None),
        size: int = Query(100),
        from_index: int = Query(0),
        summary: bool = Query(
            False,
            description=
            "Return only summary information about each validation test"),
        # from header
        token: HTTPAuthorizationCredentials = Depends(auth),
):

    # get the values of of the Enums
    if brain_region:
        brain_region = [item.value for item in brain_region]
    if species:
        species = [item.value for item in species]
    if cell_type:
        cell_type = [item.value for item in cell_type]
    if recording_modality:
        recording_modality = [item.value for item in recording_modality]
    if test_type:
        test_type = [item.value for item in test_type]
    if score_type:
        score_type = [item.value for item in score_type]

    filter_query, context = build_validation_test_filters(
        alias,
        id,
        name,
        implementation_status,
        brain_region,
        species,
        cell_type,
        data_type,
        recording_modality,
        test_type,
        score_type,
        author,
    )
    if len(filter_query["value"]) > 0:
        logger.info(
            f"Searching for ValidationTestDefinition with the following query: {filter_query}"
        )
        # note that from_index is not currently supported by KGQuery.resolve
        query = KGQuery(ValidationTestDefinition, {"nexus": filter_query},
                        context)
        test_definitions = query.resolve(kg_client, api="nexus", size=size)
    else:
        test_definitions = ValidationTestDefinition.list(kg_client,
                                                         api="nexus",
                                                         size=size,
                                                         from_index=from_index)
    if summary:
        cls = ValidationTestSummary
    else:
        cls = ValidationTest

    return [
        cls.from_kg_object(test_definition, kg_client)
        for test_definition in as_list(test_definitions)
    ]
Ejemplo n.º 10
0
def test_all():
    ## Search based on brain region

    cells_in_ca1 = PatchedCell.list(
        client, brain_region=BrainRegion("hippocampus CA1"))
    pprint([cell.brain_location for cell in cells_in_ca1])

    ### Download the recorded data for one of these cells

    example_cell = cells_in_ca1[3]
    experiment = example_cell.experiments.resolve(client)
    trace = experiment.traces.resolve(client)
    print(trace.time_step)
    print(trace.data_unit)

    download_url = trace.data_location.location
    print(download_url)

    data = np.genfromtxt(BytesIO(requests.get(download_url).content))

    ### Plot the data

    times = data[:, 0]
    signals = data[:, 1:]

    # plot the first 100 signals
    plt.figure(figsize=(18, 16), dpi=80, facecolor='w', edgecolor='k')
    xlim = (times.min(), times.max())
    for i in range(1, 101):
        plt.subplot(10, 10, i)
        plt.plot(times, data[:, i], 'grey')
        plt.xlim(*xlim)
        plt.axis('off')

    ## Search based on species

    cells_from_mouse = PatchedCell.list(client,
                                        species=Species("Mus musculus"))
    for cell in cells_from_mouse[:10]:
        pprint(
            cell.collection.resolve(client).slice.resolve(
                client).slice.resolve(client).subject.resolve(client).species)

    ## Search based on cell type

    pyramidal_neurons = PatchedCell.list(
        client, cell_type=CellType("hippocampus CA1 pyramidal cell"))
    pprint([pn.cell_type for pn in pyramidal_neurons[:10]])

    # An activity dataset with minimal metadata

    Dataset.set_strict_mode(False)

    query = {
        "path":
        "minds:specimen_group / minds:subjects / minds:samples / minds:methods / schema:name",
        "op":
        "in",
        "value": [
            "Electrophysiology recording", "Voltage clamp recording",
            "Single electrode recording",
            "functional magnetic resonance imaging"
        ]
    }
    context = {
        "schema": "http://schema.org/",
        "minds": "https://schema.hbp.eu/minds/"
    }

    activity_datasets = KGQuery(Dataset, query, context).resolve(client)
    for dataset in activity_datasets:
        print("* " + getattr(dataset, "name", "unknown"))

    dataset = activity_datasets[-1]
    #dataset.owners[0].resolve(client)
    #dataset.license.resolve(client)

    ## An activity dataset with extended metadata

    dataset = client.by_name(
        Dataset,
        "sIPSCs from juvenile (P21-30) C57Bl6/J male mice from CA1 pyramidal neurons receiving input from PV+ interneurons"
    )
    query = {"path": "nsg:partOf", "op": "eq", "value": dataset.id}
    context = {
        "nsg":
        "https://bbp-nexus.epfl.ch/vocabs/bbp/neurosciencegraph/core/v0.1.0/"
    }
    traces = KGQuery(Trace, query, context).resolve(client)
    print(traces)

    tr0 = traces[0]

    print(tr0.name)
    print(tr0.channel)
    print(tr0.time_step)
    print(tr0.data_unit)
    print(tr0.data_location)

    experiment = tr0.generated_by.resolve(client)
    print(experiment.name)

    cell = experiment.recorded_cell.resolve(client)

    print(cell.brain_location)
    print(cell.cell_type)
    print(cell.reversal_potential_cl)
Ejemplo n.º 11
0
async def query_models(
        alias: List[str] = Query(
            None,
            description="A list of model aliases (short names) to search for"),
        id: List[UUID] = Query(
            None, description="A list of specific model IDs to search for"),
        name: List[str] = Query(None,
                                description="Model name(s) to search for"),
        brain_region: List[BrainRegion] = Query(
            None,
            description=
            "Find models intended to represent this/these brain region(s)"),
        species: List[Species] = Query(
            None,
            description="Find models intended to represent this/these species"
        ),
        cell_type: List[CellType] = Query(
            None, description="Find models of this/these cell type(s)"),
        model_scope: ModelScope = Query(
            None, description="Find models with a certain scope"),
        abstraction_level: AbstractionLevel = Query(
            None, description="Find models with a certain abstraction level"),
        author: List[str] = Query(
            None, description="Find models by author (family name)"),
        owner: List[str] = Query(
            None, description="Find models by owner (family name)"),
        organization: List[str] = Query(
            None, description="Find models by organization"),
        project_id: List[str] = Query(
            None,
            description="Find models belonging to a specific project/projects"
        ),
        private: bool = Query(
            None, description="Limit the search to public or private models"),
        summary: bool = Query(
            False,
            description="Return only summary information about each model"),
        size: int = Query(100, description="Maximum number of responses"),
        from_index: int = Query(
            0, description="Index of the first response returned"),
        # from header
        token: HTTPAuthorizationCredentials = Depends(auth),
):
    """
    Search the model catalog for specific models (identitified by their unique ID or by a short name / alias),
    and/or search by attributes of the models (e.g. the cell types being modelled, the type of model, the model author).
    """

    # If project_id is provided:
    #     - private = None: both public and private models from that project (collab), if the user is a member
    #     - private = True: only private models from that project, if that user is a member
    #     - private = False: only public models from that project
    # If project_id is not provided:
    #     - private = None: only public models, from all projects
    #     - private = True: 400? error "To see private models, you must specify the project/collab"
    #     - private = False: only public models, from all projects

    if private:
        if project_id:
            for collab_id in project_id:
                if not await is_collab_member(collab_id, token.credentials):
                    raise HTTPException(
                        status_code=status.HTTP_403_FORBIDDEN,
                        detail="You are not a member of project #{collab_id}",
                    )
        else:
            raise HTTPException(
                status_code=status.HTTP_400_BAD_REQUEST,
                detail=
                "To see private models, you must specify the project/collab id",
            )

    # get the values of of the Enums
    if brain_region:
        brain_region = [item.value for item in brain_region]
    if species:
        species = [item.value for item in species]
    if cell_type:
        cell_type = [item.value for item in cell_type]
    if model_scope:
        model_scope = model_scope.value
    if abstraction_level:
        abstraction_level = abstraction_level.value

    filter_query, context = build_model_project_filters(
        alias,
        id,
        name,
        brain_region,
        species,
        cell_type,
        model_scope,
        abstraction_level,
        author,
        owner,
        organization,
        project_id,
        private,
    )
    if len(filter_query["value"]) > 0:
        logger.info(
            "Searching for ModelProject with the following query: {}".format(
                filter_query))
        # note that from_index is not currently supported by KGQuery.resolve
        model_projects = KGQuery(ModelProject, {
            "nexus": filter_query
        }, context).resolve(kg_client, api="nexus", size=size)
    else:
        model_projects = ModelProject.list(kg_client,
                                           api="nexus",
                                           size=size,
                                           from_index=from_index)
    if summary:
        cls = ScientificModelSummary
    else:
        cls = ScientificModel
    return [
        cls.from_kg_object(model_project, kg_client)
        for model_project in as_list(model_projects)
    ]