Peano
Loading...
Searching...
No Matches
Dataset.py
Go to the documentation of this file.
1# This file is part of the Peano project. For conditions of distribution and
2# use, please see the copyright notice at www.peano-framework.org
3
4
5import pandas
6import glob
7
8
9def load_file_sequence(file_prefix,dimensions):
10 """
11
12 Load all files that start with file_prefix and merge them into one
13 instance of Dataset.
14
15 """
16 result = None
17 filtered_files = glob.glob( file_prefix + "*.csv" )
18 for filtered_file in filtered_files:
19 new_dataset = Dataset()
20 new_dataset.parse(filtered_file,dimensions)
21 if new_dataset.valid and result==None:
22 result = new_dataset
23 elif new_dataset.valid:
24 result.append( new_dataset )
25 else:
26 print( "ERROR: file " + filtered_file + " was not a valid Peano 4 particle database file" )
27 return result
28
29
30def assert_ascending(data):
31 for i in range(0,len(data)-1):
32 if data[i]>data[i+1]:
33 raise Exception( "not in ascending order at position {}: {} vs {}".format(i,data[i],data[i+1]) )
34
35
36class Dataset(object):
37 """!
38
39 Dataset object corresponding to one VTU snapshot
40
41 """
42 def __init__(self):
43 self.valid = False
45 self.dimensions_ = -1
46 self.data_ = None
47
48 __Tree_Number_Identifier = "tree_number"
49 __Tree_Local_Particle_Number_Identifier = "particle_number"
50
51
54 if self.dimensions_==2:
55 colnames += [ "x", "y" ]
56 else:
57 colnames += [ "x", "y", "z" ]
58 for i in range(self.number_of_data_columns_): colnames.append("Q{}".format(i))
59 return colnames
60
61
62 def _parse_number_of_entries(self,filename):
63 read_header = open(filename, "r" )
64 current_line = read_header.readline()
65 while "#" in current_line:
66 current_line = read_header.readline()
67 # Assume first row without # is header, so read first data row
68 current_line = read_header.readline()
69 self.number_of_data_columns_ = current_line.count( "," ) + 1 - self.dimensions_ - 2 - 1
70
71
72 def parse(self,filename,dimensions):
73 print( "" )
74 print( "-------------------------------------------------------")
75 print( "parse file " + filename )
76 print( "-------------------------------------------------------")
77
78 self.dimensions_ = dimensions
79 self.valid = True
80
81 self._parse_number_of_entries(filename)
82 print( "file hosts {} data entries per particle snapshot".format(self.number_of_data_columns_) )
83 self.data_ = pandas.read_csv(filename,names=self._make_column_names(), skiprows=1)
84
85
86 def append(self,other_dataset):
87 if self.dimensions_!=other_dataset.dimensions_:
88 raise Exception( "dimensions of datasets do not match")
89 if self.number_of_data_columns_!=other_dataset.number_of_data_columns_:
90 raise Exception( "number of data columns per particle do not match")
91
92 self.data_ = pandas.concat([self.data_, other_dataset.data_])
93
94
96 """
97
98 Particles are identified by integer tuples and not by a single
99 number. This operation returns a set of thes tuples.
100
101 """
103
104
105 def get_data(self,particle,column_number,clean_up=1e-3):
106 """
107
108 particle: (Int,Int)
109 Tuple identifying a particular particle
110
111 column_number: Int
112 Integer with the data column requested
113
114 Returns two lists: A lits of time stamps and a list of data entries
115
116 """
117 data_frame = pandas.DataFrame(self.data_)
118
119 filtered_data = data_frame.loc[ data_frame[self.__Tree_Number_Identifier]==particle[0] ]
120 filtered_data = filtered_data.loc[ filtered_data[self.__Tree_Local_Particle_Number_Identifier]==particle[1] ]
121
122 filtered_data = filtered_data.sort_values( by="t" )
123
124 result = list(filtered_data.t), list(filtered_data["Q{}".format(column_number)])
125 assert_ascending(result[0])
126 if clean_up!=0.0 and len(result[0])>2 and result[0][1]-result[0][0]<=clean_up:
127 result = result[0][1:],result[1][1:]
128 return result
129
130
131 def get_position(self,particle):
132 """
133
134 Pass in a particle identified by a two-tuple and return d+1 arrays.
135 The first result array is the timestamps, then we have d arrays with
136 the particle locations.
137
138 """
139 data_frame = pandas.DataFrame(self.data_)
140
141 filtered_data = data_frame.loc[ data_frame[self.__Tree_Number_Identifier]==particle[0] ]
142 filtered_data = filtered_data.loc[ filtered_data[self.__Tree_Local_Particle_Number_Identifier]==particle[1] ]
143
144 filtered_data = filtered_data.sort_values( by="t" )
145
146 if self.dimensions_==2:
147 return list(filtered_data.t), list(filtered_data["x"]), list(filtered_data["y"])
148 else:
149 return list(filtered_data.t), list(filtered_data["x"]), list(filtered_data["y"]), list(filtered_data["z"])
Dataset object corresponding to one VTU snapshot.
Definition Dataset.py:36
extract_particle_keys(self)
Particles are identified by integer tuples and not by a single number.
Definition Dataset.py:95
get_position(self, particle)
Pass in a particle identified by a two-tuple and return d+1 arrays.
Definition Dataset.py:131
get_data(self, particle, column_number, clean_up=1e-3)
particle: (Int,Int) Tuple identifying a particular particle
Definition Dataset.py:105