annotate python/cluster.py @ 13:1cd5c7952aaa default tip

fix failure to read first line of Air/Lava, keep me from swimming in Lava, again!
author Henry S. Thompson <ht@inf.ed.ac.uk>
date Sun, 30 Jan 2022 14:49:33 -0500
parents 1e42c0147a49
children
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
4
56508a6033a9 minutor chunk hacking
Henry S. Thompson <ht@inf.ed.ac.uk>
parents:
diff changeset
1 #!/usr/bin/python3
56508a6033a9 minutor chunk hacking
Henry S. Thompson <ht@inf.ed.ac.uk>
parents:
diff changeset
2 '''Read a minutor block lockation tsv and resort it to show clusters'''
56508a6033a9 minutor chunk hacking
Henry S. Thompson <ht@inf.ed.ac.uk>
parents:
diff changeset
3 import sys
56508a6033a9 minutor chunk hacking
Henry S. Thompson <ht@inf.ed.ac.uk>
parents:
diff changeset
4
56508a6033a9 minutor chunk hacking
Henry S. Thompson <ht@inf.ed.ac.uk>
parents:
diff changeset
5 from util import *
56508a6033a9 minutor chunk hacking
Henry S. Thompson <ht@inf.ed.ac.uk>
parents:
diff changeset
6
56508a6033a9 minutor chunk hacking
Henry S. Thompson <ht@inf.ed.ac.uk>
parents:
diff changeset
7 if len(sys.argv)==1 or sys.argv[1]=='-h':
7
3ee329b129c6 rename block properties, slightly revise result format
Henry S. Thompson <ht@inf.ed.ac.uk>
parents: 5
diff changeset
8 print("""Usage: cluster.py [-h] [-c n] [-s] infile.tsv [outfile.tsv]
4
56508a6033a9 minutor chunk hacking
Henry S. Thompson <ht@inf.ed.ac.uk>
parents:
diff changeset
9 n is cluster diameter, default is 5
7
3ee329b129c6 rename block properties, slightly revise result format
Henry S. Thompson <ht@inf.ed.ac.uk>
parents: 5
diff changeset
10 -s for strict circle distance, reduces number of candidates
4
56508a6033a9 minutor chunk hacking
Henry S. Thompson <ht@inf.ed.ac.uk>
parents:
diff changeset
11 default outfile is [infile]_c[n].tsv""")
56508a6033a9 minutor chunk hacking
Henry S. Thompson <ht@inf.ed.ac.uk>
parents:
diff changeset
12 exit(1)
56508a6033a9 minutor chunk hacking
Henry S. Thompson <ht@inf.ed.ac.uk>
parents:
diff changeset
13 if sys.argv[1]=='-c':
56508a6033a9 minutor chunk hacking
Henry S. Thompson <ht@inf.ed.ac.uk>
parents:
diff changeset
14 sys.argv.pop(1)
56508a6033a9 minutor chunk hacking
Henry S. Thompson <ht@inf.ed.ac.uk>
parents:
diff changeset
15 n=float(sys.argv.pop(1))
56508a6033a9 minutor chunk hacking
Henry S. Thompson <ht@inf.ed.ac.uk>
parents:
diff changeset
16 else:
56508a6033a9 minutor chunk hacking
Henry S. Thompson <ht@inf.ed.ac.uk>
parents:
diff changeset
17 n=5.0
56508a6033a9 minutor chunk hacking
Henry S. Thompson <ht@inf.ed.ac.uk>
parents:
diff changeset
18
7
3ee329b129c6 rename block properties, slightly revise result format
Henry S. Thompson <ht@inf.ed.ac.uk>
parents: 5
diff changeset
19 if sys.argv[1]=='-s':
3ee329b129c6 rename block properties, slightly revise result format
Henry S. Thompson <ht@inf.ed.ac.uk>
parents: 5
diff changeset
20 sys.argv.pop(1)
3ee329b129c6 rename block properties, slightly revise result format
Henry S. Thompson <ht@inf.ed.ac.uk>
parents: 5
diff changeset
21 strict=True
3ee329b129c6 rename block properties, slightly revise result format
Henry S. Thompson <ht@inf.ed.ac.uk>
parents: 5
diff changeset
22 else:
3ee329b129c6 rename block properties, slightly revise result format
Henry S. Thompson <ht@inf.ed.ac.uk>
parents: 5
diff changeset
23 strict=False
3ee329b129c6 rename block properties, slightly revise result format
Henry S. Thompson <ht@inf.ed.ac.uk>
parents: 5
diff changeset
24
4
56508a6033a9 minutor chunk hacking
Henry S. Thompson <ht@inf.ed.ac.uk>
parents:
diff changeset
25 infile_name=sys.argv.pop(1)
56508a6033a9 minutor chunk hacking
Henry S. Thompson <ht@inf.ed.ac.uk>
parents:
diff changeset
26 if len(sys.argv)>1:
56508a6033a9 minutor chunk hacking
Henry S. Thompson <ht@inf.ed.ac.uk>
parents:
diff changeset
27 outfile_name=sys.argv.pop(1)
56508a6033a9 minutor chunk hacking
Henry S. Thompson <ht@inf.ed.ac.uk>
parents:
diff changeset
28 else:
56508a6033a9 minutor chunk hacking
Henry S. Thompson <ht@inf.ed.ac.uk>
parents:
diff changeset
29 outfile_name="%s_c%s.tsv"%(infile_name.split('.')[0],n)
56508a6033a9 minutor chunk hacking
Henry S. Thompson <ht@inf.ed.ac.uk>
parents:
diff changeset
30
56508a6033a9 minutor chunk hacking
Henry S. Thompson <ht@inf.ed.ac.uk>
parents:
diff changeset
31 cc=[]
56508a6033a9 minutor chunk hacking
Henry S. Thompson <ht@inf.ed.ac.uk>
parents:
diff changeset
32
56508a6033a9 minutor chunk hacking
Henry S. Thompson <ht@inf.ed.ac.uk>
parents:
diff changeset
33 with open(infile_name,'r') as infile:
56508a6033a9 minutor chunk hacking
Henry S. Thompson <ht@inf.ed.ac.uk>
parents:
diff changeset
34 with open(outfile_name,'w') as outfile:
56508a6033a9 minutor chunk hacking
Henry S. Thompson <ht@inf.ed.ac.uk>
parents:
diff changeset
35 l=infile.readline().rstrip()
56508a6033a9 minutor chunk hacking
Henry S. Thompson <ht@inf.ed.ac.uk>
parents:
diff changeset
36 print(l,file=outfile)
56508a6033a9 minutor chunk hacking
Henry S. Thompson <ht@inf.ed.ac.uk>
parents:
diff changeset
37 ff=PPAT.split(l)
56508a6033a9 minutor chunk hacking
Henry S. Thompson <ht@inf.ed.ac.uk>
parents:
diff changeset
38 (nr,ox,oy,oz)=intsMaybe(ff)
5
672621ab4db4 trim points we check to a circle per radius
Henry S. Thompson <ht@inf.ed.ac.uk>
parents: 4
diff changeset
39 home=(float(ox),float(oy),float(oz))
4
56508a6033a9 minutor chunk hacking
Henry S. Thompson <ht@inf.ed.ac.uk>
parents:
diff changeset
40 et=ff[9]
56508a6033a9 minutor chunk hacking
Henry S. Thompson <ht@inf.ed.ac.uk>
parents:
diff changeset
41 l=infile.readline().rstrip()
56508a6033a9 minutor chunk hacking
Henry S. Thompson <ht@inf.ed.ac.uk>
parents:
diff changeset
42 print(l,file=outfile)
11
e411408d64ec from python repo
Henry S. Thompson <ht@inf.ed.ac.uk>
parents: 7
diff changeset
43 l2=PPAT.split(l)
e411408d64ec from python repo
Henry S. Thompson <ht@inf.ed.ac.uk>
parents: 7
diff changeset
44 if l2[-1]=='bounded':
e411408d64ec from python repo
Henry S. Thompson <ht@inf.ed.ac.uk>
parents: 7
diff changeset
45 orad=int(l2[1])
e411408d64ec from python repo
Henry S. Thompson <ht@inf.ed.ac.uk>
parents: 7
diff changeset
46 ymin=ymax=-1
e411408d64ec from python repo
Henry S. Thompson <ht@inf.ed.ac.uk>
parents: 7
diff changeset
47 else:
e411408d64ec from python repo
Henry S. Thompson <ht@inf.ed.ac.uk>
parents: 7
diff changeset
48 (orad,ymin,ymax)=intsMaybe(l2)
4
56508a6033a9 minutor chunk hacking
Henry S. Thompson <ht@inf.ed.ac.uk>
parents:
diff changeset
49 _=infile.readline()
56508a6033a9 minutor chunk hacking
Henry S. Thompson <ht@inf.ed.ac.uk>
parents:
diff changeset
50 for l in infile:
56508a6033a9 minutor chunk hacking
Henry S. Thompson <ht@inf.ed.ac.uk>
parents:
diff changeset
51 found=False
5
672621ab4db4 trim points we check to a circle per radius
Henry S. Thompson <ht@inf.ed.ac.uk>
parents: 4
diff changeset
52 (_,td,qd)=l.rstrip().split('\t')
12
1e42c0147a49 minutor changed file format for minecraft 1.18
Henry S. Thompson <ht@inf.ed.ac.uk>
parents: 11
diff changeset
53 q=[float(i) for i in qd.split('/')]
5
672621ab4db4 trim points we check to a circle per radius
Henry S. Thompson <ht@inf.ed.ac.uk>
parents: 4
diff changeset
54 td=float(td)
7
3ee329b129c6 rename block properties, slightly revise result format
Henry S. Thompson <ht@inf.ed.ac.uk>
parents: 5
diff changeset
55 if strict and td>orad:
5
672621ab4db4 trim points we check to a circle per radius
Henry S. Thompson <ht@inf.ed.ac.uk>
parents: 4
diff changeset
56 # Enforce a circular region, not square
7
3ee329b129c6 rename block properties, slightly revise result format
Henry S. Thompson <ht@inf.ed.ac.uk>
parents: 5
diff changeset
57 print(td,orad,file=sys.stderr)
5
672621ab4db4 trim points we check to a circle per radius
Henry S. Thompson <ht@inf.ed.ac.uk>
parents: 4
diff changeset
58 break
4
56508a6033a9 minutor chunk hacking
Henry S. Thompson <ht@inf.ed.ac.uk>
parents:
diff changeset
59 for c in cc:
56508a6033a9 minutor chunk hacking
Henry S. Thompson <ht@inf.ed.ac.uk>
parents:
diff changeset
60 for p in c:
56508a6033a9 minutor chunk hacking
Henry S. Thompson <ht@inf.ed.ac.uk>
parents:
diff changeset
61 if d(p,q)<=n:
56508a6033a9 minutor chunk hacking
Henry S. Thompson <ht@inf.ed.ac.uk>
parents:
diff changeset
62 c.append(q)
56508a6033a9 minutor chunk hacking
Henry S. Thompson <ht@inf.ed.ac.uk>
parents:
diff changeset
63 found=True
56508a6033a9 minutor chunk hacking
Henry S. Thompson <ht@inf.ed.ac.uk>
parents:
diff changeset
64 break
56508a6033a9 minutor chunk hacking
Henry S. Thompson <ht@inf.ed.ac.uk>
parents:
diff changeset
65 if found:
56508a6033a9 minutor chunk hacking
Henry S. Thompson <ht@inf.ed.ac.uk>
parents:
diff changeset
66 break
56508a6033a9 minutor chunk hacking
Henry S. Thompson <ht@inf.ed.ac.uk>
parents:
diff changeset
67 if not found:
56508a6033a9 minutor chunk hacking
Henry S. Thompson <ht@inf.ed.ac.uk>
parents:
diff changeset
68 cc.append([q])
56508a6033a9 minutor chunk hacking
Henry S. Thompson <ht@inf.ed.ac.uk>
parents:
diff changeset
69 oc=cc
56508a6033a9 minutor chunk hacking
Henry S. Thompson <ht@inf.ed.ac.uk>
parents:
diff changeset
70 cc=[] # lose
56508a6033a9 minutor chunk hacking
Henry S. Thompson <ht@inf.ed.ac.uk>
parents:
diff changeset
71 w=0
56508a6033a9 minutor chunk hacking
Henry S. Thompson <ht@inf.ed.ac.uk>
parents:
diff changeset
72 ow=-1
56508a6033a9 minutor chunk hacking
Henry S. Thompson <ht@inf.ed.ac.uk>
parents:
diff changeset
73 nc=[] # win
56508a6033a9 minutor chunk hacking
Henry S. Thompson <ht@inf.ed.ac.uk>
parents:
diff changeset
74 while True:
56508a6033a9 minutor chunk hacking
Henry S. Thompson <ht@inf.ed.ac.uk>
parents:
diff changeset
75 for i,c in enumerate(oc):
56508a6033a9 minutor chunk hacking
Henry S. Thompson <ht@inf.ed.ac.uk>
parents:
diff changeset
76 win=False
56508a6033a9 minutor chunk hacking
Henry S. Thompson <ht@inf.ed.ac.uk>
parents:
diff changeset
77 for p in c:
56508a6033a9 minutor chunk hacking
Henry S. Thompson <ht@inf.ed.ac.uk>
parents:
diff changeset
78 for g in oc[i+1:]:
56508a6033a9 minutor chunk hacking
Henry S. Thompson <ht@inf.ed.ac.uk>
parents:
diff changeset
79 for q in g:
56508a6033a9 minutor chunk hacking
Henry S. Thompson <ht@inf.ed.ac.uk>
parents:
diff changeset
80 if d(p,q)<=n:
56508a6033a9 minutor chunk hacking
Henry S. Thompson <ht@inf.ed.ac.uk>
parents:
diff changeset
81 win=True
56508a6033a9 minutor chunk hacking
Henry S. Thompson <ht@inf.ed.ac.uk>
parents:
diff changeset
82 w+=1
56508a6033a9 minutor chunk hacking
Henry S. Thompson <ht@inf.ed.ac.uk>
parents:
diff changeset
83 nc.append(c+g)
56508a6033a9 minutor chunk hacking
Henry S. Thompson <ht@inf.ed.ac.uk>
parents:
diff changeset
84 break
56508a6033a9 minutor chunk hacking
Henry S. Thompson <ht@inf.ed.ac.uk>
parents:
diff changeset
85 if win:
56508a6033a9 minutor chunk hacking
Henry S. Thompson <ht@inf.ed.ac.uk>
parents:
diff changeset
86 break
56508a6033a9 minutor chunk hacking
Henry S. Thompson <ht@inf.ed.ac.uk>
parents:
diff changeset
87 if win:
56508a6033a9 minutor chunk hacking
Henry S. Thompson <ht@inf.ed.ac.uk>
parents:
diff changeset
88 break
56508a6033a9 minutor chunk hacking
Henry S. Thompson <ht@inf.ed.ac.uk>
parents:
diff changeset
89 if not win:
56508a6033a9 minutor chunk hacking
Henry S. Thompson <ht@inf.ed.ac.uk>
parents:
diff changeset
90 cc.append(c)
56508a6033a9 minutor chunk hacking
Henry S. Thompson <ht@inf.ed.ac.uk>
parents:
diff changeset
91 print(len(cc),len(nc),ow,w,file=sys.stderr)
56508a6033a9 minutor chunk hacking
Henry S. Thompson <ht@inf.ed.ac.uk>
parents:
diff changeset
92 if ow==w:
56508a6033a9 minutor chunk hacking
Henry S. Thompson <ht@inf.ed.ac.uk>
parents:
diff changeset
93 break
56508a6033a9 minutor chunk hacking
Henry S. Thompson <ht@inf.ed.ac.uk>
parents:
diff changeset
94 ow=w
56508a6033a9 minutor chunk hacking
Henry S. Thompson <ht@inf.ed.ac.uk>
parents:
diff changeset
95 oc=nc
56508a6033a9 minutor chunk hacking
Henry S. Thompson <ht@inf.ed.ac.uk>
parents:
diff changeset
96 nc=[]
56508a6033a9 minutor chunk hacking
Henry S. Thompson <ht@inf.ed.ac.uk>
parents:
diff changeset
97 for c in sorted(cc,reverse=True,key=lambda x:len(x)):
56508a6033a9 minutor chunk hacking
Henry S. Thompson <ht@inf.ed.ac.uk>
parents:
diff changeset
98 print(c,file=outfile)
56508a6033a9 minutor chunk hacking
Henry S. Thompson <ht@inf.ed.ac.uk>
parents:
diff changeset
99
56508a6033a9 minutor chunk hacking
Henry S. Thompson <ht@inf.ed.ac.uk>
parents:
diff changeset
100
56508a6033a9 minutor chunk hacking
Henry S. Thompson <ht@inf.ed.ac.uk>
parents:
diff changeset
101
56508a6033a9 minutor chunk hacking
Henry S. Thompson <ht@inf.ed.ac.uk>
parents:
diff changeset
102