<jupyter_start><jupyter_text>Air Quality and CO2 Correlation Moduleauthor: <NAME>This is an example of analyzing the correlation values between air quality and CO2 taken at the same time and plotting the related graph.<jupyter_code># Standard csv python libraries
import csv

# Main python library for mathematical calculations
import numpy as np
from scipy.stats.stats import pearsonr
import scipy

# Plotting related python libraries
import matplotlib.pyplot as plt

# Open csv CO2 and air quality files
userfile_CO2 = input("CO2 File: ")
userfile_aq =input("Air Quality File: ") 
results_CO2 = csv.reader(open(userfile_CO2), delimiter=',')
results_aq = csv.reader(open(userfile_aq), delimiter=',')

# Append CO2 and air quality data into separate lists
CO2 = []
aq = []
row_counter1 = 0
row_counter2 = 0

for r in results_CO2:
    row_counter1 += 1
    if row_counter1>1:
        CO2.append(int(r[1]))
        
for r in results_aq:
    row_counter2 += 1
    if row_counter2>1:
        aq.append(float(r[4]))
        
# Compare the length of the data lists
if len(aq)>len(CO2):
    minlist = CO2
if len(CO2)>len(aq):
    minlist = aq
    
# Create n_merge and calculate nsum_data
n_merge = int(input("n data points to combine:"))
ndata = len(minlist)
nsum_data = int(ndata/n_merge)

# Append merged CO2 and air quality data into separate lists
CO2_ave = []
CO2_unc = []
aq_ave = []
aq_unc = []


for i in range(nsum_data):
    idata1 = CO2[i*n_merge:(i+1)*n_merge]
    idata_array1 = np.asarray(idata1)
    CO2mean = np.mean(idata_array1)
    CO2sigma = np.sqrt(np.var(idata_array1))
    CO2_ave.append(CO2mean)
    CO2_unc.append(CO2sigma)
    idata2 = aq[i*n_merge:(i+1)*n_merge]
    idata_array2 = np.asarray(idata2)
    aq_mean = np.mean(idata_array2)
    aq_sigma = np.sqrt(np.var(idata_array2))
    aq_ave.append(aq_mean)
    aq_unc.append(aq_sigma)
    
# Caculate correlation values
a = pearsonr(CO2_ave, aq_ave)
b = scipy.stats.spearmanr(CO2_ave, aq_ave)
print("Pearson r =", a[0])
print("P value =", a[1])
print("Spearman r =", b[0])
print("Spearman r=", b[1])

# Plot graph
fig = plt.figure()
ax = fig.add_subplot(111)
plt.plot(CO2_ave, aq_ave, "b.")
plt.title("Air Quality vs CO2")
plt.xlabel("CO2 (ppm)")
plt.ylabel("Particle Concentration")
plt.legend()

# Show correlation values on graph
plt.text(0.6, 0.95, '%s %s' % ("Pearson r =",a[0]), ha='center', va='center', transform = ax.transAxes)
plt.text(0.6, 0.85, '%s %s' % ("P value =",a[1]), ha='center', va='center', transform = ax.transAxes)
plt.text(0.6, 0.75, '%s %s' % ("Spearman r =",b[0]), ha='center', va='center', transform = ax.transAxes)
plt.text(0.6, 0.65, '%s %s' % ("P value =",b[1]), ha='center', va='center', transform = ax.transAxes)

# Show graph
plt.show()<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.14569058781103497, 'fraction_numerical': 0.031013342949873783, 'mean_word_length': 4.460629921259843, 'pattern_counts': {'":': 0, '<': 6, '<?xml version=': 0, '>': 10, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '1', 'dir': 'jupyter-structured-clean-dedup', 'id': '230568', 'license': 'MIT', 'n_tokens_mistral': 1125, 'n_tokens_neox': 1062, 'n_words': 272, 'path': 'EnvironmentSensorModules/Air Quality and CO2 Correlation Module.ipynb', 'repo_name': 'kevinlmao/dosenet-analysis', 'size': '2775'}