comparison python/safe.py @ 10:73bb35b96624

show good hit output
author Henry S. Thompson <ht@inf.ed.ac.uk>
date Thu, 27 May 2021 06:09:01 -0400
parents 0d1670ab37df
children e411408d64ec
comparison
equal deleted inserted replaced
9:0d1670ab37df 10:73bb35b96624
1 #!/usr/bin/python3 1 #!/usr/bin/python3
2 '''Find a safe landing space (i.e. in air, not above lava) near a cluster''' 2 '''Find a safe landing space (i.e. in air, not above lava) near a cluster'''
3 import sys, math 3 import sys, math
4 4
5 from util import * 5 from util import *
6 from bisect import insort_left as insort 6 from bisect import insort
7 7
8 def usage(): 8 def usage():
9 print("""Usage: safe.py [-d n] 9 print("""Usage: safe.py [-d n] [filename]
10 n is maximum distance of landing to some point in the cluster, default is 10. 10 n is maximum distance of landing to some point in the cluster, default is 10.
11 11
12 Feed stdin with lines from the output of cluster.py, having produced 12 Feed stdin with lines from the output of cluster.py, having produced
13 air.tsv with the same settings as the cluster input, and lava.tsv 13 air.tsv with the same settings as the cluster input, and lava.tsv
14 with a lower-bound on Y of 0""") 14 with a lower-bound on Y of 0""")
15 15
16 n=10.0
16 if len(sys.argv)>1: 17 if len(sys.argv)>1:
17 if sys.argv.pop(1)!='-d': 18 if sys.argv[1]=='-d':
18 usage() 19 sys.argv.pop()
19 exit(1) 20 n=float(sys.argv.pop(1))
20 n=float(sys.argv.pop(1)) 21
22 if len(sys.argv)>1:
23 infile=open(sys.argv.pop(1))
21 else: 24 else:
22 n=10.0 25 infile=sys.stdin
23 26
24 class Block: 27 class Block:
25 '''Shared by Air and Lava, 28 '''Shared by Air and Lava,
26 holds points and columns. 29 holds points and columns.
27 Columns are dictionaries indexed by (x,z) coords, 30 Columns are dictionaries indexed by (x,z) coords,
40 yy=tc[k] 43 yy=tc[k]
41 insort(yy,y) 44 insort(yy,y)
42 except KeyError: 45 except KeyError:
43 tc[k]=[y] 46 tc[k]=[y]
44 # convert to lists of intervals 47 # convert to lists of intervals
45 # Simplest case first
46 for k,yy in tc.items(): 48 for k,yy in tc.items():
47 tyy=[yy] 49 tyy=[yy]
48 ii=[] 50 ii=[]
49 while True: 51 while True:
50 clean=True 52 clean=True
58 ii+=(yy[0],yy[i]) 60 ii+=(yy[0],yy[i])
59 tyy=[yy[i+1:]]+tyy[j+1:] 61 tyy=[yy[i+1:]]+tyy[j+1:]
60 break 62 break
61 if clean: 63 if clean:
62 break 64 break
63 self.columns[k]=ii 65 self.columns[k]=set(ii) # so we can merge later
64 66
65 def readPoints(self,file): 67 def readPoints(self,file):
66 file.readline() 68 file.readline()
67 self.points=[[float(i) for i in l.split('\t')[2].split(',')] for l in file] 69 self.points=[[float(i) for i in l.split('\t')[2].split(',')] for l in file]
68 70
86 88
87 def safety(p): 89 def safety(p):
88 ka=[(a[0],a[2]) for a in A.points if d(a,p)<=n] 90 ka=[(a[0],a[2]) for a in A.points if d(a,p)<=n]
89 return [(k,A.columns[k],L.columns[k]) for k in ka if k in L.columns] 91 return [(k,A.columns[k],L.columns[k]) for k in ka if k in L.columns]
90 92
91 readHeaders(sys.modules['__main__'],sys.stdin,False) 93 readHeaders(sys.modules['__main__'],infile,False)
92 for l in sys.stdin: 94 for l in infile:
93 c=eval(l) 95 c=eval(l)
94 ss=[] 96 ss=[]
95 hits={} 97 hits={}
96 misses=[] 98 misses=[]
97 for p in c: 99 for p in c:
98 s=safety(p) 100 ss=safety(p)
99 if s: 101 if ss:
100 pass 102 for (k,a,l) in ss:
103 try:
104 (aa,ll)=hits[k]
105 aa.update(a)
106 ll.update(l)
107 except KeyError:
108 hits[k]=(a,l)
101 else: 109 else:
102 misses.append(p) 110 misses.append(p)
103 print(c) 111 print(c)
104 print(' %s nearby landing zones'%( 112 if hits:
105 (lambda nm,nc:'No' if nm==nc else nc-nm)(len(misses),len(c)))) 113 print(' %s nearby landing columns'%len(hits))
114 for k in sorted(hits.keys(),key=lambda k:k[0]):
115 aa,ll=hits[k]
116 print("%s: %s %s"%(k,sorted(list(aa)),sorted(list(ll))))
117 else:
118 print(' No nearby landing zones')
119
120 if infile!=sys.stdin:
121 infile.close()