Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 6 additions & 16 deletions app.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ def spectrum() -> dict[bool, list[float], list[float]]:
if not param_check(params):
return {
"success": False,
"text": "Parameter check failed",
"text": "One of the given parameters was invalid. Please change some settings and try again.",
}

# perform:
Expand Down Expand Up @@ -63,15 +63,14 @@ def spectrum() -> dict[bool, list[float], list[float]]:

@app.route("/background", methods=["POST"])
def background() -> dict[bool, list[float], list[float]]:
# try:
# put incoming JSON into a dictionary
data = json.loads(request.data)

# verify user input is valid
if not param_check(data):
return {
"success": False,
"text": "Parameter check failed",
"text": "One of the given parameters was invalid. Please change some settings and try again.",
}

# perform:
Expand All @@ -90,7 +89,7 @@ def background() -> dict[bool, list[float], list[float]]:
except:
return {
"success": False,
"text": "Background Failure"
"text": "There was an issue collecting the background spectra."
}

# perform:
Expand All @@ -111,31 +110,22 @@ def background() -> dict[bool, list[float], list[float]]:
"success": True,
"x": list(x_value),
"y": list(map(str, y_value)),
}

# except:
# return {
# "success": False,
# "text": "Issue Executing Scanning Procedures"
# }
}


@app.route("/find_peaks", methods=["POST"])
def handle_peaks() -> dict[bool, dict[float, float], str]:
data = json.loads(request.data)

peaks = find_peaks(
peaks, error = find_peaks(
data["x"],
data["y"],
float(data["lowerbound"]),
float(data["upperbound"]),
float(data["threshold"]),
)

if peaks:
return {"success": True, "peaks": peaks, "text": None}
else:
return {"success": False, "peaks": None, "text": "Failed to find peaks"}
return {"success": True, "peaks": peaks, "text": error}


# set debug to false in production environment
Expand Down
11 changes: 5 additions & 6 deletions processing.py
Original file line number Diff line number Diff line change
Expand Up @@ -151,14 +151,14 @@ def generate_spectrum(params: dict[str, str, str, float, str, float,
mole_fraction={params["molecule"]: params["mole"]},
)
except radis.misc.warning.EmptyDatabaseError:
return None, True, "error: No line in the specified wavenumber range"
return None, True, "There were not enough data points in the requested Wavenumber Range. Please expand your range and try again."
except Exception as e:
match str(e):
case "Failed to retrieve data for given parameters.":
return (
None,
True,
"error: HITRAN data does not exist for requested molecule.",
"There was an issue processing the data for the given parameters. Please adjust some settings and try again.",
)
case other:
return None, True, str(e)
Expand All @@ -167,7 +167,7 @@ def generate_spectrum(params: dict[str, str, str, float, str, float,


def find_peaks(x_data: list[float], y_data: list[float], lowerbound: float,
upperbound: float, threshold: float = 0) -> dict[float, float]:
upperbound: float, threshold: float = 0) -> tuple[dict[float, float], str]:
try:
spectrum = Spectrum.from_array(
x_data, y_data, "absorbance_noslit", wunit="cm-1", unit=""
Expand All @@ -177,8 +177,7 @@ def find_peaks(x_data: list[float], y_data: list[float], lowerbound: float,
)
lines = find_lines_threshold(new_spec, noise_factor=1)
except:
# TODO Add error message???
return None
return None, "Unable to find peaks with the given data and settings. Please adjust your settings and try again."

peaks = {}
for num, peak_type, _ in lines:
Expand All @@ -187,4 +186,4 @@ def find_peaks(x_data: list[float], y_data: list[float], lowerbound: float,
if peak_type == "emission" and y_data[index] >= threshold:
peaks[round(float(num.value), 4)] = round(y_data[index], 4)

return peaks
return peaks, None