Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add/improve easy querying functionality #104

Closed
aiida-bot opened this issue Oct 17, 2014 · 3 comments
Closed

Add/improve easy querying functionality #104

aiida-bot opened this issue Oct 17, 2014 · 3 comments

Comments

@aiida-bot
Copy link

Originally reported by: Giovanni Pizzi (Bitbucket: pizzi, GitHub: giovannipizzi)


Create wrapper functions (and maybe even a cmdline interface, partially already existing) to easily query attributes.

E.g., to query calculations done on a structure with a given element and at least num_elems_min, and containing a given string in the label:

#!python
def get_calcs(num_elems_min, element, label_string):
    from aiida.orm import DataFactory, Calculation
    from django.db.models import Count
    # Find attributes referring to the given element
    q1 = models.DbAttribute.objects.filter(key__startswith='sites.',key__endswith='.kind_name', tval=element)
    # Find structures with those attributes
    q2 = DataFactory('structure').query(dbattributes__in=q1)
    # Annotate the number of attributes (of the specified type) 
    q3 = q2.annotate(num_elems=Count('dbattributes'))
    # filter only those with at least the specified # of elements
    q4 = q3.filter(num_elems__gte=num_elems_min)
    # Query calculations using as input the specified structures
    q5 = Calculation.query(label__contains=label_string, inputs__in=q4)
    # return a Django QuerySet with the resulting calculations
    return q5.distinct()

print "I found %s entries." % get_calcs(num_elems_min=2, element='Fe', label_string='MD').count()


@aiida-bot
Copy link
Author

Original comment by Giovanni Pizzi (Bitbucket: pizzi, GitHub: giovannipizzi):


Committed first version of querytool in 57abb61
Example of usage:

#!python
#!/usr/bin/env runaiida

from aiida.orm.querytool import QueryTool

Ph = CalculationFactory('quantumespresso.ph')
Pw = CalculationFactory('quantumespresso.pw')
ParameterData = DataFactory('parameter')
q = QueryTool()
q.set_class(Ph)
q.add_attr_filter('state', '=', 'FINISHED')
q.add_attr_filter('INPUTPH.nq1', '>=', 4, relnode='inp.parameters')

print [_.pk for _ in q.run_query()]

q = QueryTool()
q.set_class(ParameterData)
q.add_attr_filter('INPUTPH.nq1', '>=', 4)
q.add_attr_filter('state', '=', 'FINISHED', relnode='out.parameters', relnodeclass=Ph)

print [_.pk for _ in q.run_query()]

Still to do:

  • document it
  • add to the todo what is missing/should be implemented (it's in the docstrings of the QueryTool class)
  • add the QueryTool to the things automatically imported by verdi run/shell

@aiida-bot
Copy link
Author

Original comment by Giovanni Pizzi (Bitbucket: pizzi, GitHub: giovannipizzi):


Duplicate of #178.

@aiida-bot
Copy link
Author

Original comment by Giovanni Pizzi (Bitbucket: pizzi, GitHub: giovannipizzi):


This is now addressed with the QueryBuilder. The QueryTool will be deprecated.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

1 participant