Beispiel #1
0
    def select_and_verify_parameters(self, order=2, **props):
        """
        A wrapper to the select_parameters method.
        This method can deal with overlaps of grids.
        But since it does not know the grid apriori,
        it is unable to recognize wrong result.

        This wrapper checks the result.

        input:
          order.. maximal number of interpolated spectra
          props.. dictionary of interpolated parameters
        output:
          parlist.. each row represents one spectrum
                which is needed to interpolate in
                give props
          vals.. values in which we interpolate
          keys.. names of the interpolated
        """

        # all parameters are defined lowercase
        # so we have to convert it
        for key in props.keys():
            v = props.pop(key)
            props[key.lower()] = v

        if self.debug:
            print "In select_and_verify_parameters: order=%i properties:" % (order)
            print str(props)

        # keys and values
        keys = props.keys()
        vals = [props[key] for key in props.keys()]

        # gets the parameter list
        # print order, props
        parlist = self.select_parameters(order=order,**props)
        # print parlist

        # deselect reduntdant spectra
        parlist = self.deselect_exact(parlist, **props)

        if len(parlist) == 0:
            raise Exception('Do %s lie within the grid? I do not think so...' % (str(props)))

        if self.debug:
            print 'Following parameters were chosen with select_parameters method:'
            for row in parlist:
                print row

        # checks the result
        temp = np.array(parlist)
        # print temp, vals
        for i, val in enumerate(vals):
            # print val,  temp[:, i], is_within_interval(val, temp[:, i])
            if not is_within_interval(val, temp[:, i]):
                raise ValueError('Parameters %s lie outside the grid.' % (str(props)))

        return parlist, vals, keys
Beispiel #2
0
    def select_parameters(self, values=[], order=2, constraints={}, **props):
        """
        Creates a final list - this is still
        first guess. I think that searching up
        eligible values and spectra can be done
        better.
        input:
          grid - synthetic spectraList, which is searched
          order - how many spectra are used this is
              adjusted dynamically if there are not
              enough values
          constraints - resolve conflicts between grids
          props - properties in which we fit
        output:
          values of spectra for interpolation
        """


        # extract the parameter and its values
        key = props.keys()[0].lower()
        v = props.pop(key)

        # print key, constraints, props
        # list eligible values for a given parameter
        elig_vals = np.array(self.get_available_values_fast(key, **constraints))
        # print key, elig_vals

        # sorts the grid, from nearest to farthest
        ind = np.argsort(abs(elig_vals - v))
        vals = elig_vals[ind]

        # equality check

        # print vals, v, key
        # what if the grid step is inhomogeneous? - actually it is
        # in z - what shall we do, what shall we do???
        if vals[:order].min() > v or vals[:order].max() < v:
            # TODO think of something better than this!!!!!!
            try:
                lower = np.max(vals[np.where(vals - v < ZERO_TOLERANCE)[0]])
                upper = np.min(vals[np.where(vals - v > ZERO_TOLERANCE)[0]])
                vals = np.array([lower, upper])
            except:
                pass
            # print lower, upper, vals


        # checks that there is not equality
        # if np.any(abs(vals - v) < ZERO_TOLERANCE):
        #     ind = np.argmin(abs(vals - v))
        #     vals = [vals[ind]]
        #
        #     if self.debug:
        #         print "%s=%s is precise. Skipping choice of parameters." % (key, str(v))

        # if the eligible values do not surround the parameter
        if not is_within_interval(v, vals):
            return values

        # if there are no other spectra to interpolate in
        if len(props.keys()) == 0:
            for i in range(0, len(vals)):
                row = []
                # append those that are already fixed
                for key in constraints.keys():
                    row.append(constraints[key])

                # append the last parameter
                row.append(vals[i])
                # append the row
                values.append(row)

                # once 'order' spectra are appended, we can
                # end
                if i == order - 1:
                    break
            return values
        else:
            j = 0
            for i in range(0, len(vals)):

                # add a constraint
                constraints[key] = vals[i]

                # recursively calls the function
                values_new = self.select_parameters(values=copy.deepcopy(values), order=order, constraints=constraints,
                                                    **props)

                # some searches are in vain - so we
                # wait until meaningful calls accumulate
                if len(values_new) > len(values):
                    j += 1

                # copis the result, so we can go on
                values = values_new

                # remove constraint
                constraints.pop(key)
                if j == order:
                    break
        return values