def test_from_xml_multi_form(self):
     data = dict(self.make_mgmt_data(2).items() + self.make_sub_form_data(0).items() + self.make_sub_form_data(1).items())
     form = Form(self.uih, data, prefix=self.prefix)
     self.assertTrue(form.is_valid())
     root = create_root('tao', xmlns='http://tao.asvo.org.au/schema/module-parameters-v1')
     work = child_element(root, 'workflow')
     child_element(work, 'schema-version', '2.0')
     form.to_xml(work)
     root = xml_parse(xml_print(root)) ## do this to recover namespace info not explicit above
     form = Form.from_xml(self.uih, root, self.prefix)
     self.assertTrue(form.is_valid())
     self.assertEqual(form.total_form_count(), 2, msg='Must create extra form.')
def to_xml_2(form, root):
   from tao.xml_util import find_or_create, child_element

   selected_type, selected_filter = form.cleaned_data['filter'].split('-')
   if selected_filter == NO_FILTER:
       return

   filter_parameter = None
   filter_type = ''
   units = ''
   if selected_type == 'D':
       filter_parameter = DataSetProperty.objects.get(pk=selected_filter)
       filter_type = filter_parameter.name
       units = filter_parameter.units
   elif selected_type == 'B':
       selected_filter, selected_extension = selected_filter.split('_')
       filter_parameter = datasets.band_pass_filter(selected_filter)
       filter_type = str(filter_parameter.filter_id) + '_' + selected_extension
       units = 'bpunits'

   rf_elem = find_or_create(root, 'record-filter')
   child_element(rf_elem, 'module-version', text=RecordFilterForm.MODULE_VERSION)
   filter_elem = find_or_create(rf_elem, 'filter')
   child_element(filter_elem, 'filter-attribute', filter_type)
   filter_min = form.cleaned_data['min']
   filter_max = form.cleaned_data['max']
   default_filter = form.ui_holder.dataset.default_filter_field
   if default_filter is not None and filter_parameter.id == default_filter.id and filter_min is None and filter_max is None:
       filter_min = form.ui_holder.dataset.default_filter_min
       filter_max = form.ui_holder.dataset.default_filter_max
   child_element(filter_elem, 'filter-min', text=str(filter_min), units=units)
   child_element(filter_elem, 'filter-max', text=str(filter_max), units=units)
def to_xml_2(form, root):
   from tao.xml_util import find_or_create, child_element

   # Hunt down the full item from the list of output formats.
   fmt = form.cleaned_data['supported_formats']
   ext = ''
   for x in configured_output_formats():
       if x['value'] == fmt:
           ext = '.' + x['extension']
           break

   # The output file should be a CSV, by default.
   of_elem = find_or_create(root, fmt, id=FormsGraph.OUTPUT_ID)
   child_element(of_elem, 'module-version', text=OutputFormatForm.MODULE_VERSION)
   child_element(of_elem, 'filename', text='tao.output' + ext)
Esempio n. 4
0
def _make_parameters(user, forms):
    # precondition: forms are valid

    root = create_root('tao', timestamp=time.timestamp(), nsmap={None:NAMESPACE})

    child_element(root, 'username', text=user.username)

    workflow = find_or_create(root, 'workflow', name='alpha-light-cone-image')
    child_element(workflow, 'schema-version', text='2.0')

    for form in forms:
        form.to_xml(workflow)

    child_element(root, 'signature', text='base64encodedsignature')

    return xml_print(root)
Esempio n. 5
0
def to_xml_2(form, root):
    if form.apply_mock_image:
        from tao.xml_util import find_or_create, child_element

        # Prepare the base element.
        mi_elem = find_or_create(root, 'skymaker', id=FormsGraph.MOCK_IMAGE_ID)
        child_element(mi_elem, 'module-version', text=Form.MODULE_VERSION)
        child_element(child_element(mi_elem, 'parents'), 'item', text=FormsGraph.BANDPASS_FILTER_ID)

        # Create a list element for the images.
        list_elem = child_element(mi_elem, 'images')

        # Iterate over the forms, creating entries. Remember there will always
        # be one extra empty form, so don't include it.
        assert form.total_form_count() > 0, 'Internal error, this should never happen!'
        for ii, sub in enumerate(form):
            sub_elem = child_element(list_elem, 'item')
            for field, val in sub.cleaned_data.iteritems():
                if field[-9:] == 'mag_field':
                    item_id, item_extension = val.split('_')
                    op = datasets.band_pass_filter(item_id)
                    child_element(sub_elem, field, op.filter_id + '_' + item_extension)
                else:
                    child_element(sub_elem, field, str(val))
Esempio n. 6
0
def to_xml_2(form, root):
    apply_sed = form.cleaned_data.get('apply_sed')
    output_format = form.ui_holder.cleaned_data('output_format', 'supported_formats')

    if apply_sed:
        from tao.xml_util import find_or_create, child_element, add_encoding

        sed_elem = find_or_create(root, 'sed', id=FormsGraph.SED_ID)
        child_element(sed_elem, 'module-version', text=Form.MODULE_VERSION)

        # Add a hard-coded connection to the light-cone and the CSV output.
        elem = child_element(sed_elem, 'parents')
        child_element(elem, 'item', text=FormsGraph.LIGHT_CONE_ID)

        child_element(child_element(find_or_create(root, output_format, id=FormsGraph.OUTPUT_ID), 'parents'), 'item', text=FormsGraph.BANDPASS_FILTER_ID)

        single_stellar_population_model = tao_models.StellarModel.objects.get(
            pk=form.cleaned_data['single_stellar_population_model'])
        add_encoding(sed_elem, single_stellar_population_model.encoding)

        # Create an independant filter module.
        filter_elem = find_or_create(root, 'filter', id=FormsGraph.BANDPASS_FILTER_ID)
        child_element(filter_elem, 'module-version', text=Form.MODULE_VERSION)

        apply_dust = form.cleaned_data['apply_dust']
        if apply_dust:
            dust_elem = find_or_create(root, 'dust', id=FormsGraph.DUST_ID)
            child_element(dust_elem, 'module-version', text=Form.MODULE_VERSION)
            child_element(child_element(dust_elem, 'parents'), 'item', text=FormsGraph.SED_ID)
            selected_dust_model = tao_models.DustModel.objects.get(pk=form.cleaned_data['select_dust_model'])
            child_element(dust_elem, 'model', text=selected_dust_model.name)
            # Parent of the dust module is either the SED module or, if selected, the dust module
            child_element(child_element(filter_elem, 'parents'), 'item', text=FormsGraph.DUST_ID)
        else:
            child_element(child_element(filter_elem, 'parents'), 'item', text=FormsGraph.SED_ID)

        # Find the CSV output element or create it, and get access to
        # the fields tag.
        fields_elem = find_or_create(find_or_create(root, output_format, id=FormsGraph.OUTPUT_ID), 'fields')

        band_pass_filters = form.cleaned_data['band_pass_filters']
        if len(band_pass_filters) > 0:
            bf_elem = child_element(filter_elem, 'bandpass-filters')
            added = {}
            selected = {}
            for item in band_pass_filters:
                item_id, item_extension = item.split('_')
                if item_id not in selected: selected[item_id] = []
                selected[item_id].append(item_extension)
            for item in band_pass_filters:
                item_id, item_extension = item.split('_')
                op = datasets.band_pass_filter(item_id)
                if item_id not in added:
                    child_element(bf_elem, 'item', text=op.filter_id, label=op.label, description=op.description, selected=",".join(selected[item_id]))
                    added[item_id] = True
                child_element(fields_elem, 'item', text=op.filter_id + '_' + item_extension, label=op.label + ' (' + item_extension.capitalize() + ')')

    else:
        from tao.xml_util import find_or_create, child_element

        # No SED module, connect the output to the light-cone module.
        child_element(child_element(find_or_create(root, output_format, id=FormsGraph.OUTPUT_ID), 'parents'), 'item', text=FormsGraph.LIGHT_CONE_ID)
Esempio n. 7
0
def to_xml_2(form, root):
    from tao.xml_util import find_or_create, child_element

    light_cone_elem = find_or_create(root, 'light-cone', id=FormsGraph.LIGHT_CONE_ID)

    simulation = tao_models.Simulation.objects.get(pk=form.cleaned_data['dark_matter_simulation'])
    galaxy_model = tao_models.GalaxyModel.objects.get(id=form.cleaned_data['galaxy_model'])

    child_element(light_cone_elem, 'module-version', text=Form.MODULE_VERSION)
    child_element(light_cone_elem, 'geometry', text=form.cleaned_data['catalogue_geometry'])
    child_element(light_cone_elem, 'simulation', text=simulation.name)
    child_element(light_cone_elem, 'galaxy-model', text=galaxy_model.name)

    if form.cleaned_data['catalogue_geometry'] == Form.BOX:

        snapshot = tao_models.Snapshot.objects.get(id=form.cleaned_data['snapshot'])
        child_element(light_cone_elem, 'redshift', text=snapshot.redshift)
        box_size = form.cleaned_data['box_size']
        if box_size is None or box_size == '':
            box_size = simulation.box_size
        child_element(light_cone_elem, 'query-box-size', text=box_size, units='Mpc')
        child_element(light_cone_elem, 'rng-seed', text=form.cleaned_data['rng_seed'])

    else:  # == Form.CONE

        child_element(light_cone_elem, 'box-repetition', text=form.cleaned_data['light_cone_type'])
        child_element(light_cone_elem, 'num-cones', text=form.cleaned_data['number_of_light_cones'])
        child_element(light_cone_elem, 'redshift-min', text=form.cleaned_data['redshift_min'])
        child_element(light_cone_elem, 'redshift-max', text=form.cleaned_data['redshift_max'])
        child_element(light_cone_elem, 'ra-min', text='0.0', units='deg')
        child_element(light_cone_elem, 'ra-max', text=form.cleaned_data['ra_opening_angle'], units='deg')
        child_element(light_cone_elem, 'dec-min', text='0.0', units='deg')
        child_element(light_cone_elem, 'dec-max', text=form.cleaned_data['dec_opening_angle'], units='deg')
        if form.cleaned_data['light_cone_type'] == 'random':
            rng_seeds_string = form.cleaned_data['rng_seeds']
            rng_seeds = re.sub(r"[\[\]\su']", '', rng_seeds_string).split(',')
            rng_seeds = [r for r in rng_seeds if r] # removes empty strings
            if len(rng_seeds) > 0:
                rng_seeds_elem = child_element(light_cone_elem, 'rng-seeds')
                i = 0
                for rng_seed in rng_seeds:
                    child_element(rng_seeds_elem, 'rng-seed-%d' % i, text=rng_seed)
                    i += 1

    output_properties = form.cleaned_data['output_properties']
    if len(output_properties) > 0:

        # Create the light-cone output properties.
        output_elem = child_element(light_cone_elem, 'output-fields')

        # Either create or find the CSV/HDF5 output properties.
        output_format = form.ui_holder.cleaned_data('output_format', 'supported_formats')
        fields_elem = find_or_create(find_or_create(root, output_format, id=FormsGraph.OUTPUT_ID), 'fields')

        # Insert entries.
        for item in output_properties:
            op = datasets.output_property(item)
            attrs = {'label': op.label}
            if op.units is not None and len(op.units) > 0: attrs['units'] = op.units
            child_element(fields_elem, 'item', text=op.name, **attrs)
            attrs.update({'description': op.description})
            if not op.is_computed:
                child_element(output_elem, 'item', text=op.name, **attrs)