-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpostprocess.py
295 lines (254 loc) · 11.7 KB
/
postprocess.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
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
# Filename: postprocess.py
# Authors: christofs, daschloer, hennyu
# Version 0.3.0 (2016-03-20)
"""
POSTPROCESSING OF RAW TMW DATA
"""
import os
from os.path import join
import glob
import pandas as pd
import numpy as np
import re
##############################
# create_mastermatrix #
##############################
def get_metadata(metadatafile):
print("- getting metadata...")
"""Read metadata file and create DataFrame."""
metadata = pd.DataFrame.from_csv(metadatafile, header=0, sep=",")
#print("metadata\n", metadata)
return metadata
def get_topicscores(topics_in_texts, numOfTopics, version):
"""Create a matrix of segments x topics, with topic score values, from Mallet output."""
print("- getting topicscores...")
if version == "207":
print("(Warning: With Mallet 2.0.7 output, this is very memory-intensive.)")
## Load Mallet 2.0.7 output (strange format)
topicsintexts = pd.read_csv(topics_in_texts, header=None, skiprows=[0], sep="\t", index_col=0)
#topicsintexts = topicsintexts.iloc[0:100,] ### For testing only!!
#print("topicsintexts\n", topicsintexts.head())
listofsegmentscores = []
idnos = []
i = -1
## For each row, collect segment and idno
for row_index, row in topicsintexts.iterrows():
segment = row[1][-15:-4]
idno = row[1][-15:-11]
#print(segment, idno)
idnos.append(idno)
topics = []
scores = []
## For each segment, get the topic number and its score
i +=1
for j in range(1,numOfTopics,2):
k = j+1
topic = topicsintexts.iloc[i,j]
score = topicsintexts.iloc[i,k]
#score = round(score, 4) ## round off for smaller file.
topics.append(topic)
scores.append(score)
## Create dictionary of topics and scores for one segment
persegment = dict(zip(topics, scores))
segmentscores = pd.DataFrame.from_dict(persegment, orient="index")
segmentscores.columns = [segment]
segmentscores = segmentscores.T
listofsegmentscores.append(segmentscores)
## Putting it all together
topicscores = pd.concat(listofsegmentscores)
topicscores["segmentID"] = topicscores.index
topicscores.fillna(0,inplace=True)
print("topicscores\n", topicscores.head())
return topicscores
if version == "208+":
## Load Mallet output (new, not so strange format)
header = list(range(0,numOfTopics)) ## for use as column headers in dataframe
header = ["segmentID"] + header
#print(header)
topicsintexts = pd.read_csv(topics_in_texts, header=None, sep="\t", index_col=0)
topicsintexts.columns = header
#print(topicsintexts)
##topicsintexts = topicsintexts.iloc[0:100,] ### For testing only!!
if "§" in topicsintexts.iloc[0,0]:
segmentIDs = topicsintexts.iloc[:,0].str[-15:-4]
else:
segmentIDs = topicsintexts.iloc[:,0].str[-10:-4]
topicsintexts["segmentID"] = segmentIDs
topicscores = topicsintexts
#print("topicscores\n", topicscores.head())
return topicscores
def get_docmatrix(corpuspath):
"""Create a matrix containing segments with their idnos."""
print("- getting docmatrix...")
## Create dataframe with filenames of segments and corresponding idnos.
segs = []
idnos = []
for file in glob.glob(corpuspath):
seg,ext = os.path.basename(file).split(".")
segs.append(seg)
idno = seg[0:6]
idnos.append(idno)
docmatrix = pd.DataFrame(segs)
docmatrix["idno"] = idnos
docmatrix.rename(columns={0:"segmentID"}, inplace=True)
#print("docmatrix\n", docmatrix)
return docmatrix
def merge_data(corpuspath, metadatafile, topics_in_texts, mastermatrixfile,
numOfTopics, version):
"""Merges the three dataframes into one mastermatrix."""
print("- getting data...")
## Get all necessary data.
metadata = get_metadata(metadatafile)
docmatrix = get_docmatrix(corpuspath)
topicscores = get_topicscores(topics_in_texts, numOfTopics, version)
## For inspection only.
##print("Metadata\n", metadata.head())
##print("Docmatrix\n", docmatrix.head())
##print("topicscores\n", topicscores.head())
print("- merging data...")
## Merge metadata and docmatrix, matching each segment to its metadata.
if not("idno" in metadata.columns):
metadata["idno"] = metadata.index
mastermatrix = pd.merge(docmatrix, metadata, how="inner", on="idno")
#print("mastermatrix: metadata and docmatrix\n", mastermatrix)
## Merge mastermatrix and topicscores, matching each segment to its topic scores.
#print(mastermatrix.columns)
#print(topicscores.columns)
#print(topicscores)
mastermatrix = pd.merge(mastermatrix, topicscores, on="segmentID", how="inner")
#print("mastermatrix: all three\n", mastermatrix.head())
return mastermatrix
def add_binData(mastermatrix, binDataFile):
print("- adding bin data...")
## Read the information about bins
binData = pd.read_csv(binDataFile, sep=",")
#print(binData)
## Merge existing mastermatrix and binData.
mastermatrix = pd.merge(mastermatrix, binData, how="inner", on="segmentID")
#print(mastermatrix)
return mastermatrix
def create_mastermatrix(corpuspath, outfolder, mastermatrixfile, metadatafile,
topics_in_texts, numOfTopics, useBins, binDataFile, version):
"""Builds the mastermatrix uniting all information about texts and topic scores."""
print("\nLaunched create_mastermatrix.")
if not os.path.exists(outfolder):
os.makedirs(outfolder)
mastermatrix = merge_data(corpuspath, metadatafile, topics_in_texts,
mastermatrixfile, numOfTopics, version)
if useBins == True:
mastermatrix = add_binData(mastermatrix, binDataFile)
mastermatrix.to_csv(join(outfolder, mastermatrixfile), sep=",", encoding="utf-8")
print("Done. Saved mastermatrix. Segments and columns:", mastermatrix.shape)
################################
# calculate_averageTopicScores #
################################
def calculate_averageTopicScores(mastermatrixfile, targets, outfolder):
"""Function to calculate average topic scores based on the mastermatrix."""
print("\nLaunched calculate_averageTopicScores.")
if not os.path.exists(outfolder):
os.makedirs(outfolder)
with open(mastermatrixfile, "r") as infile:
mastermatrix = pd.DataFrame.from_csv(infile, header=0, sep=",")
## Calculate average topic scores for each target category
for target in targets:
grouped = mastermatrix.groupby(target, axis=0)
avg_topicscores = grouped.agg(np.mean)
for col in avg_topicscores.columns:
if not(re.match("\d+", col)):
avg_topicscores = avg_topicscores.drop([col], axis=1)
#avg_topicscores = grouped.agg(np.median)
#print(avg_topicscores)
#if target != "pub-year":
# avg_topicscores = avg_topicscores.drop(["pub-year"], axis=1)
#if target != "binID":
# avg_topicscores = avg_topicscores.drop(["binID"], axis=1)
#avg_topicscores = avg_topicscores.drop(["tei"], axis=1)
## Save grouped averages to CSV file for visualization.
resultfilename = "avgtopicscores_by-"+target+".csv"
resultfilepath = join(outfolder, resultfilename)
## TODO: Some reformatting here, or adapt make_heatmaps.
avg_topicscores.to_csv(resultfilepath, sep=",", encoding="utf-8")
print(" Saved average topic scores for:", target)
print("Done.")
################################
# complexAverageTopicScores #
################################
def calculate_complexAverageTopicScores(mastermatrixfile, targets, outfolder):
"""Function to calculate average topic scores based on the mastermatrix."""
print("\nLaunched calculate_complexAverageTopicScores.")
if not os.path.exists(outfolder):
os.makedirs(outfolder)
with open(mastermatrixfile, "r", encoding="utf-8") as infile:
mastermatrix = pd.DataFrame.from_csv(infile, header=0, sep=",")
## Calculate average topic scores for each target category
grouped = mastermatrix.groupby(targets, axis=0)
avg_topicscores = grouped.agg(np.mean)
for col in avg_topicscores.columns:
if not(re.match("\d+", col)):
avg_topicscores = avg_topicscores.drop([col], axis=1)
#if "year" not in targets:
# avg_topicscores = avg_topicscores.drop(["year"], axis=1)
#if "binID" not in targets:
# avg_topicscores = avg_topicscores.drop(["binID"], axis=1)
#print(avg_topicscores)
## Save grouped averages to CSV file for visualization.
identifierstring = '+'.join(map(str, targets))
resultfilename = "complex-avgtopicscores_by-"+identifierstring+".csv"
resultfilepath = join(outfolder, resultfilename)
avg_topicscores.to_csv(resultfilepath, sep=",", encoding="utf-8")
print("Done. Saved average topic scores for: "+identifierstring)
#################################
# save_firstWords #
#################################
def save_firstWords(topicWordFile, outfolder, filename):
"""Save a table of topics with their three most important words for each topic."""
print("Launched save_someFirstWords.")
with open(topicWordFile, "r", encoding="utf-8") as infile:
firstWords = {}
topicWords = pd.read_csv(infile, sep="\t", header=None)
topicWords = topicWords.drop(1, axis=1)
topicWords = topicWords.iloc[:,1:2]
topics = topicWords.index.tolist()
words = []
for topic in topics:
topic = int(topic)
row = topicWords.loc[topic]
row = row[2].split(" ")
row = str(row[0]+"-"+row[1]+"-"+row[2]+" ("+str(topic)+")")
words.append(row)
firstWords = dict(zip(topics, words))
firstWordsSeries = pd.Series(firstWords, name="firstWords")
#firstWordsSeries.index.name = "topic"
#firstWordsSeries = firstWordsSeries.rename(columns = {'two':'new_name'})
firstWordsSeries.reindex_axis(["firstwords"])
#print(firstWordsSeries)
## Saving the file.
if not os.path.exists(outfolder):
os.makedirs(outfolder)
outfile = join(outfolder, filename)
with open(outfile, "w", encoding="utf-8") as outfile:
firstWordsSeries.to_csv(outfile)
print("Done.")
#################################
# save_topicRanks #
#################################
def save_topicRanks(topicWordFile, outfolder, filename):
"""Save a list of topics with their rank by topic score."""
print("Launched save_topicRanks.")
with open(topicWordFile, "r", encoding="utf-8") as infile:
topicRanks = pd.read_csv(infile, sep="\t", header=None)
topicRanks = topicRanks.drop(2, axis=1)
topicRanks.rename(columns={0:"Number"}, inplace=True)
topicRanks.rename(columns={1:"Score"}, inplace=True)
#topicRanks.sort(columns=["Score"], ascending=False, inplace=True)
topicRanks["Rank"] = topicRanks["Score"].rank(ascending=False)
#print(topicRanks.head())
## Saving the file.
if not os.path.exists(outfolder):
os.makedirs(outfolder)
outfile = join(outfolder, filename)
with open(outfile, "w", encoding="utf-8") as outfile:
topicRanks.to_csv(outfile)
print("Done.")