33import xml .dom .minidom as dom
44import argparse
55import math
6+ import parseComposite
67
78class Composite :
8- def __init__ (self , xmin = None , xmax = None , sense = [], anti = []):
9+ def __init__ (self , xmin = None , xmax = None , sense = [], anti = [], id = "" ):
910 self .xmin = xmin
1011 self .xmax = xmax
1112 self .sense = sense
1213 self .anti = anti
14+ self .id = id
1315
1416 def __str__ (self ):
15- return "Sense: " + str (self .sense ) + "\n Anti: " + str (self .anti ) + "\n XMin: " + str (self .xmin ) + "\n XMax: " + str (self .xmax )
17+ return "Sense: " + str (self .sense ) + "\n Anti: " + str (self .anti ) + "\n XMin: " + str (self .xmin ) + "\n XMax: " + str (self .xmax ) + " \n ID:" + str ( self . id )
1618
17- def parseComposite (file ):
18- fileArr = open (file , "r" ).read ().split ("\n " )
19- xmin = None
20- xmax = None
21- sense = []
22- anti = []
23- xmin_curr = 0
24- xmax_curr = 0
25- offset = 0
26- for line in fileArr :
27- print ("line" )
28- # Skip empty
29- if len (line .strip ()) == 0 :
30- continue
31- # Separate fields
32- fields = line .split ("\t " )
33- if fields [0 ] == "" or fields [0 ] == "NAME" :
34- print ("name" )
35- xmin_curr = int (float (fields [1 ]))
36- xmax_curr = int (float (fields [- 1 ]))
37- # If the x domain starts at 0 shift it to the left
38- if xmin_curr == 0 :
39- xmin_curr -= math .floor (xmax_curr / 2 )
40- xmax_curr -= math .floor (xmax_curr / 2 )
41- # If the x domain is not defined yet, define it
42- if xmin == None or xmax == None :
43- xmin = xmin_curr
44- xmax = xmax_curr
45- # Redefine min and max if necessary
46- xmax = max (xmax_curr , xmax )
47- xmin = min (xmin_curr , xmin )
48- sense = [0 ] * (xmax - xmin + 1 )
49- anti = [0 ] * (xmax - xmin + 1 )
19+ class CompositeGroup :
20+ def __init__ (self ):
21+ self .xmin = 0
22+ self .xmax = 0
23+ self .sense = []
24+ self .anti = []
25+ self .individual_composites = {}
26+ self .files_loaded = len (self .individual_composites )
27+
28+ def loadComposite (self ,composite : Composite ):
29+ # If no files, initialize sense and anti arrays; otherwise, pad sense and anti arrays to new xdomain
30+ self .xmin = min (composite .xmin , self .xmin )
31+ self .xmax = max (composite .xmax , self .xmax )
32+ if len (self .individual_composites ) == 0 :
33+ self .sense = [0 ] * (composite .xmax - composite .xmin + 1 )
34+ self .anti = [0 ] * (composite .xmax - composite .xmin + 1 )
35+ else :
36+ xmin = min ([int (self .individual_composites [c ].xmin ) for c in self .individual_composites ])
37+ xmax = max ([int (self .individual_composites [c ].xmax ) for c in self .individual_composites ])
38+ prefix = [0 ] * (xmin - self .xmin )
39+ suffix = [0 ] * (self .xmax - xmax )
40+ self .sense = prefix + self .sense + suffix
41+ self .anti = prefix + self .anti + suffix
42+
43+ # Update sense and anti arrays
44+ j = composite .xmin - self .xmin
45+ while j <= composite .xmax - composite .xmin :
46+ idx = composite .xmin - self .xmin + j
47+ self .sense [idx ] += composite .sense [j ]
48+ self .anti [idx ] += composite .anti [j ]
49+ j += 1
50+
51+ self .individual_composites [composite .id ] = composite
52+
53+ def loadCompositeDict (self ,compositeDict : dict ):
54+ for composite in compositeDict :
55+ # If no files, initialize sense and anti arrays; otherwise, pad sense and anti arrays to new xdomain
56+ self .xmin = min (composite .xmin , self .xmin )
57+ self .xmax = max (composite .xmax , self .xmax )
58+ if len (self .individual_composites ) == 0 :
59+ self .sense = [0 ] * (composite .xmax - composite .xmin + 1 )
60+ self .anti = [0 ] * (composite .xmax - composite .xmin + 1 )
61+ else :
62+ xmin = min ([c .xmin for c in self .individual_composites ])
63+ xmax = max ([c .xmax for c in self .individual_composites ])
64+ prefix = [0 ] * (xmin - self .xmin )
65+ suffix = [0 ] * (self .xmax - xmax )
66+ self .sense = prefix + self .sense + suffix
67+ self .anti = prefix + self .anti + suffix
5068
51- # Add the values to sense and anti arrays
52- if "sense" in fields [0 ].lower ():
53- print ("sense" )
54- i = 1
55- while i < len (fields ):
56- sense [offset + i - 1 ] += float (fields [i ])
57- i += 1
58- elif "anti" in fields [0 ].lower ():
59- i = 1
60- while i < len (fields ):
61- anti [offset + i - 1 ] += float (fields [i ])
62- i += 1
63- # If the first field is not empty or "NAME" and does not contain "sense" or "anti" parse as combined or midpoint data
64- elif not (fields [0 ] == "" or fields [0 ] == "NAME" ):
65- i = 1
66- while i < len (fields ):
67- sense [offset + i - 1 ] += float (fields [i ]) / 2
68- anti [offset + i - 1 ] += float (fields [i ]) / 2
69- return Composite (xmin , xmax , sense , anti )
69+ # Update sense and anti arrays
70+ j = composite .xmin - self .xmin
71+ while j <= composite .xmax - composite .xmin :
72+ idx = composite .xmin - self .xmin + j
73+ self .sense [idx ] += composite .sense [j ]
74+ self .anti [idx ] += composite .anti [j ]
75+ j += 1
7076
77+ self .individual_composites [composite .id ] = composite
78+
79+ def __str__ (self ):
80+ return str (self .individual_composites )
7181
7282def main ():
73- c = parseComposite ("sample.out" )
74- print (c )
83+ c = parseComposite .parseComposite ("sample_composites/sample_1.out" )
84+ compositeGroup = CompositeGroup ()
85+ compositeGroup .loadComposite (c )
86+ c2 = parseComposite .parseComposite ("sample_composites/sample2.out.txt" )
87+ compositeGroup .loadComposite (c2 )
88+ print (compositeGroup )
89+
7590
7691if __name__ == "__main__" :
7792 main ()
0 commit comments