Package generic_fns :: Module results
[hide private]
[frames] | no frames]

Source Code for Module generic_fns.results

  1  ############################################################################### 
  2  #                                                                             # 
  3  # Copyright (C) 2003, 2004 Edward d'Auvergne                                  # 
  4  #                                                                             # 
  5  # This file is part of the program relax.                                     # 
  6  #                                                                             # 
  7  # relax is free software; you can redistribute it and/or modify               # 
  8  # it under the terms of the GNU General Public License as published by        # 
  9  # the Free Software Foundation; either version 2 of the License, or           # 
 10  # (at your option) any later version.                                         # 
 11  #                                                                             # 
 12  # relax is distributed in the hope that it will be useful,                    # 
 13  # but WITHOUT ANY WARRANTY; without even the implied warranty of              # 
 14  # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the               # 
 15  # GNU General Public License for more details.                                # 
 16  #                                                                             # 
 17  # You should have received a copy of the GNU General Public License           # 
 18  # along with relax; if not, write to the Free Software                        # 
 19  # Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA   # 
 20  #                                                                             # 
 21  ############################################################################### 
 22   
 23  import sys 
 24   
 25   
26 -class Results:
27 - def __init__(self, relax):
28 """Class containing functions for reading and writing data.""" 29 30 self.relax = relax
31 32
33 - def copy(self, run1=None, run2=None, sim=None):
34 """Function for copying all results from run1 to run2.""" 35 36 # Test if run1 exists. 37 if not run1 in self.relax.data.run_names: 38 raise RelaxNoRunError, run1 39 40 # Test if run2 exists. 41 if not run2 in self.relax.data.run_names: 42 raise RelaxNoRunError, run2 43 44 # Function type. 45 function_type = self.relax.data.run_types[self.relax.data.run_names.index(run1)] 46 47 # Copy function. 48 copy = self.relax.specific_setup.setup('copy', function_type, raise_error=0) 49 50 # Copy the results. 51 copy(run1=run1, run2=run2, sim=sim)
52 53
54 - def display(self, run=None, format='columnar'):
55 """Function for displaying the results.""" 56 57 # Test if the run exists. 58 if not run in self.relax.data.run_names: 59 raise RelaxNoRunError, run 60 61 # Function type. 62 function_type = self.relax.data.run_types[self.relax.data.run_names.index(run)] 63 64 # Specific results writing function. 65 if format == 'xml': 66 format = 'XML' 67 self.write_function = self.relax.specific_setup.setup('write_xml_results', function_type, raise_error=0) 68 elif format == 'columnar': 69 self.write_function = self.relax.specific_setup.setup('write_columnar_results', function_type, raise_error=0) 70 else: 71 raise RelaxError, "Unknown format " + `format` + "." 72 73 # No function. 74 if not self.write_function: 75 raise RelaxError, "The " + format + " format is not currently supported for " + self.relax.specific_setup.get_string(function_type) + "." 76 77 # Write the results. 78 self.write_function(sys.stdout, run)
79 80
81 - def read(self, run=None, file='results', directory=None, file_data=None, format='columnar', print_flag=1):
82 """Function for reading the data out of a file.""" 83 84 # Test if the run exists. 85 if not run in self.relax.data.run_names: 86 raise RelaxNoRunError, run 87 88 # Function type. 89 function_type = self.relax.data.run_types[self.relax.data.run_names.index(run)] 90 91 # Equation type specific function setup. 92 if format == 'xml': 93 format = 'XML' 94 self.read_function = self.relax.specific_setup.setup('read_xml_results', function_type) 95 elif format == 'columnar': 96 self.read_function = self.relax.specific_setup.setup('read_columnar_results', function_type) 97 else: 98 raise RelaxError, "Unknown format " + `format` + "." 99 100 # No function. 101 if not self.read_function: 102 raise RelaxError, "The " + format + " format is not currently supported for " + self.relax.specific_setup.get_string(function_type) + "." 103 104 # The directory. 105 if directory == 'run': 106 directory = run 107 108 # Make sure that there are no data structures corresponding to the run. 109 for data_name in dir(self.relax.data): 110 # Get the object. 111 data = getattr(self.relax.data, data_name) 112 113 # Skip the data if it is not a dictionary (or equivalent). 114 if not hasattr(data, 'has_key'): 115 continue 116 117 # Skip the data if it doesn't contain the key 'old_run'. 118 if data.has_key(run): 119 raise RelaxError, "Data corresponding to the run " + `run` + " exists." 120 121 # Extract the data from the file. 122 file_data = self.relax.IO.extract_data(file_name=file, dir=directory, file_data=file_data) 123 124 # Strip data. 125 file_data = self.relax.IO.strip(file_data) 126 127 # Do nothing if the file does not exist. 128 if not file_data: 129 raise RelaxFileEmptyError 130 131 # Read the results. 132 self.read_function(run, file_data, print_flag)
133 134
135 - def write(self, run=None, file="results", directory=None, force=0, format='columnar', compress_type=1, print_flag=1):
136 """Create the results file.""" 137 138 # Test if the run exists. 139 if not run in self.relax.data.run_names: 140 raise RelaxNoRunError, run 141 142 # The directory. 143 if directory == 'run': 144 directory = run 145 146 # Function type. 147 function_type = self.relax.data.run_types[self.relax.data.run_names.index(run)] 148 149 # Specific results writing function. 150 if format == 'xml': 151 format = 'XML' 152 self.write_function = self.relax.specific_setup.setup('write_xml_results', function_type, raise_error=0) 153 elif format == 'columnar': 154 self.write_function = self.relax.specific_setup.setup('write_columnar_results', function_type, raise_error=0) 155 else: 156 raise RelaxError, "Unknown format " + `format` + "." 157 158 # No function. 159 if not self.write_function: 160 raise RelaxError, "The " + format + " format is not currently supported for " + self.relax.specific_setup.get_string(function_type) + "." 161 162 # Open the file for writing. 163 results_file = self.relax.IO.open_write_file(file_name=file, dir=directory, force=force, compress_type=compress_type, print_flag=print_flag) 164 165 # Write the results. 166 self.write_function(results_file, run) 167 168 # Close the results file. 169 results_file.close()
170