def execute(trial=False): '''Run a statistical analysis gathering data comparing carbon efficacy to emissions per capita''' startTime = datetime.datetime.now() client = dml.pymongo.MongoClient() repo = client.repo repo.authenticate('aheckman_jfimbres', 'aheckman_jfimbres') efficacy = repo.aheckman_jfimbres.carbon_efficacy.find({}) epc = repo.aheckman_jfimbres.emissions_per_capita.find({}) eff = t.project(efficacy, lambda x: x) epc = t.project(epc, lambda x: x) eff = [list(x.values()) for x in eff] epc = [list(x.values()) for x in epc] eff = eff[0][1:] epc = epc[0][1:] corr = scipy.stats.pearsonr(eff, epc) corr = {"Stats": corr} repo.dropCollection("Stats") repo.createCollection("Stats") repo['aheckman_jfimbres.Stats'].insert(corr) repo.logout() endTime = datetime.datetime.now() return {"start": startTime, "end": endTime}
def execute(trial = False): '''Return data that shows carbon efficacy alongside partisan climate opinion data''' startTime = datetime.datetime.now() client = dml.pymongo.MongoClient() repo = client.repo repo.authenticate('aheckman_jfimbres', 'aheckman_jfimbres') efficacy = repo.aheckman_jfimbres.carbon_efficacy.find({}) opinions = repo.aheckman_jfimbres.partisan_map.find({}) state_ops = t.select(opinions, lambda x: x['GeoType'] == 'State') ops_by_state = t.project(state_ops, lambda x: [x['GeoName'], x['Group'],x['TotalPop'], x['congress'], x['congressOppose'], x['corporations'], x['corporationsOppose'], x['citizens'], x['citizensOppose'], x['regulate'], x['regulateOppose'], x['exp'], x['expOppose'], x['prienv'], x['prienvOppose'], x['happening'], x['happeningOppose'], x['human'], x['humanOppose'], x['consensus'], x['consensusOppose'], x['worried'], x['worriedOppose'], x['harmUS'], x['harmUSOppose']]) eff = t.project(efficacy, lambda x: x) ops_w_eff = t.project(ops_by_state, lambda x: [x[0], (eff[0][x[0]], x[1:])]) ops_w_eff = t.aggregate(ops_w_eff, lambda x: [x[0], x[1][1]]) repo.dropCollection("opinion_by_efficacy") repo.createCollection("opinion_by_efficacy") repo['aheckman_jfimbres.opinion_by_efficacy'].insert(dict(ops_w_eff)) repo.logout() endTime = datetime.datetime.now() return {"start": startTime, "end": endTime}
def execute(trial = False): '''Give the adjusted emissions per capita on a state-by-state basis''' startTime = datetime.datetime.now() client = dml.pymongo.MongoClient() repo = client.repo repo.authenticate('aheckman_jfimbres', 'aheckman_jfimbres') co2_adjusted = repo.aheckman_jfimbres.co2_adjusted.find({}) c = repo.aheckman_jfimbres.census.find({}) adj = t.project(t.select(co2_adjusted, lambda x: x['State'] in list_of_states), lambda x: (x['State'], x['2016']*1000000)) pops = t.project(t.select(c, lambda x: x['NAME'] in list_of_states), lambda x: (x['NAME'], x['POPESTIMATE2017'])) # adj is given in x millions, so it needs to be scaled up to match population, which is just given in x tons = t.aggregate(t.union(adj, pops), lambda vs: (vs[0]/vs[1])) epc = dict(t.project(tons, lambda t: (t[0], t[1]))) repo.dropCollection("emissions_per_capita") repo.createCollection("emissions_per_capita") repo["aheckman_jfimbres.emissions_per_capita"].insert(epc) repo.logout() endTime = datetime.datetime.now() return {"start":startTime, "end":endTime}
def execute(trial = False): '''Find the efficiency of carbon usage on a stat-by-state basis''' startTime = datetime.datetime.now() client = dml.pymongo.MongoClient() repo = client.repo repo.authenticate('aheckman_jfimbres', 'aheckman_jfimbres') co2_adjusted = repo.aheckman_jfimbres.co2_adjusted.find({}) co2_unadjusted = repo.aheckman_jfimbres.co2_unadjusted.find({}) carbon_intensity = repo.aheckman_jfimbres.carbon_intensity.find({}) adj = t.project(t.select(co2_adjusted, lambda x: x['State'] in list_of_states), lambda x: (x['State'], x['2016'])) unadj = t.project(t.select(co2_unadjusted, lambda x: x['State'] in list_of_states), lambda x: (x['State'], x['Total'])) intense = t.project(t.select(carbon_intensity, lambda x: x['State'] in list_of_states), lambda x: (x['State'], x['2016'])) emissions = t.aggregate(t.union(adj, unadj), np.mean) efficacy = dict(t.aggregate(t.union(emissions, intense), lambda v: (v[0]/v[1]))) repo.dropCollection("carbon_efficacy") repo.createCollection("carbon_efficacy") repo['aheckman_jfimbres.carbon_efficacy'].insert(efficacy) repo.logout() endTime = datetime.datetime.now() return {"start":startTime, "end":endTime}
def execute(trial=False): '''Run a statistical analysis gathering data comparing carbon efficacy to emissions per capita''' startTime = datetime.datetime.now() client = dml.pymongo.MongoClient() repo = client.repo repo.authenticate('aheckman_jfimbres', 'aheckman_jfimbres') ops_by_efficacy = repo.aheckman_jfimbres.opinion_by_efficacy.find({}) ops = t.project(ops_by_efficacy, lambda x: x) ops = [list(x.values()) for x in ops] ops = ops[0][1:] #dem beliefs are ops[0][0][1] #rep beliefs are ops[0][1] q1 = [] q2 = [] q3 = [] q4 = [] q5 = [] q6 = [] q7 = [] q8 = [] q9 = [] q10 = [] q11 = [] for i in range(len(ops)): q1 += ([ops[i][0][1][2], ops[i][0][1][3]], [ops[i][1][2], ops[i][1][3]]) q2 += ([ops[i][0][1][4], ops[i][0][1][5]], [ops[i][1][4], ops[i][1][5]]) q3 += ([ops[i][0][1][6], ops[i][0][1][7]], [ops[i][1][6], ops[i][1][7]]) q4 += ([ops[i][0][1][8], ops[i][0][1][9]], [ops[i][1][8], ops[i][1][9]]) q5 += ([ops[i][0][1][10], ops[i][0][1][11]], [ops[i][1][10], ops[i][1][11]]) q6 += ([ops[i][0][1][12], ops[i][0][1][13]], [ops[i][1][12], ops[i][1][13]]) q7 += ([ops[i][0][1][14], ops[i][0][1][15]], [ops[i][1][14], ops[i][1][15]]) q8 += ([ops[i][0][1][16], ops[i][0][1][17]], [ops[i][1][16], ops[i][1][17]]) q9 += ([ops[i][0][1][18], ops[i][0][1][19]], [ops[i][1][18], ops[i][1][19]]) q10 += ([ops[i][0][1][20], ops[i][0][1][21]], [ops[i][1][20], ops[i][1][21]]) q11 += ([ops[i][0][1][22], ops[i][0][1][23]], [ops[i][1][22], ops[i][1][23]]) k1 = KMeans(n_clusters=2, random_state=0).fit(q1) k2 = KMeans(n_clusters=2, random_state=0).fit(q2) k3 = KMeans(n_clusters=2, random_state=0).fit(q3) k4 = KMeans(n_clusters=2, random_state=0).fit(q4) k5 = KMeans(n_clusters=2, random_state=0).fit(q5) k6 = KMeans(n_clusters=2, random_state=0).fit(q6) k7 = KMeans(n_clusters=2, random_state=0).fit(q7) k8 = KMeans(n_clusters=2, random_state=0).fit(q8) k9 = KMeans(n_clusters=2, random_state=0).fit(q9) k10 = KMeans(n_clusters=2, random_state=0).fit(q10) k11 = KMeans(n_clusters=2, random_state=0).fit(q11) inertias = [ k1.inertia_, k2.inertia_, k3.inertia_, k4.inertia_, k5.inertia_, k6.inertia_, k7.inertia_, k8.inertia_, k9.inertia_, k10.inertia_, k11.inertia_ ] inertias = {"disagreement on each question": inertias} repo.dropCollection("k_means") repo.createCollection("k_means") repo['aheckman_jfimbres.k_means'].insert(inertias) repo.logout() endTime = datetime.datetime.now() return {"start": startTime, "end": endTime}
def execute(trial=False): '''Run a statistical analysis gathering data comparing carbon efficacy to emissions per capita''' startTime = datetime.datetime.now() client = dml.pymongo.MongoClient() repo = client.repo repo.authenticate('aheckman_jfimbres', 'aheckman_jfimbres') ops_by_efficacy = repo.aheckman_jfimbres.opinion_by_efficacy.find({}) ops = t.project(ops_by_efficacy, lambda x: x) ops = [list(x.values()) for x in ops] ops = ops[0][1:] #dem beliefs are ops[0][0][1] #rep beliefs are ops[0][1] q1 = [] q2 = [] q3 = [] q4 = [] q5 = [] q6 = [] q7 = [] q8 = [] q9 = [] q10 = [] q11 = [] for i in range(len(ops)): q1 += ([ops[i][0][1][2], ops[i][0][1][3]], [ops[i][1][2], ops[i][1][3]]) q2 += ([ops[i][0][1][4], ops[i][0][1][5]], [ops[i][1][4], ops[i][1][5]]) q3 += ([ops[i][0][1][6], ops[i][0][1][7]], [ops[i][1][6], ops[i][1][7]]) q4 += ([ops[i][0][1][8], ops[i][0][1][9]], [ops[i][1][8], ops[i][1][9]]) q5 += ([ops[i][0][1][10], ops[i][0][1][11]], [ops[i][1][10], ops[i][1][11]]) q6 += ([ops[i][0][1][12], ops[i][0][1][13]], [ops[i][1][12], ops[i][1][13]]) q7 += ([ops[i][0][1][14], ops[i][0][1][15]], [ops[i][1][14], ops[i][1][15]]) q8 += ([ops[i][0][1][16], ops[i][0][1][17]], [ops[i][1][16], ops[i][1][17]]) q9 += ([ops[i][0][1][18], ops[i][0][1][19]], [ops[i][1][18], ops[i][1][19]]) q10 += ([ops[i][0][1][20], ops[i][0][1][21]], [ops[i][1][20], ops[i][1][21]]) q11 += ([ops[i][0][1][22], ops[i][0][1][23]], [ops[i][1][22], ops[i][1][23]]) k1 = KMeans(n_clusters=2, random_state=0).fit(q1) k2 = KMeans(n_clusters=2, random_state=0).fit(q2) k3 = KMeans(n_clusters=2, random_state=0).fit(q3) k4 = KMeans(n_clusters=2, random_state=0).fit(q4) k5 = KMeans(n_clusters=2, random_state=0).fit(q5) k6 = KMeans(n_clusters=2, random_state=0).fit(q6) k7 = KMeans(n_clusters=2, random_state=0).fit(q7) k8 = KMeans(n_clusters=2, random_state=0).fit(q8) k9 = KMeans(n_clusters=2, random_state=0).fit(q9) k10 = KMeans(n_clusters=2, random_state=0).fit(q10) k11 = KMeans(n_clusters=2, random_state=0).fit(q11) inertias = [ k1.inertia_, k2.inertia_, k3.inertia_, k4.inertia_, k5.inertia_, k6.inertia_, k7.inertia_, k8.inertia_, k9.inertia_, k10.inertia_, k11.inertia_ ] '''questions = ['q1', 'q2', 'q3', 'q4', 'q5', 'q6', 'q7', 'q8', 'q9', 'q_10', 'q_11'] plt.figure() plt.bar(questions, inertias) plt.xlabel('Questions') plt.ylabel('Total distance between agreement') plt.title('Disagreement per question') plt.show()''' inertias = {"disagreement on each question": inertias} questions = { 'Question 1': q1, 'Question 2': q2, 'Question 3': q3, 'Question 4': q4, 'Question 5': q5, 'Question 6': q6, 'Question 7': q7, 'Question 8': q8, 'Question 9': q9, 'Question 10': q10, 'Question 11': q11 } repo.dropCollection("k_means") repo.createCollection("k_means") repo['aheckman_jfimbres.k_means'].insert(inertias) repo.dropCollection("questions") repo.createCollection("questions") repo['aheckman_jfimbres.questions'].insert(questions) repo.logout() endTime = datetime.datetime.now() return {"start": startTime, "end": endTime}