Beispiel #1
0
def check_if_alias_is_already_taken(name):

    print('comparing with KG database [...]')
    client = KGClient(token,
                      nexus_endpoint="https://nexus.humanbrainproject.org/v0")
    MODELS = ModelProject.list(client, size=10000)
    NAMES = [project.name for project in MODELS]
    if name in NAMES:
        i0 = int(np.argwhere(np.array(NAMES, dtype=str) == name))
        if type(MODELS[i0].authors) is list:
            author_list = ''
            for nn in list(MODELS[i0].authors):
                author_list += nn.resolve(client).full_name + '; '
        else:
            author_list = MODELS[i0].authors.resolve(client).full_name
        print('/!\ --> The alias "%s"' % name,
              'is redundant with the following entry:')
        print(''' 
- Description:
        %s
- Author(s):
        %s
        ''' % (MODELS[i0].resolve(client).description[:200]+'  [...]  '+\
               MODELS[i0].resolve(client).description[-200:],
               author_list))
    else:
        print('--> The alias "%s"' % name, 'is valid')
 def test_list_kgquery(self, kg_client):
     models = ModelProject.list(kg_client,
                                api="query",
                                scope="latest",
                                size=10,
                                resolved=True)
     assert len(models) == 10
 def test_list_kgquery_with_filter(self, kg_client):
     models = ModelProject.list(kg_client,
                                api="query",
                                scope="latest",
                                size=10,
                                resolved=True,
                                species="Rattus norvegicus")
     assert len(models) == 2
 def test_list_nexus_with_filter(self, kg_client):
     models = ModelProject.list(kg_client,
                                api="nexus",
                                size=10,
                                species="Rattus norvegicus")
     assert len(models) == 6
Beispiel #5
0
        assert len(name) > 6

        bundle = FileBundle(identifier=hashlib.md5(script.code_location.encode('utf-8')).hexdigest(),
                            name=name,
                            description=model_instance.description,
                            url=script.code_location,
                            #usageNotes,
                            modelInstance=model_release)  # check this is showing in KG editor
        print(bundle)
        bundle.save(client)


if __name__ == "__main__":
    client = KGClient(token, nexus_endpoint="https://nexus.humanbrainproject.org/v0")

    projects = ModelProject.list(client, size=10000)
    single_cell_projects = [p for p in projects if p.model_of and p.model_of.label == "single cell" and "emodel" not in p.name]
    single_cell_no_memodel = [p for p in single_cell_projects if "CA1_pyr" not in p.name and "CA1_int" not in p.name]

    #for model_project in ModelProject.list(client, size=10000):
    for model_project in single_cell_no_memodel:
        #if model_project.model_of and "network" in model_project.model_of.label:
        #if model_project.model_of and "single cell" not in model_project.model_of.label:
        if True:
            contributors = get_uniminds_person_list(model_project.authors, client)
            custodians = get_uniminds_person_list(model_project.owners, client)

            for model_instance in as_list(model_project.instances):
                model_instance = model_instance.resolve(client)
                script = model_instance.main_script.resolve(client)
                release = create_or_update_model_release(model_project, model_instance, contributors, custodians, script, client)
Beispiel #6
0
def load_model_instances(show_ignore=False,
                         size=100000,
                         scope='inferred',
                         verbose=False,
                         api='nexus'):

    client = KGClient(os.environ["HBP_token"])
    
    MODEL_INSTANCES = []

    models = ModelProject.list(client,
                               api=api,
                               scope=scope,
                               size=size)
        
    for model in models:

        if verbose:
            print('\n##########################################')
            print('------ %s ------' % model.name)
        minst = template.copy()
        minst['name'] = model.name

        minst['owner'] = get_author_details(model.owners.resolve(client))
        if type(model.authors) is list:
            minst['author(s)'] = [get_author_details(auth.resolve(client)) for auth in model.authors]
        else:
            minst['author(s)'] = [get_author_details(model.authors.resolve(client))]

        try:
            minst['authors_str'] = model.authors_str(client)
        except TypeError:
            minst['authors_str'] = ''
            for a in minst['author(s)']:
                minst['authors_str'] += a['family_name']+', '+a['given_name']+';' 

        minst['description'] = model.description
        minst['private'] = model.private
        minst['collab_id'] = model.collab_id
        minst['alias'] = model.alias

        for key, quant in zip(['brain_region', 'species', 'cell_type', 'abstraction_level', 'model_scope'],
                              [model.brain_region, model.species, model.celltype, model.abstraction_level, model.model_of]):
            if quant is not None:
                minst[key] = (quant.label, '')
            elif verbose:
                print('[!!] "%s" missing for %s ' % (key, model.name))
        for key, quant in zip(['organization'],
                              [model.organization]):
            if quant is not None:
                minst[key] = (quant.resolve(client).name, '')
            elif verbose:
                print('[!!] "%s" missing for %s ' % (key, model.name))

        if type(model.instances) is list:
            for modelI in model.instances:
                MODEL_INSTANCES.append(minst)
                # pprint.pprint(MODEL_INSTANCES[-1])
                # print('')
                add_version_details_to_model(MODEL_INSTANCES[-1], modelI.resolve(client), client, verbose=verbose)
        elif type(model.instances) is KGProxy:
            modelI = model.instances
            MODEL_INSTANCES.append(minst.copy())
            add_version_details_to_model(MODEL_INSTANCES[-1], modelI.resolve(client), client, verbose=verbose)
        elif show_ignore:
            print('Ignoring %s @ %s' % (model.name, model.date_created))
            pass # we don't care about models without specific version

        
    DATES = np.array([time.mktime(minst['date_created'].timetuple()) for minst in MODEL_INSTANCES])

    return [MODEL_INSTANCES[i] for i in np.argsort(DATES)[::-1]]
Beispiel #7
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)
    ]
Beispiel #8
0
def test_all():
    ModelProject.set_strict_mode(False)  # check: will this interfere with other tests?
    MEModel.set_strict_mode(False)

    ## Get a list of possible filter terms

    for cls in (Species, CellType, BrainRegion, AbstractionLevel, ModelScope):
        print("\nPossible values of {}:\n  - ".format(cls.__name__))
        print("\n  - ".join(cls.iri_map.keys()))

    ## Find models of hippocampus pyramidal neurons

    models = ModelProject.list(client,
                            brain_region="hippocampus",
                            model_of="single cell",
                            species="Rattus norvegicus")
    len(models)
    columns = ("name", "brain_region", "celltype", "authors")

    data = []
    for model in models:
        row = {}
        for attr in columns:
            value = getattr(model, attr)
            if hasattr(value, "label"):
                row[attr] = value.label
            elif attr == "authors":
                row[attr] = ", ".join("{au.given_name} {au.family_name}".format(au=obj.resolve(client))
                                    for obj in as_list(value))
            else:
                row[attr] = value
        data.append(row)

    df = pd.DataFrame(data=data, columns=columns)  #.sort_values(by=['name'], kind="mergesort")
    dfStyler = df.style.set_properties(**{'text-align': 'left'})
    dfStyler.set_table_styles([dict(selector='th', props=[('text-align', 'left')])])


    # Exploring a single model

    # take the last model from the list above, and show its versions
    example_model = models[-1]
    example_model.instances

    # take the last version (not necessarily the most recent)
    instance = as_list(example_model.instances)[-1].resolve(client)
    instance
    instance.timestamp.isoformat()

    code = instance.main_script.resolve(client)
    code.code_format
    code.code_location

    morph = instance.morphology.resolve(client)
    morph.morphology_file

    def print_model_information(index):
        model = models[index]
        instance = as_list(model.instances)[-1].resolve(client)
        code = instance.main_script.resolve(client)
        morph = instance.morphology.resolve(client)
        print(instance.name, instance.timestamp.isoformat())
        print(code.code_location)
        print(morph.morphology_file)

    print_model_information(99)

    # not all models have morphologies, I'm not sure if this is expected or not.
    # Here is a list of models with morphologies
    for model in models:
        for instance in as_list(model.instances):
            instance = instance.resolve(client)
            morph_file = instance.morphology.resolve(client).morphology_file
            if morph_file:
                print(instance.name, morph_file)