def status(self): url = self.get_absolute_url() if process_is_running(url): status = """Analysis is currently running.""" if self.progress[0] == 0: status += " (Pre-processing)" code = 2 elif process_is_complete(url): status = "Analysis completed. Compiling results..." code = 3 elif process_is_pending(url): status = "Analysis is in the queue but not yet running." res = get_process_result(url) code = 1 if res is not None: status += ".. " status += str(res) else: status = "An error occured while running this analysis." code = 0 res = get_process_result(url) if res is not None: status += "..<br/> " status += str(res) status += "<br/>Please edit the scenario and try again. If the problem persists, please contact us." return (code, "<p>%s</p>" % status)
def status(self): url = self.get_absolute_url() if process_is_running(url): status = """Analysis for <em>%s</em> is currently running.""" % (self.name,) code = 2 elif process_is_complete(url): status = "%s processing is done." % self.name code = 3 elif process_is_pending(url): status = "%s is in the queue but not yet running." % self.name res = get_process_result(url) code = 1 if res is not None: status += ".. " status += str(res) else: status = "An error occured while running this analysis." code = 0 res = get_process_result(url) if res is not None: status += "..<br/> " status += str(res) status += "<br/>Please edit the scenario and try again. If the problem persists, please contact us." return (code, "<p>%s</p>" % status)
def process_results(self): if process_is_complete(self.get_absolute_url()): chosen = get_process_result(self.get_absolute_url()) wshds = PlanningUnit.objects.filter(pk__in=chosen) self.output_best = json.dumps({'best': [str(x.pk) for x in wshds]}) ssoln = [x.strip().split(',') for x in open(os.path.join(self.outdir,"output","seak_ssoln.csv"),'r').readlines()][1:] selected = {} for s in ssoln: num = int(s[1]) if num > 0: selected[int(s[0])] = num self.output_pu_count = json.dumps(selected) super(Analysis, self).save() # save without calling save() self.invalidate_cache()
def reserve_energy(state): """ The "Objective Function"... Calculates the 'energy' of the reserve. Should incorporate costs of reserve and penalties for not meeting species targets. Note: This example is extremely simplistic compared to the Marxan objective function (see Appendix B in Marxan manual) but at least we have access to it! """ start = datetime.now() target_dict = {} penalty_dict = {} for cfkey in cfkeys: if cfkey in state: target_dict[cfkey] = 0.5 else: target_dict[cfkey] = 0 penalty_dict[cfkey] = 1.0 scalefactor = 5 name = ( str(len([k for k, v in target_dict.items() if v > 0])) + ": " + " ".join([x.split("---")[1] for x in state])[:100] ) wp = Scenario( input_targets=json.dumps(target_dict), input_penalties=json.dumps(penalty_dict), input_relativecosts=json.dumps(costs_dict), input_geography=json.dumps(geography_list), input_scalefactor=scalefactor, name=name, user=user, ) wp.save() url = wp.get_absolute_url() time.sleep(3) # assuming it takes at least X secs while not process_is_complete(url): time.sleep(0.2) wp.process_results() res = wp.results energy = res["surrogate"]["objective_score"] elapsed = datetime.now() - start print "Scenario %d\t\t%s secs\t\tscore = %s" % ( wp.id, elapsed.seconds + elapsed.microseconds / 1000000.0, energy, ) return energy