forked from paulodder/QuantifierComplexity
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathadjust_csv.py
241 lines (207 loc) · 8.02 KB
/
adjust_csv.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
'''
This file is part of QuantifierComplexity.
'''
import argparse
import os
from pathlib import Path
import dotenv
import pandas as pd
import numpy as np
import scipy.stats as stats
import utils
# Load environment variables from .env (which is in same dir as src).
# Don't forget to set "PROJECT_DIR" in .env to the name of the location
# from which you are running current source code.
dotenv.load_dotenv(dotenv.find_dotenv())
# Set paths to relevant directories.
PROJECT_DIR = Path(os.getenv("PROJECT_DIR"))
RESULTS_DIR_RELATIVE = os.getenv("RESULTS_DIR_RELATIVE")
RESULTS_DIR = PROJECT_DIR / RESULTS_DIR_RELATIVE
def parse_args():
'''Create argument parser, add arguments, return parsed args.'''
parser = argparse.ArgumentParser()
parser.add_argument(
"--max_expr_len",
"-l",
type=int,
default=MAX_EXPR_LEN,
help="Generate expressions up to this length",
)
parser.add_argument(
"--max_model_size",
"-m",
type=int,
default=MAX_MODEL_SIZE,
help="Models up to this size will be used to evaluate the meaning " + \
"of statements",
)
parser.add_argument(
"--language_name",
"-j",
type=str,
default=LANGUAGE_NAME,
help="Name of json file (when adding .sjon) that specifies " + \
"settings",
)
parser.add_argument(
"--dest_dir",
"-d",
type=str,
default=RESULTS_DIR,
help="Dir to write results to",
)
parser.add_argument(
"--lang_gen_date",
"-g",
type=str,
default=LANG_GEN_DATE,
help="Date of language generation. Used to load the right csv file. ",
)
parser.add_argument(
"--csv_date",
"-c",
type=str,
default=CSV_DATE,
help="Date of csv file creation. Used to load the right csv file. ",
)
return parser.parse_args()
def shuff_and_standardize_ml(data: pd.DataFrame, verbose=False):
'''Add zcores and randomly shuffle ml scores.
"ml" := minimal expression length. Add 3 columns to dataframe:
Column "ml_shuffled": randomly shuffled values of expr_length.
Column "ml_zscore": zscores of expr_length (normalization).
Column "ml_shuff_zscore": zscores of the shuffled expr_length.
Args:
data: A pandas DataFrame with language data.
verbose: True or False. When True, print old and new
columns, to check.
'''
if verbose:
print(data[["expression", "expr_length"]])
print("==========================================")
data["ml_shuffled"] = \
data["expr_length"].sample(frac=1).reset_index(drop=True)
if verbose:
print(data[["expression", "expr_length", "ml_shuffled"]])
data["ml_zscore"] = stats.zscore(data["expr_length"])
data["ml_shuff_zscore"] = stats.zscore(data["ml_shuffled"])
if verbose:
print(data[["ml_shuffled", "ml_shuff_zscore"]])
def shuff_and_standardize_lz(data: pd.DataFrame, verbose=False):
'''Add zcores and randomly shuffled lz scores.
"lz" := minimal expression length. Add 3 columns to dataframe:
Column "lz_shuffled": randomly shuffled values of lempel_ziv.
Column "lz_zscore": zscores of lempel_ziv (normalization).
Column "lz_shuff_zscore": zscores of the shuffled lempel_ziv.
Args:
data: A pandas DataFrame with language data.
verbose: True or False. When True, print old and new
columns, to check.
'''
if verbose:
print(data[["expression", "lempel_ziv_0"]])
print("==========================================")
data["lz_0_shuffled"] = \
data["lempel_ziv_0"].sample(frac=1).reset_index(drop=True)
if verbose:
print(data[["expression", "lempel_ziv_0", "lz_shuffled"]])
data["lz_0_zscore"] = stats.zscore(data["lempel_ziv_0"])
data["lz_0_shuff_zscore"] = \
data["lz_0_zscore"].sample(frac=1).reset_index(drop=True)
data["lz_1_zscore"] = stats.zscore(data["lempel_ziv_1"])
data["lz_1_shuff_zscore"] = \
data["lz_1_zscore"].sample(frac=1).reset_index(drop=True)
data["lz_2_zscore"] = stats.zscore(data["lempel_ziv_2"])
data["lz_2_shuff_zscore"] = \
data["lz_2_zscore"].sample(frac=1).reset_index(drop=True)
data["lz_mean_zscore"] = stats.zscore(data["lempel_ziv_mean"])
data["lz_mean_shuff_zscore"] = \
data["lz_mean_zscore"].sample(frac=1).reset_index(drop=True)
if verbose:
print(data[["lz_shuffled", "lz_shuff_zscore"]])
def mon_quan_cons(row):
'''Return combined property value: 1 iff all props are 1.
Args: row: A row from pandas dataframe with language data.
'''
if (row["monotonicity"] == 1) & (row["quantity"] == 1) \
& (row["conservativity"] == 1):
return 1
else:
return 0
if __name__ == "__main__":
# Default values for argparse args.
LANGUAGE_NAME = "Logical_index" # "Logical_index" # "Logical"
MAX_EXPR_LEN = 5 # 5 for Logical_index # 7 for Logical
MAX_MODEL_SIZE = 8 # 8
LANG_GEN_DATE = "2022-03-11" # "2022-03-11" # "2020-12-25"
CSV_DATE = "2022-03-14" # "2022-03-14" # "2021-01-16"
args = parse_args()
# Set DataFrame print options.
# pd.set_option("display.max_rows", None)
pd.set_option("display.max_columns", None)
# pd.set_option("display.width", None)
# pd.set_option("display.max_colwidth", None)
data = utils.load_csv_data(
args.max_model_size, args.max_expr_len, args.language_name,
args.lang_gen_date, args.csv_date
)
# Compute binary properties based on graded properties.
for prop in ["monotonicity", "quantity", "conservativity"]:
# Rename column name of graded scores: prop --> g_prop.
data.rename(columns={prop:'g_' + prop[0:4]}, inplace=True)
# Add column with binary scores under original name: prop.
# Prop == 1 iff g_prop == 1.0, and prop == 0 otherwise.
data[prop] = np.where(data['g_' + prop[0:4]] == 1.0, 1, 0)
if "index" in args.language_name:
# For language data with index operator.
data["mon_quan_cons"] = data.apply(
lambda row: mon_quan_cons(row), axis=1
)
else:
# For language data without index operator. Quantity is
# alway 1, therefore not explicitly mentioned.
data["mon_cons"] = data.apply(
lambda row: mon_quan_cons(row), axis=1
)
shuff_and_standardize_lz(data, verbose=False)
shuff_and_standardize_ml(data, verbose=False)
# Uniformity shuff and zscores.
data["uniformity_zscore"] = stats.zscore(data["uniformity"])
data["uniformity_shuff"] = \
data["uniformity"].sample(frac=1).reset_index(drop=True)
data["uniformity_shuff_zscore"] = stats.zscore(data["uniformity_shuff"])
# Store adjusted DataFrame as csv.
utils.store_language_data_to_csv(
data, args.max_model_size, args.max_expr_len,
args.language_name, args.lang_gen_date, verbose=True
)
check_01 = data["lempel_ziv_0"] == data["lempel_ziv_1"]
check_12 = data["lempel_ziv_1"] == data["lempel_ziv_2"]
check_20 = data["lempel_ziv_2"] == data["lempel_ziv_0"]
check_mean_0 = data["lempel_ziv_0"] == data["lempel_ziv_mean"]
check_mean_1 = data["lempel_ziv_1"] == data["lempel_ziv_mean"]
check_mean_2 = data["lempel_ziv_2"] == data["lempel_ziv_mean"]
print(
"\ncheck_01 = data[lempel_ziv_0] == data[lempel_ziv_1] =",
check_01.all()
)
print(
"\ncheck_12 = data[lempel_ziv_1] == data[lempel_ziv_2] =",
check_12.all()
)
print(
"\ncheck_20 = data[lempel_ziv_2] == data[lempel_ziv_0] =",
check_20.all()
)
print(
"\ncheck_mean_0 = data[lempel_ziv_0] == data[lempel_ziv_mean] =",
check_mean_0.all()
)
print(
"\ncheck_mean_1 = data[lempel_ziv_1] == data[lempel_ziv_mean] =",
check_mean_1.all()
)
print(
"\ncheck_mean_2 = data[lempel_ziv_1] == data[lempel_ziv_mean] =",
check_mean_2.all(), "\n"
)