swdata/cluster_stat.py
2018-11-19 13:04:19 +08:00

53 lines
1.6 KiB
Python

"""
统计所有的连通子图规模
"""
import csv
import numpy as np
from collections import Counter
from island.matches import Matches
from island.match import Match
class ClusterStat:
def __init__(self):
self.detail = {}
# self.matches = Matches.from_profile_expr(lambda r: True)
self.matches = Matches.from_profile('SURVIVE')
def stat(self):
for m in self.matches.data:
cluster = {}
no = 0
for r in m.query('player','join').raw_data:
cluster[r['pid']] = no
no += 1
for r in m.query('action', 'done').raw_data:
if cluster[r['a']] != cluster[r['b']]:
if cluster[r['a']] < cluster[r['b']]:
cmax, cmin = cluster[r['b']], cluster[r['a']]
else:
cmax, cmin = cluster[r['a']], cluster[r['b']]
lmax = [k for (k,v) in cluster.items() if v == cmax]
for k in lmax:
cluster[k] = cmin
res = [v for v in Counter(cluster.values()).values()]
# print(m.name, list(sorted(res, reverse=True)))
self.detail[m.name] = res
def output(self):
data = []
for r in self.detail.values():
data.extend(r)
res = np.zeros(max(data))
s = sum(data)
for d in data:
res[d-1] += d
res /= s
print(res)
np.save('outputs/cluster.npy', res)
if __name__ == '__main__':
c = ClusterStat()
c.stat()
c.output()