Commit ffd3aff9 authored by Hugo Buddelmeijer's avatar Hugo Buddelmeijer
Browse files

Example of a CVS based AttributeCalculator.

parent a0d94a9c
# -*- coding: utf-8 -*-
__version__ = '@(#)$Revision$'
"""
Script to experiment with using a file on the CVS with a derived
AttributeCalculator instance. See
http://redmine.hpc.rug.nl/redmine/issues/88
http://redmine.hpc.rug.nl/redmine/issues/142
The base AttributeCalculator SourceCollection class only provides a
framework to calculate new attributes from existing ones. Derived
classes should fill in the details, in particular:
- What new attributes are calculated,
- which existing attributes are needed for that, and
- the code with the calculation.
These derived AttributeCalculator classes subsequently have to be
registered in the database as an AttributeCalculatorDefinition
object. This allows the Target Processor to find them automatically
in order to determine how to calculate a requested attribute.
The derived classes can be made without requiring a new persistent
class because the base AttributeCalculator class already provides a
reusable framework. Therefore no database evolution is necessary to
define a new way to calculate attributes.
Creating such a derived class is done as follows:
1) Create a file for your new AttributeCalculator class:
MyNiceAttributeCalculator.py
2) Write your code, include the following:
2a) Import the base AttributeCalculator as BaseAttributeCalculator:
from astro.main.sourcecollection.AttributeCalculator import AttributeCalculator as BaseAttributeCalculator
2b) Create a new class called AttributeCalculator that inherits from
BaseAttributeCalculator:
class AttributeCalculator(BaseAttributeCalculator): pass # your code goes here
2c) Assign this new AttributeCalculator to your choosen name:
MyNiceAttributeCalculator = AttributeCalculator
2d) Register your new AttributeCalculator class:
MyNiceAttributeCalculator.register_definition()
3) Use your new AttributeCalculator:
3a) Import the class
from astro.experimental.MyNiceAttributeCalculator import MyNiceAttributeCalculator
ac = MyNiceAttributeCalculator()
ac.parent_collection = my_other_sourcecollection()
ac.commit()
The AttributeCalculatorDefinition will be committed the first time
that you commit an AttributeCalculator.
"""
from common.database.Context import context
from astro.main.sourcecollection.SourceCollection import SourceCollection
from astro.main.sourcecollection.AttributeCalculator import AttributeCalculatorDefinition
from astro.main.sourcecollection.ConcatenateAttributes import ConcatenateAttributes
# Import the base AttributeCalculator as BaseAttributeCalculator.
from astro.main.sourcecollection.AttributeCalculator import AttributeCalculator as BaseAttributeCalculator
# Inherit your new AttributeCalculator from this BaseAttributeCalculator.
class AttributeCalculator(BaseAttributeCalculator):
'''
Calculates test attribute CVStest.
'''
# The attributes that are calculated.
acd_attributes = [
{
# Test attribute
'name': 'CVStest',
'format': 'float32',
'ucd': '',
'null': None,
'length': 1,
},
]
# The .input_attribute_names of the ACD. These are the attributes that
# are necessary to calculate the new attributes. These attributes
# are recursively searched for when pulling data.
acd_input_attribute_names = [
'MAG_AUTO',
]
# The .acd_parameters are the processing parameters and dependencies
# that are necessary for the calculation. They are used for:
# - ACD.calculator_parameters, which will then be used for
# - AC.process_parameters
acd_parameters = [
{
'name': 'plus',
'format': 'float',
'value': 1.0,
'description': 'To add',
},
]
# The name of the AttributeCalculatorDefinition
acd_name = "CVStest calculator"
# calculate_attributes() is the function that is used to
# calculate the new attributes from the old attributes.
def calculate_attributes_vector(
self,
MAG_AUTO,
plus,
):
CVStest = MAG_AUTO * 0.1 + plus
data = {
'CVStest': CVStest
}
return data
# 'Rename' your AttributeCalculator to the desired name.
CVSBasedAttributeCalculator = AttributeCalculator
# Register your AttributeCalculator.
CVSBasedAttributeCalculator.register_definition()
if __name__ == '__main__':
# Set the project because our test SourceCollection is there.
# Note that an AttributeCalculatorDefinition object will be made
# when the AttributeCalculator is registered by importing the
# class for the first time. Therefore think about which project
# your AttributeCalculatorDefinition should go.
context.set_project("KIDS")
# Import the CVSBasedAttributeCalculator. Include the full path
# in the import, because this prevents circular imports (in a
# a rather ugly way..).
from astro.experimental.SourceCollection.CVSBasedAttributeCalculator import CVSBasedAttributeCalculator
# Testing it.
slw = (SourceCollection.SCID == 1115841)[0]
slw.load_data()
ac = CVSBasedAttributeCalculator()
ac.parent_collection = slw
ac.make()
ac.load_data()
ca = ConcatenateAttributes()
ca.parent_collections = [slw, ac]
ca.load_data()
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment