text_plain_output_1.png image_output_1.png
from glob import os

import collections

import csv

import matplotlib.pyplot as plt

import re

import csv
import matplotlib.pyplot as plt
import statistics as st
import re
import collections
from glob import os
import pandas as pd
arr = os.listdir('../input/spectra-files/')
arr = sorted(arr, key=lambda x: int(os.path.splitext(x)[0]))

def function_general(file_name):
    wav = []
    absr = []
    d = {}
    with open(file_name, 'r') as df:
        reader = csv.reader(df)
        header = next(reader)
        for row in reader:
            d[float(row[0])] = float(row[1])
            wav.append(float(row[0]))
            absr.append(float(row[1]))
function_general('../input/spectra-files/50.csv')

def function_peak_area(file_name, in_w, fin_w):
    dp = {}
    absr = []
    wav = []
    product = {}
    with open(file_name, 'r') as df:
        reader = csv.reader(df)
        header = next(reader)
        for row in reader:
            if float(row[0]) > in_w and float(row[0]) < fin_w:
                dp[float(row[0])] = float(row[1])
                absr.append(float(row[1]))
                wav.append(float(row[0]))
    max_w = list(dp.keys())[list(dp.values()).index(max(absr))]
    min_w = list(dp.keys())[list(dp.values()).index(min(absr))]
    peak = dp[max_w] - dp[min_w]
    area = peak * (fin_w - in_w) / 2
    name = file_name
    tiempo = re.findall('\\d+', name)
    product = {float(tiempo[0]): area}
    return product

def function_macro(file_list):
    product_dict_list = {}
    react_dict_list = {}
    for x in file_list:
        file_name = '../input/spectra-files/' + x
        product_dict_list.update(function_peak_area(file_name, 1070, 1330))
        react_dict_list.update(function_peak_area(file_name, 2186, 2705))
    product = collections.OrderedDict(sorted(product_dict_list.items()))
    reactive = collections.OrderedDict(sorted(react_dict_list.items()))
    return (product, reactive)
product, react = function_macro(arr)