예제 #1
0
    def _add_grid_variable(self, variable):
        """
        Prepares to use another dependent variable to change from.

        Args: see VarChangingInterpolator._add_grid_variable.

        Returns: None
        """

        with db_session_scope() as db_session:
            variable_db_id = self._variable_db_id(variable, db_session, False)
            if (
                    #False positive
                    #pylint: disable=no-member
                    db_session.query(VarchangeDependentValue).filter_by(
                        variable_id=variable_db_id,
                        grid_id=self._grid_db_id).count() > 0
                    #pylint: enable=no-member
            ):
                self._read_variable_from_db(variable, db_session)
                #Attribute defined_weights defined by parent class.
                #pylint: disable=access-member-before-definition
                #pylint: disable=attribute-defined-outside-init
                if not self.defined_weights:
                    self._read_variable_from_db('weights', db_session)
                    self.defined_weights = True
                #pylint: enable=access-member-before-definition
            else:
                new_weights = not self.defined_weights
                super()._add_grid_variable(variable)
                assert self.defined_weights
                self._add_variable_to_db(variable, db_session)
                if new_weights:
                    self._add_variable_to_db('weights', db_session)
예제 #2
0
    def register_track_collection(self,
                                  track_fnames,
                                  fname_rex=library_track_fname_rex,
                                  model_suite='MESA'):
        """
        Add a collection of tracks with [Fe/H] and M* encoded in filename.

        Args:
            -track_fnames:
                The filenames of the tracks to add.
            - fname_rex:
                A regular expression defining groups named 'MASS' and
                either 'Z' or 'FeH' used to parse the filename for the
                stellar mass and metallicity each track applies to.
            - model_suite:
                The software suite used to generate the stellar evolution
                tracks.
        """

        for fname in track_fnames:
            parsed_fname = fname_rex.match(os.path.basename(fname)).groupdict()
            mass = self._get_decimal(parsed_fname['MASS'])
            if 'Z' in parsed_fname:
                feh = self._get_decimal(
                    library.feh_from_z(float(parsed_fname['Z'])))
            else:
                feh = self._get_decimal(parsed_fname['FeH'])

            with db_session_scope() as db_session:
                self._add_track(fname, mass, feh, model_suite, db_session)
예제 #3
0
    def get_suite_tracks(model_suite='MESA'):
        """Return all tracks from a given suite."""

        with db_session_scope() as db_session:
            #False positive
            #pylint: disable=no-member
            return db_session.query(Track).filter(
                and_(Track.model_suite_id == ModelSuite.id,
                     ModelSuite.name == model_suite))
예제 #4
0
    def list_suites():
        """Return a list of all software suites with available tracks."""

        with db_session_scope() as db_session:
            return [
                record[0] for record in
                #False positive
                #pylint: disable=no-member
                db_session.query(ModelSuite.name).all()
                #pylint: enable=no-member
            ]
예제 #5
0
    def list_interpolator_names():
        """Return a list of all intorpolator names."""

        with db_session_scope() as db_session:
            return [
                record[0] for record in
                #False positive
                #pylint: disable=no-member
                db_session.query(SerializedInterpolator.name).all()
                #pylint: enable=no-member
            ]
예제 #6
0
    def get_interpolator_by_name(self, name):
        """Return the interpolator with the given name."""

        with db_session_scope() as db_session:
            #False positive
            #pylint: disable=no-member
            return ManagedInterpolator(
                db_interpolator=db_session.query(
                    SerializedInterpolator).filter_by(name=name).one(),
                serialization_path=self._serialization_path,
                db_session=db_session)
예제 #7
0
    def __init__(self, serialization_path):
        """
        Create a manager storing serialized interpolators in the given path.

        Args:
            serialization_path:    The path where to store
                serialized interpolators.

        Returns:
            None
        """

        if not os.path.exists(serialization_path):
            os.makedirs(serialization_path)
        db_engine = create_engine(
            'sqlite:///' +
            os.path.join(serialization_path, 'serialized.sqlite'),
            echo=False)
        Session.configure(bind=db_engine)
        self._serialization_path = serialization_path
        with db_session_scope() as db_session:
            self._initialize_database(db_engine, db_session)
        with db_session_scope() as db_session:
            self._get_db_config(db_session)
예제 #8
0
def define_varchange_dependent_variables(db_session) :
    """
    Define the dependent variables for stellar evolution variable change.

    Args:
        - db_session:
            The currently active database session.

    Returns: None
    """

    db_variables = [
        VarchangeDependentVariable(id = id, name = name)
        for id, name in enumerate(['Teff', 'logg', 'L', 'rho'])
    ]
    db_session.add_all(db_variables)

if __name__ == '__main__' :
    db_engine = create_engine('sqlite:///test.sqlite',
                              echo = True)
    Session.configure(bind = db_engine)

    with db_session_scope() as db_session :
        for table in DataModelBase.metadata.sorted_tables :
            if db_engine.has_table(table.name) : continue
            if table.name.startswith('varchange_') :
                table.create(db_engine)
                if table.name == 'varchange_dependent_variables' :
                    define_varchange_dependent_variables(db_session)
예제 #9
0
    def register_track(self, track_fname, mass, feh, model_suite='MESA'):
        """Register a track for use in creating interpolators."""

        with db_session_scope() as db_session:
            self._add_track(track_fname, self._get_decimal(mass),
                            self._get_decimal(feh), model_suite, db_session)
예제 #10
0
    def get_interpolator(
            self,
            *,
            nodes=VarChangingInterpolator.default_nodes,
            smoothing=VarChangingInterpolator.default_smoothing,
            vs_log_age=VarChangingInterpolator.default_vs_log_age,
            log_quantity=VarChangingInterpolator.default_log_quantity,
            track_fnames=None,
            masses=None,
            feh=None,
            model_suite='MESA',
            new_interp_name=None,
            num_threads=1):
        """
        Return a stellar evolution interpolator with the given configuration.

        All tracks that the interpolator should be based on must be
        pre-registered with the manager. Two ways are supported for
        identifying tracks: as a list of filenames or as a mass-[Fe/H]
        grid combined with a suite. The first case always works, while the
        second requires that the set of identified tracks is unique, i.e. for
        none of the mass - [Fe/H] combinations there are two or more
        tracks registered for the given suite.

        Args:
            nodes:    The number of nodes to use for the age interpolation of
                each quantity of each track. Should be a dictionary with keys
                VarChangingInterpolator.quantity_list. See the POET code
                StellarEvolution::Interpolator::create_from() documentation for
                a description of what this actually means.

            smoothing:    The amount of smoothing to use for the age
                interpolation of each quantity of each track. Should be a
                dictionary with keys VarChangingInterpolator.quantity_list. See
                the POET code StellarEvolution::Interpolator::create_from()
                documentation for a description of what this actually means.

            vs_log_age:    Use log(age) instead of age as the independent
                argument for the intperpolation? Should be a dictionary with
                keys VarChangingInterpolator.quantity_list.

            log_quantity:    Interpolate log(quantity) instead of quantity?
                Should be a dictionary with keys
                VarChangingInterpolator.quantity_list.

            track_fnames:    A list of files containing stellar evolution tracks
                the interpolator should be based on.

            masses:    A list of the stellar masses to include in the
                interpolation. Unique tracks with those masses and all selected
                [Fe/H] (see next argument) must already be registered
                with the database for the given suite. If None, all track
                masses from the given suite are used.

            feh:    A list of the stellar [Fe/H] values to include in the
                interpolation. If None, all track [Fe/H] from the
                given suite are used.

            model_suite:    The software suite used to generate the stellar
                evolution tracks. May be omitted if tracks are specified by
                filename, but must be supplied if using masses and [Fe/H].

            new_interp_name:    Name to assign to the a newly generated
                interolator. Ignored if an interpolator matching all other
                arguments already exists. If not specified, and no interpolator
                exists matching the remining arguments, a new interpolator is
                not generated.

            num_threads:    If a new interpolator is created this many
                simultaneous interpolation threads are used.

        Returns:
            VarChangingInterpolator:
                Configured per the arguments supplied or None if
                no existing interpolator is found and creating a new one is
                forbidden (see new_interp_name argument).
        """

        with db_session_scope() as db_session:
            if track_fnames is None:
                track_grid = self._track_grid_from_grid(
                    masses, feh, model_suite, db_session)
            else:
                track_grid = self._track_grid_from_files(
                    track_fnames, db_session, model_suite)

            result = self._find_existing_interpolator(
                track_grid=track_grid,
                nodes=nodes,
                smoothing=smoothing,
                vs_log_age=vs_log_age,
                log_quantity=log_quantity,
                db_session=db_session)
            if result is not None:
                return result
            if new_interp_name is None:
                return None

            return self._create_new_interpolator(track_grid=track_grid,
                                                 nodes=nodes,
                                                 smoothing=smoothing,
                                                 vs_log_age=vs_log_age,
                                                 log_quantity=log_quantity,
                                                 db_session=db_session,
                                                 name=new_interp_name,
                                                 num_threads=num_threads)