Example #1
0
def main(arguments=None):

    if arguments is None:
        arguments = sys.argv[1:]
    args = parser.parse_args(args=arguments)

    with database.LEMONdB(args.db_path) as db:
        print("Input coordinates:")
        print("α: {} ({})".format(args.ra, util.coords.ra_str(args.ra)))
        print("δ: {} ({})".format(args.dec, util.coords.dec_str(args.dec)))

        star_id, distance = db.star_closest_to_world_coords(args.ra, args.dec)
        star = db.get_star(star_id)

        print()
        print("Selected star:")
        print("ID: {}".format(star_id))
        print("α: {} ({})".format(star.ra, util.coords.ra_str(star.ra)))
        print("δ: {} ({})".format(star.dec, util.coords.dec_str(star.dec)))
        print("Distance to input coordinates: {} deg".format(distance))
        print()

        if args.output == sys.stdout:
            print("Light curve in {!r} photometric filter:".format(
                args.filter))

        star_diff = db.get_light_curve(star_id, args.filter)
        if star_diff is None:
            raise ValueError(
                "no light curve for {!r} photometric filter".format(
                    args.filter))

        table = prettytable.PrettyTable()
        table.field_names = ["Date (UTC)", "JD", "Δ Mag", "SNR"]

        def format_float(f):
            """Returns f as a string rounded to parser.places decimal places."""
            return "{:.{places}f}".format(f, places=args.places)

        for unix_time, magnitude, snr in star_diff:
            jd = astropy.time.Time(unix_time, format="unix").jd
            table.add_row([
                util.utctime(unix_time, suffix=False),
                format_float(jd),
                format_float(magnitude),
                format_float(snr),
            ])

        args.output.write(str(table))
        args.output.write("\n")

        if args.output != sys.stdout:
            print(
                "Wrote light curve in {!r} photometric filter to {!r}.".format(
                    args.filter, args.output.name))
Example #2
0
def plot_stars(filename, star_identifiers):
    """ . """

    print 'Opening LEMON db %s for stars %s.' % (filename, star_identifiers)

    # Create database in LEMON format.
    db = database.LEMONdB(filename)

    # Retrieve the information of filters created.
    filters = db.pfilters

    print 'Ready to read stars from a LEMON db for filters %s.' % str(filters)

    # Properties for the Lomb Scargle method.
    lsprop = lombscargle.LSProperties()
    ls = lombscargle.LombScargle(lsprop)

    # For all the stars in the database.
    for star_id in star_identifiers:
        # For all the filters of current star.
        for filter_index in range(len(filters)):
            # Get the filter.
            pfilter = filters[filter_index]

            curve = db.get_light_curve(int(star_id), pfilter)
            # Get the object that will calculate the periodgram of the curve
            # (not normalized).
            try:
                # Calculate and plot the periodgram.
                ls.calculate_periodgram(pfilter, curve, True)

            except TypeError:
                print "Error reading from DB star with identifier %s for filter %s" % (
                    star_id, str(pfilter))

            break

    print 'Finished the reading of stars from LEMON db.'
Example #3
0
def predict_star(database_filename, starsclasses_filename, model_name,
                 star_id):
    """ Predict the class of a star. """

    #print 'Opening LEMON db %s for star %s.' % (database_filename, star_id)

    # Create database in LEMON format.
    db = database.LEMONdB(database_filename)

    # Retrieve the information of filters created.
    filters = db.pfilters
    #print "Predicting star '%s' for filters %s" % (star_id, filters)

    #print "Reading star classes."
    scn = modelserial.StarClassNames()
    # Read star classes.
    scn.read()

    #print "Reading model."
    cm = modelserial.ClassifModel()
    # Read classifier model.
    clf, filters_names = cm.read_model(model_name)

    #print "Read model for filters %s" % filters_names

    if clf <> None and len(clf) > 0:

        #print 'Ready to read stars from a LEMON db for filters %s.' % str(filters)

        # Properties for the Lomb Scargle method.
        lsprop = lombscargle.LSProperties()
        ls = lombscargle.LombScargle(lsprop)

        # For all the filters of current star.
        for filter_index in range(len(filters)):
            # Get the filter.
            pfilter = filters[filter_index]

            curve = db.get_light_curve(int(star_id), pfilter)
            # Get the object that will calculate the periodgram of the curve
            # (not normalized).
            try:
                print "Calculating features."
                # Calculate and plot the periodgram.
                pgram, nmags, ntimes = ls.calculate_periodgram(pfilter, curve)

                # Calculate periodic features of stars.
                perfeat = periodicfeature.PeriodicFeature(pgram, lsprop)

                # Calculate no periodic features of stars.
                noperfeat = nonperiodicfeature.NonPeriodicFeature(
                    nmags, ntimes)

                # Store all the features of this star in a list.
                star_features_in_current_filter, features_names = \
                    starfeatures.StarsFeatures.save_feature(perfeat, noperfeat)

                predict_stars_class(star_features_in_current_filter,
                                    str(pfilter), clf, filters_names, scn)

            except TypeError:
                print "Error reading from DB star with identifier %s for filter %s" % (
                    star_id, str(pfilter))
    else:
        print "ERROR: No classifier has been read."
Example #4
0
def main(arguments=None):
    """ main() function, encapsulated in a method to allow for easy invokation.

    This method follows Guido van Rossum's suggestions on how to write Python
    main() functions in order to make them more flexible. By encapsulating the
    main code of the script in a function and making it take an optional
    argument the script can be called not only from other modules, but also
    from the interactive Python prompt.

    Guido van van Rossum - Python main() functions:
    http://www.artima.com/weblogs/viewpost.jsp?thread=4829

    Keyword arguments:
    arguments - the list of command line arguments passed to the script.

    """

    if arguments is None:
        arguments = sys.argv[1:]  # ignore argv[0], the script name
    (options, args) = parser.parse_args(args=arguments)

    # Adjust the logger level to WARNING, INFO or DEBUG, depending on the
    # given number of -v options (none, one or two or more, respectively)
    logging_level = logging.WARNING
    if options.verbose == 1:
        logging_level = logging.INFO
    elif options.verbose >= 2:
        logging_level = logging.DEBUG
    logging.basicConfig(format=style.LOG_FORMAT, level=logging_level)

    if len(args) != 2:
        parser.print_help()
        return 2  # used for command line syntax errors
    else:
        assert len(args) == 2
        input_db_path = args[0]
        output_db_path = args[1]

    if options.min_cstars > options.ncstars:
        print "%sError. The value of --minimum-stars must be <= --stars." % style.prefix
        print style.error_exit_message
        return 1

    if not os.path.exists(input_db_path):
        print "%sError. Database '%s' does not exist." % (style.prefix,
                                                          input_db_path)
        print style.error_exit_message
        return 1

    if os.path.exists(output_db_path):
        if not options.overwrite:
            print "%sError. The output database '%s' already exists." % \
                  (style.prefix, output_db_path)
            print style.error_exit_message
            return 1
        else:
            os.unlink(output_db_path)

    # Note that we do not allow the user to update an existing LEMON database,
    # although it would make sense if we had just done photometry and now we
    # wanted to generate the light curves. However, and to be on the safe side,
    # we prefer to preserve a copy of the original database, as it is not
    # inconceivable that the astronomer may need to recompute the curves more
    # than once, each time with a different set of parameters.

    print "%sMaking a copy of the input database..." % style.prefix,
    sys.stdout.flush()
    shutil.copy2(input_db_path, output_db_path)
    methods.owner_writable(output_db_path, True)  # chmod u+w
    print 'done.'

    db = database.LEMONdB(output_db_path)
    nstars = len(db)
    print "%sThere are %d stars in the database" % (style.prefix, nstars)

    for pfilter in sorted(db.pfilters):

        print style.prefix
        print "%sLight curves for the %s filter will now be generated." % \
              (style.prefix, pfilter)
        print "%sLoading photometric information..." % style.prefix,
        sys.stdout.flush()
        all_stars = [
            db.get_photometry(star_id, pfilter) for star_id in db.star_ids
        ]
        print 'done.'

        # The generation of each light curve is a task independent from the
        # others, so we can use a pool of workers and do it in parallel.
        pool = multiprocessing.Pool(options.ncores)
        map_async_args = ((star, all_stars, options) for star in all_stars)
        result = pool.map_async(parallel_light_curves, map_async_args)

        methods.show_progress(0.0)
        while not result.ready():
            time.sleep(1)
            methods.show_progress(queue.qsize() / len(all_stars) * 100)
            # Do not update the progress bar when debugging; instead, print it
            # on a new line each time. This prevents the next logging message,
            # if any, from being printed on the same line that the bar.
            if logging_level < logging.WARNING:
                print

        result.get()  # reraise exceptions of the remote call, if any
        methods.show_progress(100)  # in case the queue was ready too soon
        print

        # The multiprocessing queue contains two-element tuples,
        # mapping the ID of each star to its light curve.
        print "%sStoring the light curves in the database..." % style.prefix
        methods.show_progress(0)
        light_curves = (queue.get() for x in xrange(queue.qsize()))
        for index, (star_id, curve) in enumerate(light_curves):

            # NoneType is returned by parallel_light_curves when the light
            # curve could not be calculated -- because it did not meet the
            # minimum number of images or comparison stars.
            if curve is None:
                logging.debug("Nothing for star %d; light curve could not "
                              "be generated" % star_id)
                continue

            logging.debug("Storing light curve for star %d in database" %
                          star_id)
            db.add_light_curve(star_id, curve)
            logging.debug("Light curve for star %d successfully stored" %
                          star_id)

            methods.show_progress(100 * (index + 1) / len(all_stars))
            if logging_level < logging.WARNING:
                print

        else:
            logging.info("Light curves for %s generated" % pfilter)
            logging.debug("Committing database transaction")
            db.commit()
            logging.info("Database transaction commited")

            methods.show_progress(100.0)
            print

    print "%sUpdating statistics about tables and indexes..." % style.prefix,
    sys.stdout.flush()
    db.analyze()
    print 'done.'

    # Update LEMONdB metadata
    db.date = time.time()
    db.author = pwd.getpwuid(os.getuid())[0]
    db.hostname = socket.gethostname()
    db.commit()

    methods.owner_writable(output_db_path, False)  # chmod u-w
    print "%sYou're done ^_^" % style.prefix
    return 0
Example #5
0
    def calculate_features(self, filename):
        """ Calculate features of the stars. Read the light curves from
            database, calculate periodic and no periodic features and store
            all the features in a data structure that is accessed using
            an index corresponding to the order the star has been stored
            in star_classes.
            
            filename - Name of the LEMON database file that contains the
            light curves of the stars.
            
        """

        logging.info('Opening LEMON db %s.' % filename)

        # Create database in LEMON format.
        db = database.LEMONdB(filename)

        # Retrieve the information of filters created.
        filters = db.pfilters

        # For each filter add an empty list to contain the features
        # of all the stars in that filter.
        for a_filter in filters:
            # Adds a new filter for the set of stars.
            self.__star_classes.add_filter_name(str(a_filter))

        logging.info('Ready to read and calculate features using %d light curves from a LEMON db for filters %s.' % \
                     (self.__star_classes.number_of_stars, self.__star_classes.filters_names) )

        # Properties for the Lomb Scargle method.
        lsprop = lombscargle.LSProperties()
        ls = lombscargle.LombScargle(lsprop)

        # Percentage of calculation completed.
        perc_completed = 0

        # For all the stars in the database.
        for star_index in range(self.__star_classes.number_of_stars):
            # For all the filters of current star.
            for filter_index in range(len(filters)):
                # Get the filter.
                pfilter = filters[filter_index]

                # Get the curve of the current star in the current filter.
                star_id = self.__star_classes.get_instance_id(star_index)

                curve = db.get_light_curve(star_id, pfilter)
                # Get the object that will calculate the periodgram of the curve
                # (not normalized).
                try:
                    # Calculate the periodgram.
                    pgram, nmags, ntimes = ls.calculate_periodgram(
                        pfilter, curve)

                    # Calculate periodic features of stars.
                    perfeat = periodicfeature.PeriodicFeature(pgram, lsprop)

                    # Calculate no periodic features of stars.
                    noperfeat = nonperiodicfeature.NonPeriodicFeature(
                        nmags, ntimes)

                    # Store all the features of this star in a list.
                    star_features_in_current_filter, self.__features_names = \
                        StarsFeatures.save_feature(perfeat, noperfeat)

                    # Add the features calculated in the appropriate filter
                    # data structure.
                    self.__star_classes.add_feature(
                        filter_index, star_features_in_current_filter)
                except TypeError:
                    logging.error(
                        "Error reading from DB star with identifier %d for filter %s"
                        % (star_id, pfilter))

                    self.__star_classes.disable_star(star_id)

                    # Save fake feature for this star. Necessary to avoid an
                    # error accessing with a sequential index to a non
                    # existing feature. This feature wouldn't be accessed as
                    # this has been disabled.
                    self.__star_classes.add_feature(filter_index, [])

            # Only to print a progress message of the calculation each 10% of advance.
            perc = star_index * 100 / self.__star_classes.number_of_stars
            perc_module = perc % 10
            if perc >= 10 and perc_module == 0 and perc != perc_completed:
                logging.info('Calculating features for stars:%3.f%% done.',
                             perc)
                perc_completed = perc

        logging.info('Finished the calculation of features from LEMON db.')