def _get_parameter_files(): """ Extracts the available parameter files from the param sub folder """ fn = os.path.split(sys.executable) if fn[1].lower().startswith("python"): exedir = os.path.abspath(os.path.split(sys.argv[0])[0]) else: exedir = fn[0] join = os.path.join pdir = join(exedir, "param") if os.path.exists(pdir): p_files = glob.glob(join(pdir, "*.dat")) pset = [] for f in p_files: p = os.path.basename(f).split(".")[0] pset.append(p) return pset else: errmsg = ( "The model parameter directory param is missing. " "It must be in the same directory as the program " "executable" ) error(errmsg, title="Error starting the program", buttons=["OK"]) sys.exit(-1)
def _get_parameter_files(): """ Extracts the available parameter files from the param sub folder """ fn = os.path.split(sys.executable) if fn[1].lower().startswith('python'): exedir = os.path.abspath(os.path.split(sys.argv[0])[0]) else: exedir = fn[0] join = os.path.join pdir = join(exedir, 'param') if os.path.exists(pdir): p_files = glob.glob(join(pdir, '*.dat')) pset = [] for f in p_files: p = os.path.basename(f).split('.')[0] pset.append(p) return pset else: print "Not finding the parameter directory", pdir errmsg = 'The model parameter directory param is missing. '\ 'It must be in the same directory as the program '\ 'executable' error(errmsg, title='Error starting the program', buttons=['OK']) sys.exit(-1)
def add_current_state_to_database(self): mdv = self.main_window # file name may contain unicode characters data_id = mdv.annotations_view.annotations_container.name if data_id is '': data_id = 'anonymous_annotations' elif type(data_id) is unicode: u_data_id = unicodedata.normalize('NFKD', data_id) data_id = u_data_id.encode('ascii','ignore') try: self.database.store_result( data_id, mdv.annotations_view.annotations_container, mdv.model, mdv.log_likelihood ) except PyannoValueError as e: logger.info(e) errmsg = e.args[0] error('Error: ' + errmsg) if self.db_window_open: self.database_window.db_updated = True
def _set_area_change(self, data): errmsg = "Area change should contain:\n timestep, relative area change" for vals in data: if len(vals) == 2: obj = AreaChange(timestep=int(vals[0]), rel_change=vals[1]) self.area_change.append(obj) elif vals != []: errmsg = errmsg + "\n%s data values found, 2 needed" % (len(data)) error(errmsg, title="error reading data", buttons=["OK"]) break
def _set_monthly_climate(self, data): errmsg = "Monthly climate data should contain: month,\n" "temperature and rainfall" for vals in data: if len(vals) == 3: obj = MonthlyClimate(month=int(vals[0]), temperature=vals[1], rainfall=vals[2]) self.monthly_climate.append(obj) elif vals != []: errmsg = errmsg + "\n%s data values found, 3 needed" % (len(data)) error(errmsg, title="Error reading data", buttons=["OK"]) break
def _set_constant_climate(self, data): errmsg = 'Constant climate should contain: mean temperature,\n'\ 'annual rainfall and temperature variation amplitude' if len(data[0])==3: self.constant_climate.mean_temperature = data[0][0] self.constant_climate.annual_rainfall = data[0][1] self.constant_climate.variation_amplitude = data[0][2] elif data[0]!=[]: errmsg = errmsg + '\n%s data values found, 3 needed' % (len(data)) error(errmsg, title='error reading data', buttons=['OK'])
def _action_failure(self, err): self._action_finally() if isinstance(err, PyannoValueError): errmsg = err.args[0] if 'Annotations' in errmsg: # raised when annotations are incompatible with the model error('Error: ' + errmsg) else: # re-raise exception if it has not been handled raise err
def _check_for_subjects_surfaces_fired(self): project_surface_directory=self.project_surface_directory subject_id=self.subject_id os.environ['SUBJECTS_DIR']=self.project_surface_directory if os.path.isfile(os.path.join(project_surface_directory,subject_id, 'mri','aparc+aseg.mgz')): self.subjects_existence=True message(message='Subject %s was successfully found at %s.'%(subject_id,os.path.join(project_surface_directory,subject_id)),title='Subject successfully found', buttons=['OK']) else: self.subjects_existence=False error(message='Subject %s was not found at %s. Please check your subject directory and subject id'%(subject_id,os.path.join(project_surface_directory,subject_id)), title='Subject not found', buttons=['OK'])
def _set_constant_climate(self, data): errmsg = ( "Constant climate should contain: mean temperature,\n" "annual rainfall and temperature variation amplitude" ) if len(data[0]) == 3: self.constant_climate.mean_temperature = data[0][0] self.constant_climate.annual_rainfall = data[0][1] self.constant_climate.variation_amplitude = data[0][2] elif data[0] != []: errmsg = errmsg + "\n%s data values found, 3 needed" % (len(data)) error(errmsg, title="error reading data", buttons=["OK"])
def _set_area_change(self, data): errmsg = 'Area change should contain:\n timestep, relative area change' for vals in data: if len(vals)==2: obj = AreaChange(timestep=int(vals[0]), rel_change=vals[1]) self.area_change.append(obj) elif vals!=[]: errmsg = errmsg + '\n%s data values found, 2 needed' % (len(data)) error(errmsg, title='error reading data', buttons=['OK']) break
def _modelrun_event_fired(self): # set the parameter set to use fn = os.path.split(sys.executable) if fn[1].lower().startswith('python'): exedir = os.path.abspath(os.path.split(sys.argv[0])[0]) else: exedir = fn[0] pdir = os.path.join(exedir, 'param') parfile = os.path.join(pdir, '%s.dat' % self.parameter_set) if self.initial_mode=='zero' and self.litter_mode=='zero': errmsg = ("Both soil carbon input and initial state may not be " "zero simultaneously.") error(errmsg, title='Invalid model parameters', buttons=['OK']) return if self.climate_mode=='yearly' and not self.yearly_climate: errmsg = ("Climate mode may not be 'yearly' if there are no " "yearly climate entries in the data file.") error(errmsg, title='Invalid model parameters', buttons=['OK']) return if self.leaching>0: errmsg = ("Leaching parameter may not be larger than 0.") error(errmsg, title='Invalid model parameters', buttons=['OK']) return if self.climate_mode=='monthly' and not self.monthly_climate: errmsg = ("Climate mode may not be 'monthly' if there are no " "monthly climate entries in the data file.") error(errmsg, title='Invalid model parameters', buttons=['OK']) return yassorunner = ModelRunner(parfile) if not yassorunner.is_usable_parameter_file(): errmsg = ("The selected parameter file has wrong number of columns " "and cannot be used.") error(errmsg, title='Invalid model parameters', buttons=['OK']) return self.yassorunner = yassorunner if self.initial_mode=='steady state': steady_state = self.yassorunner.compute_steady_state(self) self._set_steady_state(steady_state) self._init_results() self.c_stock, self.c_change, self.co2_yield = \ self.yassorunner.run_model(self) self._create_co2_plot() self._chart_type_changed()
def _load_litter_object(self, data, errmsg, hastime=False): obj = None loaded = True if hastime: if len(data) == 14: obj = TimedLitterComponent( timestep=int(data[0]), mass=data[1], mass_std=data[2], acid=data[3], acid_std=data[4], water=data[5], water_std=data[6], ethanol=data[7], ethanol_std=data[8], non_soluble=data[9], non_soluble_std=data[10], humus=data[11], humus_std=data[12], size_class=data[13], ) elif data != []: errmsg = errmsg + "\n%s data values found, 14 needed" % (len(data)) error(errmsg, title="Error reading data", buttons=["OK"]) loaded = False elif data == []: loaded = False else: if len(data) == 13: obj = LitterComponent( mass=data[0], mass_std=data[1], acid=data[2], acid_std=data[3], water=data[4], water_std=data[5], ethanol=data[6], ethanol_std=data[7], non_soluble=data[8], non_soluble_std=data[9], humus=data[10], humus_std=data[11], size_class=data[12], ) elif data != []: errmsg = errmsg + "\n%s data values found, 13 needed" % (len(data)) error(errmsg, title="Error reading data", buttons=["OK"]) loaded = False elif data == []: loaded = False return loaded, obj
def _set_monthly_climate(self, data): errmsg = 'Monthly climate data should contain: month,\n'\ 'temperature and rainfall' for vals in data: if len(vals)==3: obj = MonthlyClimate(month=int(vals[0]), temperature=vals[1], rainfall=vals[2]) self.monthly_climate.append(obj) elif vals!=[]: errmsg = errmsg + '\n%s data values found, 3 needed' % (len(data)) error(errmsg, title='Error reading data', buttons=['OK']) break
def _load_litter_object(self, data, errmsg, hastime=False): obj = None loaded = True if hastime: if len(data)==14: obj = TimedLitterComponent(timestep=int(data[0]), mass=data[1], mass_std=data[2], acid=data[3], acid_std=data[4], water=data[5], water_std=data[6], ethanol=data[7], ethanol_std=data[8], non_soluble=data[9], non_soluble_std=data[10], humus=data[11], humus_std=data[12], size_class=data[13]) elif data!=[]: errmsg = errmsg + '\n%s data values found, 14 needed' % (len(data)) error(errmsg, title='Error reading data', buttons=['OK']) loaded = False elif data==[]: loaded = False else: if len(data)==13: obj = LitterComponent(mass=data[0], mass_std=data[1], acid=data[2], acid_std=data[3], water=data[4], water_std=data[5], ethanol=data[6], ethanol_std=data[7], non_soluble=data[8], non_soluble_std=data[9], humus=data[10], humus_std=data[11], size_class=data[12]) elif data!=[]: errmsg = errmsg + '\n%s data values found, 13 needed' % (len(data)) error(errmsg, title='Error reading data', buttons=['OK']) loaded = False elif data==[]: loaded = False return loaded, obj
def _load_all_data(self, datafile): """ Loads all data from a single file. Data in sections defined by [name], data in whitespace delimited rows """ self._reset_data() sectionp = re.compile("\[([\w+\s*]+)\]") datap = re.compile("[+-Ee\d+\.\d*\s*]+") active = None data = defaultdict(list) alldata = "" linecount = 0 for line in datafile: linecount += 1 alldata += line m = re.match(sectionp, line) if m is not None: active = m.group(1) d = re.match(datap, line) if d is not None: try: vals = [float(val) for val in d.group(0).split()] data[active].append(vals) except ValueError: errmsg = ( "There's an error on line %s\n %s" "for section %s\n" "Values must be space separated and . is the decimal" " separator" % (linecount, d.group(0), active) ) error(errmsg, title="Error saving data", buttons=["OK"]) self.all_data = alldata for section, vallist in data.items(): if section == "Initial state": self._set_initial_state(vallist) elif section == "Constant soil carbon input": self._set_constant_litter(vallist) elif section == "Monthly soil carbon input": self._set_monthly_litter(vallist) elif section == "Yearly soil carbon input": self._set_yearly_litter(vallist) elif section == "Relative area change": self._set_area_change(vallist) elif section == "Constant climate": self._set_constant_climate(vallist) elif section == "Monthly climate": self._set_monthly_climate(vallist) elif section == "Yearly climate": self._set_yearly_climate(vallist)
def _set_yearly_climate(self, data): errmsg = 'Yearly climate should contain: timestep, mean temperature,\n'\ 'annual rainfall and temperature variation amplitude' for vals in data: if len(vals)==4: obj = YearlyClimate(timestep=int(vals[0]), mean_temperature=vals[1], annual_rainfall=vals[2], variation_amplitude=vals[3]) self.yearly_climate.append(obj) elif vals!=[]: errmsg = errmsg + '\n%s data values found, 4 needed' % (len(data)) error(errmsg, title='error reading data', buttons=['OK']) break
def _load_all_data(self, datafile): """ Loads all data from a single file. Data in sections defined by [name], data in whitespace delimited rows """ self._reset_data() sectionp = re.compile('\[([\w+\s*]+)\]') datap = re.compile('[+-Ee\d+\.\d*\s*]+') active = None data = defaultdict(list) alldata = '' linecount = 0 for line in datafile: linecount += 1 alldata += line m = re.match(sectionp, line) if m is not None: active = m.group(1) d = re.match(datap, line) if d is not None: try: vals = [float(val) for val in d.group(0).split()] data[active].append(vals) except ValueError: errmsg="There's an error on line %s\n %s"\ "for section %s\n"\ "Values must be space separated and . is the decimal"\ " separator" % (linecount, d.group(0), active) error(errmsg, title='Error saving data', buttons=['OK']) self.all_data = alldata for section, vallist in data.items(): if section=='Initial state': self._set_initial_state(vallist) elif section=='Constant soil carbon input': self._set_constant_litter(vallist) elif section=='Monthly soil carbon input': self._set_monthly_litter(vallist) elif section=='Yearly soil carbon input': self._set_yearly_litter(vallist) elif section=='Relative area change': self._set_area_change(vallist) elif section=='Constant climate': self._set_constant_climate(vallist) elif section=='Monthly climate': self._set_monthly_climate(vallist) elif section=='Yearly climate': self._set_yearly_climate(vallist)
def run_model(self, modeldata): self.simulation = True self.md = modeldata self.c_stock = numpy.empty(shape=(0, 10), dtype=numpy.float32) self.c_change = numpy.empty(shape=(0, 10), dtype=numpy.float32) self.co2_yield = numpy.empty(shape=(0, 3), dtype=numpy.float32) self.timemap = defaultdict(list) self.area_timemap = defaultdict(list) samplesize = self.md.sample_size msg = "Simulating %d samples for %d timesteps" % ( samplesize, self.md.simulation_length) progress = ProgressDialog(title="Simulation", message=msg, max=samplesize, show_time=True, can_cancel=True) progress.open() timesteps = self.md.simulation_length self.timestep_length = self.md.timestep_length self.ml_run = True self.infall = {} self.initial_mode = self.md.initial_mode if self.initial_mode == 'steady state': self.initial_def = self.md.steady_state else: self.initial_def = self.md.initial_litter timemsg = None for j in range(samplesize): (cont, skip) = progress.update(j) if not cont or skip: break self.draw = True self.curr_yr_ind = 0 self.curr_month_ind = 0 for k in range(timesteps): self._predict_timestep(j, k) self.ml_run = False self._fill_moment_results() progress.update(samplesize) if timemsg is not None: error(timemsg, title='Error handling timesteps', buttons=['OK']) return self.c_stock, self.c_change, self.co2_yield
def run_model(self, modeldata): self.simulation = True self.md = modeldata self.c_stock = numpy.empty(shape=(0,10), dtype=numpy.float32) self.c_change = numpy.empty(shape=(0,10), dtype=numpy.float32) self.co2_yield = numpy.empty(shape=(0,3), dtype=numpy.float32) self.timemap = defaultdict(list) self.area_timemap = defaultdict(list) samplesize = self.md.sample_size msg = "Simulating %d samples for %d timesteps" % (samplesize, self.md.simulation_length) progress = ProgressDialog(title="Simulation", message=msg, max=samplesize, show_time=True, can_cancel=True) progress.open() timesteps = self.md.simulation_length self.timestep_length = self.md.timestep_length self.ml_run = True self.infall = {} self.initial_mode = self.md.initial_mode if self.initial_mode=='steady state': self.initial_def = self.md.steady_state else: self.initial_def = self.md.initial_litter timemsg = None for j in range(samplesize): (cont, skip) = progress.update(j) if not cont or skip: break self.draw = True self.curr_yr_ind = 0 self.curr_month_ind = 0 for k in range(timesteps): self._predict_timestep(j, k) self.ml_run = False self._fill_moment_results() progress.update(samplesize) if timemsg is not None: error(timemsg, title='Error handling timesteps', buttons=['OK']) return self.c_stock, self.c_change, self.co2_yield
def add_current_state_to_database(self): mdv = self.main_window # file name may contain unicode characters data_id = mdv.annotations_view.annotations_container.name if data_id is '': data_id = 'anonymous_annotations' elif type(data_id) is unicode: u_data_id = unicodedata.normalize('NFKD', data_id) data_id = u_data_id.encode('ascii', 'ignore') try: self.database.store_result( data_id, mdv.annotations_view.annotations_container, mdv.model, mdv.log_likelihood) except PyannoValueError as e: logger.info(e) errmsg = e.args[0] error('Error: ' + errmsg) if self.db_window_open: self.database_window.db_updated = True
def set_scales(self, xscale, yscale): """Detect if the x-axis scale was changed and let the plotter know """ if not self.plot: return try: xs = self.plot.format_scale('x', xscale) self.xscale = xscale self.plot.set_xscale(xs) except: self.xscale = '1.0' error('Invalid X scale request') try: ys = self.plot.format_scale('y', yscale) self.yscale = yscale self.plot.set_yscale(ys) except: self.yscale = '1.0' error('Invalid Y scale request') self.plot.change_plot() return
def error_dialog(message): error(message) raise InaivuError(message)