def write_plaintext(data, filename, header=False): """ Write a 1D or 2D `DataSet` instance as a plaintext file Parameters ---------- data : `DataSet` the data set instance to write out filename : str the desired name for the output file """ if not isinstance(data, DataSet): raise TypeError("input `data` must be an instance of `DataSet`") if len(data.dims) == 1: mode = '1d' elif len(data.dims) == 2: mode = '2d' else: raise ValueError("`data` has too many dimensions to write to plaintext file") # format the output columns = [data[name] for name in data.variables] edges = [data.edges[dim] for dim in data.dims] if len(edges) == 1: edges = edges[0] # and write storage = MeasurementStorage.create(mode, filename) storage.write(edges, data.variables, columns, **data.attrs)
def save(self, output, result): """ Save the power spectrum results to the specified output file Parameters ---------- output : str the string specifying the file to save result : tuple the tuple returned by `run()` -- first argument specifies the bin edges and the second is a dictionary holding the data results """ from nbodykit.storage import MeasurementStorage # only the master rank writes if self.comm.rank == 0: kedges, result, meta = result k, poles, N = result ells = range(0, self.max_ell + 1, 2) # write binned statistic args = (",".join(map(str, ells)), output) self.logger.info("measurement done; saving ell = %s multipole(s) to %s" % args) cols = ["k"] + ["power_%d" % l for l in ells] + ["modes"] pole_result = [k] + [pole for pole in poles] + [N] storage = MeasurementStorage.create("1d", output) storage.write(kedges, cols, pole_result, **meta)
def save(self, output, result): """ Save the power spectrum results to the specified output file Parameters ---------- output : str the string specifying the file to save result : tuple the tuple returned by `run()` -- first argument specifies the bin edges and the second is a dictionary holding the data results """ from nbodykit.storage import MeasurementStorage # only the master rank writes if self.comm.rank == 0: kedges, result, meta = result k, poles, N = result ells = range(0, self.max_ell + 1, 2) # write binned statistic args = (",".join(map(str, ells)), output) self.logger.info( 'measurement done; saving ell = %s multipole(s) to %s' % args) cols = ['k'] + ['power_%d' % l for l in ells] + ['modes'] pole_result = [k] + [pole for pole in poles] + [N] storage = MeasurementStorage.create('1d', output) storage.write(kedges, cols, pole_result, **meta)
def save(self, output, result): """ Save the correlation function results to the specified output file Parameters ---------- output : str the string specifying the file to save result : tuple the tuple returned by `run()` -- first argument specifies the bin edges and the second is a dictionary holding the data results """ from nbodykit.storage import MeasurementStorage # only the master rank writes if self.comm.rank == 0: edges, result, pole_result, meta = result if self.mode == "1d": cols = ['r', 'corr', 'modes'] result = [numpy.squeeze(result[i]) for i in [0, 2, 3]] edges_ = edges[0] else: edges_ = edges cols = ['r', 'mu', 'corr', 'modes'] # write binned statistic self.logger.info('measurement done; saving result to %s' %output) storage = MeasurementStorage.create(self.mode, output) storage.write(edges_, cols, result, **meta) # write multipoles if len(self.poles): filename, ext = os.path.splitext(output) pole_output = filename + '_poles' + ext # format is k pole_0, pole_1, ...., modes_1d self.logger.info('saving ell = %s multipoles to %s' %(",".join(map(str,self.poles)), pole_output)) storage = MeasurementStorage.create('1d', pole_output) k, poles, N = pole_result cols = ['k'] + ['power_%d' %l for l in self.poles] + ['modes'] pole_result = [k] + [pole for pole in poles] + [N] storage.write(edges[0], cols, pole_result, **meta)
def save(self, output, result): """ Save the result returned by `run()` to the filename specified by `output` Parameters ---------- output : str the string specifying the file to save result : tuple the tuple returned by `run()` -- first argument specifies the bin edges and the second is a dictionary holding the data results """ from nbodykit.storage import MeasurementStorage # only master writes if self.comm.rank == 0: edges, result = result storage = MeasurementStorage.create(self.mode, output) cols = list(result.keys()) values = list(result.values()) storage.write(edges, cols, values)