예제 #1
0
def library_search(library):
    self_url = url_for("library_search", library=library)
    path = os_path.join(args.path, library)
    x = SpectrumLibrarySqlite(path=path)
    metadata_keys = [str(i) for i in x._metadata_fields]
    metadata_keys.sort()

    # Fetch search constraints from POST data
    search = {"minima": {}, "maxima": {}}  # This structure contains strings which we put into the search form
    constraints = {}  # This structure contains the float / string constraints which we pass to the SpectrumLibrary
    # Loop over each of the metadata items we can be constrained on
    for item in metadata_keys:
        lower_limit = str(request.form.get("min_{}".format(item), ""))
        upper_limit = str(request.form.get("max_{}".format(item), ""))
        search['minima'][item] = lower_limit
        search['maxima'][item] = upper_limit
        # We only have a constraint on this metadata item if we have POST data which is not blank
        have_constraint = (lower_limit != "") or (upper_limit != "")

        # If we do have a constraint, test whether it's a string constraint, or a numeric constraint
        if have_constraint:
            lower_limit_float = 0
            upper_limit_float = 1e9
            # Test whether we get an exception when we try converting constraint to floats
            try:
                if lower_limit != "":
                    lower_limit_float = float(lower_limit)
                if upper_limit != "":
                    upper_limit_float = float(upper_limit)
                string_constraint = False
            except ValueError:
                string_constraint = True

            # Create a new numeric metadata constraint
            if not string_constraint:
                constraints[item] = ((lower_limit_float, upper_limit_float) if lower_limit_float != upper_limit_float
                                     else lower_limit_float)

            # Create a new string metadata constraint
            else:
                if lower_limit is None:
                    lower_limit = ""
                if upper_limit is None or upper_limit == "":
                    upper_limit = "zzzzzzzzz"
                constraints[item] = (lower_limit, upper_limit) if lower_limit != upper_limit else lower_limit

    # Search the SpectrumLibrary for matching spectra
    spectrum_ids = [i['specId'] for i in x.search(**constraints)]
    result_count = len(spectrum_ids)

    # Show a maximum of 100 results
    if len(spectrum_ids) > 100:
        spectrum_ids = spectrum_ids[:100]
    results = x.get_metadata(ids=spectrum_ids)

    # Add spectrum_id into each spectrum's metadata -- the HTML template needs this so we can link to spectrum viewer
    for i in range(len(spectrum_ids)):
        results[i]["spectrum_id"] = spectrum_ids[i]
    return render_template('library.html', path=args.path, library=library, metadata_keys=metadata_keys,
                           search=search, results=results, result_count=result_count, self_url=self_url)
# Instantiate the RV code
time_start = time.time()
rv_code = RvInstanceBrani.from_spectrum_library_sqlite(
    library_path=library_path)
n_burn_default = rv_code.n_burn
n_steps_default = rv_code.n_steps
time_end = time.time()
logger.info("Set up time was {:.2f} sec".format(time_end - time_start))

# Open the library of APOKASC test spectra
test_library_name = args.test_library
test_library_path = os_path.join(workspace, test_library_name)
test_library = SpectrumLibrarySqlite(path=test_library_path, create=False)

# Load test set
test_library_ids = [i["specId"] for i in test_library.search()]

# Pick some random spectra
indices = [
    random.randint(0,
                   len(test_library_ids) - 1) for i in range(args.test_count)
]

# Start writing output
with open(args.output_file, "w") as output:
    column_headings_written = False
    stellar_label_names = []

    # Loop over the spectra we are going to test
    for index in indices:
        # Look up database ID of the test spectrum