Skip to content

Commit 3a00e30

Browse files
authored
Add files via upload
1 parent 633425e commit 3a00e30

File tree

3 files changed

+81453
-0
lines changed

3 files changed

+81453
-0
lines changed

Netlogo analysis.py

Lines changed: 98 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,98 @@
1+
# RP4 - Agent Based Model Analysis
2+
# Ben Mazin
3+
# 03/26/2024
4+
# School of Interdisciplinary Science, McMaster University
5+
6+
# This script reads in the csv files from the Netlogo model and analyzes the data to determine if the chemo treatment is effective
7+
# It then saves the results to a csv file
8+
9+
import pandas as pd
10+
from scipy import stats
11+
12+
# Set the number of trials and initial hamsters
13+
number_of_trials = 20000
14+
initial_hamsters = 100
15+
16+
# Create an empty DataFrame with 4 columns
17+
all_data = []
18+
19+
# Function to read CSV with inconsistent rows
20+
def read_inconsistent_csv(file_path, delimiter=','):
21+
with open(file_path, 'r') as file:
22+
lines = file.readlines()
23+
24+
# Determine the maximum number of columns by checking all rows
25+
max_columns = max(len(line.split(delimiter)) for line in lines)
26+
27+
# Split each line into columns and pad with None if they are too short
28+
data = [line.strip().split(delimiter) + [None] * (max_columns - len(line.split(delimiter))) for line in lines]
29+
30+
# Convert the list of lists into a DataFrame
31+
df = pd.DataFrame(data, columns=range(max_columns)) # Set index to match column numbers
32+
return df
33+
34+
# Run for number_of_trials
35+
for i in range(1, number_of_trials + 1):
36+
37+
# Read in the csv file
38+
file_path = f'/Users/benmazin/Movies/netlogo output/world{i}.csv'
39+
data = read_inconsistent_csv(file_path, delimiter=',')
40+
41+
# Get the value of ChemoWork
42+
chemowork = data.iat[6, 5]
43+
if chemowork != '"true"' and chemowork != '"false"':
44+
chemowork = data.iat[6, 3]
45+
46+
# Clean up the data
47+
data = data.iloc[18:]
48+
data = data.reset_index(drop=True)
49+
50+
# Get the total dead and max percent infected
51+
total_dead = initial_hamsters - int(data.iloc[-1, 9].replace('"', ''))
52+
max_percent_infect = int(data.iloc[2:, 1].str.replace('"', '').astype(int).max())
53+
54+
# Append the data to the proper list
55+
if chemowork == '"true"':
56+
all_data.append({
57+
'Trial': i,
58+
'ChemoTotalDead': total_dead,
59+
'ChemoPercentInfect': max_percent_infect,
60+
'NoChemoTotalDead': None,
61+
'NoChemoPercentInfect': None
62+
})
63+
64+
elif chemowork == '"false"':
65+
all_data.append({
66+
'Trial': i,
67+
'ChemoTotalDead': None,
68+
'ChemoPercentInfect': None,
69+
'NoChemoTotalDead': total_dead,
70+
'NoChemoPercentInfect': max_percent_infect
71+
})
72+
73+
else:
74+
print("Error: ChemoWork is not true or false")
75+
print(i)
76+
77+
#save dataframe to csv
78+
results = pd.DataFrame(all_data)
79+
80+
# get the mean for each data column
81+
chemo_total_dead_mean = results['ChemoTotalDead'].mean()
82+
chemo_percent_infect_mean = results['ChemoPercentInfect'].mean()
83+
no_chemo_total_dead_mean = results['NoChemoTotalDead'].mean()
84+
no_chemo_percent_infect_mean = results['NoChemoPercentInfect'].mean()
85+
86+
# run a t-test to determine if the means are significantly different for total dead and percent infect
87+
totalDeadTtest = stats.ttest_ind(results['ChemoTotalDead'], results['NoChemoTotalDead'] , nan_policy='omit')
88+
percentInfectTtest = stats.ttest_ind(results['ChemoPercentInfect'], results['NoChemoPercentInfect'], nan_policy='omit')
89+
90+
# create a new dataframe with the means and t-test results
91+
resultSum = pd.DataFrame({
92+
'Category': ['ChemoTotalDeadMean', 'ChemoPercentInfectMean', 'NoChemoTotalDeadMean', 'NoChemoPercentInfectMean', 'TotalDeadTtest', 'PercentInfectTtest'],
93+
'Value': [chemo_total_dead_mean, chemo_percent_infect_mean, no_chemo_total_dead_mean, no_chemo_percent_infect_mean, totalDeadTtest, percentInfectTtest]
94+
})
95+
96+
# save the summary and results to csv
97+
resultSum.to_csv('/Users/benmazin/Movies/netlogo results/summary.csv', index=False)
98+
results.to_csv('/Users/benmazin/Movies/netlogo results/results.csv', index=False)

0 commit comments

Comments
 (0)