# ---
# jupyter:
#   jupytext:
#     text_representation:
#       extension: .py
#       format_name: light
#       format_version: '1.5'
#       jupytext_version: 1.14.4
#   kernelspec:
#     display_name: Python 3
#     language: python
#     name: python3
# ---

# !pip install pandas
# !pip install xlrd
# !pip install sklearn
# !pip install imblearn

import xlrd
book = xlrd.open_workbook("Datasheets info.xlsx")
sheetMQ2 = book.sheet_by_name("MQ2 - Pololulu")
sheetMQ3 = book.sheet_by_name("MQ3 - Sparkfun")
sheetMQ4 = book.sheet_by_name("MQ4 - Sparkfun")
sheetMQ5 = book.sheet_by_name("MQ5 - Sparkfun")
sheetMQ6 = book.sheet_by_name("MQ6 - Sparkfun")
sheetMQ7 = book.sheet_by_name("MQ7 - Sparkfun")
sheetMQ8 = book.sheet_by_name("MQ8 - Sparkfun")
sheetMQ9 = book.sheet_by_name("MQ9 - Haoyuelectronics")
sheetMQ131 = book.sheet_by_name("MQ131- Sensorsportal")
sheetMQ135 = book.sheet_by_name("MQ135 - HANWEI")
sheetMQ303A = book.sheet_by_name("MQ303A - HANWEI")
sheetMQ309A = book.sheet_by_name("MQ309A - HANWEI")

for row_index in range(1,20): #reading first columns
    RsR0, LPG, H2, CH4, CO, Alcohol = sheetMQ6.row_values(row_index, start_colx=0, end_colx=6)
    print(RsR0, "	", LPG, "	", H2, "	", CH4, "	", CO, "	", Alcohol)
 

x_MQ6 = sheetMQ6.col_values(0)[2:]
MQ6_LPG = sheetMQ6.col_values(1)[2:]
MQ6_H2 = sheetMQ6.col_values(2)[2:]
MQ6_CH4 = sheetMQ6.col_values(3)[2:]
MQ6_CO = sheetMQ6.col_values(4)[2:]
MQ6_Alcohol = sheetMQ6.col_values(5)[2:]


def zero_to_nan(values):
    """Replace every 0 with 'nan' and return a copy."""
    return [float('nan') if x==0 else x for x in values]


MQ6_H2 =zero_to_nan(MQ6_H2)
MQ6_LPG =zero_to_nan(MQ6_LPG)
MQ6_CH4 =zero_to_nan(MQ6_CH4)
MQ6_CO =zero_to_nan(MQ6_CO)
MQ6_Alcohol =zero_to_nan(MQ6_Alcohol)

# +
import pandas as pd
import numpy as np
from sklearn.datasets import load_iris
#from sklearn.cross_validation import train_test_split
from sklearn.tree import DecisionTreeClassifier
from sklearn import datasets
from sklearn import linear_model

dataH2 = {'RsRo': x_MQ6, 'H2': MQ6_H2}
dataLPG = {'RsRo': x_MQ6, 'LPG': MQ6_LPG}
dataCH4 = {'RsRo': x_MQ6, 'CH4': MQ6_CH4}
dataCO = {'RsRo': x_MQ6, 'CO': MQ6_CO}
dataALcohol = {'RsRo': x_MQ6, 'Alcohol': MQ6_Alcohol}

dfMQ6_H2 = pd.DataFrame(dataH2)
dfMQ6_LPG = pd.DataFrame(dataLPG)
dfMQ6_CH4 = pd.DataFrame(dataCH4)
dfMQ6_CO = pd.DataFrame(dataCO)
dfMQ6_Alcohol = pd.DataFrame(dataALcohol)

dfMQ6_H2['H2'] = pd.to_numeric(dfMQ6_H2['H2'])
dfMQ6_LPG['LPG'] = pd.to_numeric(dfMQ6_LPG['LPG'])
dfMQ6_CH4['CH4'] = pd.to_numeric(dfMQ6_CH4['CH4'])
dfMQ6_CO['CO'] = pd.to_numeric(dfMQ6_CO['CO'])
dfMQ6_Alcohol['Alcohol'] = pd.to_numeric(dfMQ6_Alcohol['Alcohol'])

dfMQ6_H2['H2'] = dfMQ6_H2['H2'].replace('',None, regex=True)
dfMQ6_LPG['LPG'] = dfMQ6_LPG['LPG'].replace('',None, regex=True)
dfMQ6_CH4['CH4'] = dfMQ6_CH4['CH4'].replace('',None, regex=True)
dfMQ6_CO['CO'] = dfMQ6_CO['CO'].replace('',None, regex=True)
dfMQ6_Alcohol['Alcohol'] = dfMQ6_Alcohol['Alcohol'].replace('',None, regex=True)

#Global X_Predict variable
X_Predict = dfMQ6_LPG.RsRo.apply(lambda x: [x]).tolist()
# -

#Model and train H2
dataset2TrainH2 = dfMQ6_H2.copy()
dataset2TrainH2.dropna(inplace=True)
X_trainH2 = dataset2TrainH2.RsRo.apply(lambda x: [x]).tolist()
y_trainH2 = dataset2TrainH2['H2'].tolist()
model = linear_model.Lasso(alpha=0.1)
model.fit(X_trainH2, y_trainH2)
#Predict
H2_Predicted = model.predict(X_Predict)
#save into MQ2
MQ6_H2 = H2_Predicted


#Model and train LPG
dataset2TrainLPG = dfMQ6_LPG.copy()
dataset2TrainLPG.dropna(inplace=True)
X_trainLPG = dataset2TrainLPG.RsRo.apply(lambda x: [x]).tolist()
y_trainLPG = dataset2TrainLPG['LPG'].tolist()
model = linear_model.Lasso(alpha=0.1)
model.fit(X_trainLPG, y_trainLPG)
#Predict
LPG_Predicted = model.predict(X_Predict)
#save into MQ2
MQ6_LPG = LPG_Predicted

#Model and train CH4
dataset2TrainCH4 = dfMQ6_CH4.copy()
dataset2TrainCH4.dropna(inplace=True)
X_trainCH4 = dataset2TrainCH4.RsRo.apply(lambda x: [x]).tolist()
y_trainCH4 = dataset2TrainCH4['CH4'].tolist()
model = linear_model.Lasso(alpha=0.1)
model.fit(X_trainCH4, y_trainCH4)
#Predict
CH4_Predicted = model.predict(X_Predict)
#save into MQ2
MQ6_CH4 = CH4_Predicted

#Model and train CO
dataset2TrainCO = dfMQ6_CO.copy()
dataset2TrainCO.dropna(inplace=True)
X_trainCO = dataset2TrainCO.RsRo.apply(lambda x: [x]).tolist()
y_trainCO = dataset2TrainCO['CO'].tolist()
model = linear_model.Lasso(alpha=0.1)
model.fit(X_trainCO, y_trainCO)
#Predict
CO_Predicted = model.predict(X_Predict)
#save into MQ2
MQ6_CO = CO_Predicted

#Model and train Alcohol
dataset2TrainAlcohol = dfMQ6_Alcohol.copy()
dataset2TrainAlcohol.dropna(inplace=True)
X_trainAlcohol = dataset2TrainAlcohol.RsRo.apply(lambda x: [x]).tolist()
y_trainAlcohol = dataset2TrainAlcohol['Alcohol'].tolist()
model = linear_model.Lasso(alpha=0.1)
model.fit(X_trainAlcohol, y_trainAlcohol)
#Predict
Alcohol_Predicted = model.predict(X_Predict)
#save into MQ2
MQ6_Alcohol = Alcohol_Predicted

# +
# %config InlineBackend.figure_formats = ['svg']
# %matplotlib inline
import matplotlib.pyplot as plt
import matplotlib.lines as mlines
import matplotlib.transforms as mtransforms

fig, ax = plt.subplots()

fig.set_size_inches(9, 5.5, forward=True)
fig.set_dpi(200)

# only these two lines are calibration curves
plt.plot(MQ6_H2, x_MQ6, marker='o', linewidth=1, label='H2')
plt.plot(MQ6_LPG, x_MQ6, marker='o', linewidth=1, label='LPG')
plt.plot(MQ6_CH4, x_MQ6, marker='o', linewidth=1, label='CH4')
plt.plot(MQ6_CO, x_MQ6, marker='o', linewidth=1, label='CO')
plt.plot(MQ6_Alcohol, x_MQ6, marker='o', linewidth=1, label='Alcohol')


# reference line, legends, and axis labels
#line = mlines.Line2D([0, 1], [0, 1], color='black')
#transform = ax.transAxes
#line.set_transform(transform)
#ax.add_line(line)
plt.yscale('log')
plt.xscale('log')
plt.legend()

plt.grid(b=True, which='minor', color='lightgrey', linestyle='--')

fig.suptitle('Calibration plot for MQ-6 data')
ax.set_xlabel('PPM Concentration')
ax.set_ylabel('Rs/Ro')


#Save image
plt.savefig('MQ6.svg', format = 'svg', dpi = 1200)
plt.savefig('MQ6.png')
plt.savefig('MQ6.eps', format = 'eps', dpi = 1200)
# -



StarcoderdataJupyterScriptsDedupFiltered