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
110 changes: 55 additions & 55 deletions app.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,12 +19,12 @@


@app.route("/", methods=["GET"])
def ftir():
def ftir() -> str:
return "<h1 style='color:blue'>Raston Lab FTIR API</h1>"


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

Expand Down Expand Up @@ -62,66 +62,66 @@ def spectrum():


@app.route("/background", methods=["POST"])
def background():
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",
}

# perform:
# --> transmission spectrum of gas sample (calc_spectrum)
spectrum, error, message = generate_spectrum(data)
if error:
return {
"success": False,
"text": message,
}

# perform:
# --> set all y-values to one
try:
background_spectrum = process_background(spectrum)
except:
return {
"success": False,
"text": "Background Failure"
}

# perform:
# --> blackbody spectrum of source (sPlanck)
# --> transmission spectrum of beamsplitter and cell windows
# --> detector response spectrum
processed_spectrum = process_spectrum(data, background_spectrum)

if processed_spectrum is None:
return {
"success": False,
"text": "Issue Processing Data"
}
# https://radis.readthedocs.io/en/latest/source/radis.spectrum.spectrum.html#radis.spectrum.spectrum.Spectrum.get
x_value, y_value = processed_spectrum.get("transmittance_noslit")
# convert dictionary values to strings and return as JSON
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": True,
"x": list(x_value),
"y": list(map(str, y_value)),
"success": False,
"text": "Parameter check failed",
}


# perform:
# --> transmission spectrum of gas sample (calc_spectrum)
spectrum, error, message = generate_spectrum(data)
if error:
return {
"success": False,
"text": message,
}

# perform:
# --> set all y-values to one
try:
background_spectrum = process_background(spectrum)
except:
return {
return {
"success": False,
"text": "Background Failure"
}

# perform:
# --> blackbody spectrum of source (sPlanck)
# --> transmission spectrum of beamsplitter and cell windows
# --> detector response spectrum
processed_spectrum = process_spectrum(data, background_spectrum)

if processed_spectrum is None:
return {
"success": False,
"text": "Issue Executing Scanning Procedures"
}
"text": "Issue Processing Data"
}
# https://radis.readthedocs.io/en/latest/source/radis.spectrum.spectrum.html#radis.spectrum.spectrum.Spectrum.get
x_value, y_value = processed_spectrum.get("transmittance_noslit")
# convert dictionary values to strings and return as JSON
return {
"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():
def handle_peaks() -> dict[bool, dict[float, float], str]:
data = json.loads(request.data)

peaks = find_peaks(
Expand Down
31 changes: 18 additions & 13 deletions functions.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
# -------------------------------------
# ------------- blackbody -------------
# -------------------------------------
def __sPlanck(spectrum, source_temp):
def __sPlanck(spectrum: list[float], source_temp: int) -> list[float]:
"""
Calculates the y-values of a Blackbody spectrum.

Expand All @@ -27,7 +27,7 @@ def __sPlanck(spectrum, source_temp):
# --------------------------------------
# --------------- window ---------------
# --------------------------------------
def __CaF2(spectrum):
def __CaF2(spectrum: list[float]) -> list[float]:
"""
Calculates the y-values for a CaF2 cell window.

Expand All @@ -41,7 +41,7 @@ def __CaF2(spectrum):
return (0.93091) / (1 + (11.12929 / (10000 / spectrum)) ** -12.43933) ** 4.32574


def __ZnSe(spectrum):
def __ZnSe(spectrum: list[float]) -> list[float]:
"""
Calculates the y-values for a ZnSe cell window.

Expand All @@ -58,7 +58,7 @@ def __ZnSe(spectrum):
) * np.exp(-4 * np.log(2) * ((x_um - 16.75) ** 2) / (2.25051**2))


def __sapphire(spectrum):
def __sapphire(spectrum: list[float]) -> list[float]:
"""
Calculates the y-values for a sapphire window.

Expand All @@ -72,7 +72,7 @@ def __sapphire(spectrum):
return 0.78928 / (1 + (11.9544 / (10000 / spectrum)) ** -12.07226) ** 6903.57039


def __AR_ZnSe(spectrum):
def __AR_ZnSe(spectrum: list[float]) -> list[float]:
"""
Calculates the y-values for a AR_ZnSe beamsplitter.

Expand Down Expand Up @@ -116,7 +116,7 @@ def __AR_ZnSe(spectrum):
)


def __AR_CaF2(spectrum):
def __AR_CaF2(spectrum: list[float]) -> list[float]:
"""
Calculates the y-values for a AR_CaF2 beamsplitter.

Expand Down Expand Up @@ -160,7 +160,7 @@ def __AR_CaF2(spectrum):
# --------------------------------------
# -------------- detector --------------
# --------------------------------------
def __InSb(spectrum):
def __InSb(spectrum: list[float]) -> list[float]:
"""
Calculates the y-values for an InSb detector.

Expand All @@ -179,7 +179,7 @@ def __InSb(spectrum):
)


def __MCT(spectrum):
def __MCT(spectrum: list[float]) -> list[float]:
"""
Calculates the y-values for a MCT detector.

Expand All @@ -205,7 +205,7 @@ def __MCT(spectrum):
# -------------------------------------
# ---------- helper functions ----------
# ------------------------------------
def zeroY(spectrum):
def zeroY(spectrum: list[float]) -> list[float]:
"""
Calculates the y-values (y = 1) for background samples.

Expand All @@ -218,7 +218,9 @@ def zeroY(spectrum):
return (spectrum * 0) + 1


def param_check(params):
def param_check(params:
dict[str, str, str, float, str, float,
float, int, int, int, int, str, int]) -> bool:
"""
Parses user provided parameters for validity.

Expand Down Expand Up @@ -259,7 +261,7 @@ def param_check(params):
return True


def calc_wstep(resolution, zero_fill):
def calc_wstep(resolution: float, zero_fill: int) -> float:
"""
Calculates the appropriate wstep for a spectrum based on the given resolution and zero fill.

Expand Down Expand Up @@ -338,7 +340,7 @@ def calc_wstep(resolution, zero_fill):

return wstep

def multiscan(spectrum, num_scans):
def multiscan(spectrum: Spectrum, num_scans: int) -> Spectrum:
# add random noise to spectrum
# https://radis.readthedocs.io/en/latest/source/radis.spectrum.operations.html#radis.spectrum.operations.add_array
w = spectrum.get_wavenumber()
Expand Down Expand Up @@ -371,7 +373,10 @@ def multiscan(spectrum, num_scans):

return spectrum

def get_component_spectra(w, source_temp):
def get_component_spectra(w: list[float], source_temp: int) -> tuple[Spectrum, Spectrum,
Spectrum, Spectrum,
Spectrum, Spectrum,
Spectrum, Spectrum]:
# processing for blackbody spectrum (sPlanck)
spec_sPlanck = Spectrum(
{"wavenumber": w, "transmittance_noslit": __sPlanck(w, source_temp)},
Expand Down
14 changes: 9 additions & 5 deletions processing.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,8 @@
# ------------------------------
# ----- Spectrum Processing -----
# ------------------------------
def process_spectrum(params, raw_spectrum):
def process_spectrum(params: dict[str, str, str, float, str, float, float, int,
int, int, int, str, int], raw_spectrum: Spectrum) -> Spectrum:
"""
The following function takes a 'raw spectrum' generated using Radis's
'calc_spectrum()' function and performing custom equations that virtualize
Expand Down Expand Up @@ -85,7 +86,7 @@ def process_spectrum(params, raw_spectrum):
return spectrum


def process_background(raw_spectrum):
def process_background(raw_spectrum: Spectrum) -> Spectrum:
"""
Accepts a spectrum generated using '__generate_spectrum()'.
A background by default has all y-values of one.
Expand All @@ -110,7 +111,8 @@ def process_background(raw_spectrum):
return spec_zeroY


def generate_spectrum(params):
def generate_spectrum(params: dict[str, str, str, float, str, float,
float, int, int, int, int, str, int]) -> tuple[Spectrum, bool, str]:
"""
Generates a spectrum using Radis's 'calc_spectrum()' function based
on user parameters. That spectrum is then processed by
Expand Down Expand Up @@ -161,10 +163,11 @@ def generate_spectrum(params):
case other:
return None, True, str(e)

return spectrum, False, None
return (spectrum, False, None)


def find_peaks(x_data, y_data, lowerbound, upperbound, threshold=0):
def find_peaks(x_data: list[float], y_data: list[float], lowerbound: float,
upperbound: float, threshold: float = 0) -> dict[float, float]:
try:
spectrum = Spectrum.from_array(
x_data, y_data, "absorbance_noslit", wunit="cm-1", unit=""
Expand All @@ -174,6 +177,7 @@ def find_peaks(x_data, y_data, lowerbound, upperbound, threshold=0):
)
lines = find_lines_threshold(new_spec, noise_factor=1)
except:
# TODO Add error message???
return None

peaks = {}
Expand Down