Esempio n. 1
0
    def set_output(self,
                   filename=".png",
                   width=480,
                   height=480,
                   display="default"):
        # close output before setting new one, even if it's the same filename
        if self.filename:
            self.close_output()
        self.filename = filename

        if self.filename == ".png":
            # If .png is specified, we randomly generate a filename and treat as
            # temporary
            self.filename = trm.temp_filename(prefix='mdig_output',
                                              suffix='.png')
            self.output_is_temporary = True

        # display must always check the same file
        # use a temporary png filename while constructing png
        # (self.temp_output_file)
        # also use a temporary filename for the file the display process
        # monitors (self.displays[display])
        # then, only copy self.temp_output_file to ... and to .png
        # once close_output is called.

        if display and display not in self.displays:
            temp_filename = trm.temp_filename(prefix='mdig_display',
                                              suffix='.png')
            self.displays[display] = (self.filename, temp_filename, None)
            # start display process only when close_output is called
        elif display:
            # update the mapping from filename to temp filename
            oldd = self.displays[display]
            self.displays[display] = (self.filename, oldd[1], oldd[2])

        self.temp_output_file = trm.temp_filename(prefix='mdig_temp_output',
                                                  suffix='.png')

        # set variables
        output_vars = {
            'GRASS_RENDER_IMMEDIATE': 'TRUE',
            'GRASS_TRUECOLOR': 'TRUE',
            'GRASS_PNGFILE': 'TRUE',
            'GRASS_WIDTH': repr(width),
            'GRASS_HEIGHT': repr(height),
            'GRASS_PNGFILE': self.temp_output_file,
            'GRASS_PNG_READ': "FALSE",
        }

        # update grass variables
        for k, v in output_vars.iteritems():
            os.environ[k] = v
            self.grass_vars[k] = v
Esempio n. 2
0
    def set_output(self, filename=".png", width=480, height=480, display="default"):
        # close output before setting new one, even if it's the same filename
        if self.filename:
            self.close_output()
        self.filename = filename

        if self.filename == ".png":
            # If .png is specified, we randomly generate a filename and treat as
            # temporary
            self.filename = trm.temp_filename(prefix='mdig_output', suffix='.png')
            self.output_is_temporary = True

        # display must always check the same file
        # use a temporary png filename while constructing png
        # (self.temp_output_file)
        # also use a temporary filename for the file the display process
        # monitors (self.displays[display])
        # then, only copy self.temp_output_file to ... and to .png
        # once close_output is called.

        if display and display not in self.displays:
            temp_filename = trm.temp_filename(prefix='mdig_display', suffix='.png')
            self.displays[display] = (self.filename, temp_filename, None)
            # start display process only when close_output is called
        elif display:
            # update the mapping from filename to temp filename
            oldd = self.displays[display]
            self.displays[display] = (self.filename, oldd[1], oldd[2])
            
        self.temp_output_file = trm.temp_filename(prefix='mdig_temp_output', suffix='.png')

        # set variables
        output_vars = {
                'GRASS_RENDER_IMMEDIATE': 'TRUE',
                'GRASS_TRUECOLOR': 'TRUE',
                'GRASS_PNGFILE': 'TRUE',
                'GRASS_WIDTH': repr(width),
                'GRASS_HEIGHT': repr(height),
                'GRASS_PNGFILE': self.temp_output_file,
                'GRASS_PNG_READ': "FALSE",
        }

        # update grass variables
        for k, v in output_vars.iteritems():
            os.environ[k] = v
            self.grass_vars[k] = v
Esempio n. 3
0
    def run(self, in_name, out_name, rep, is_pop):
        """
        Run the event using in_name as the input map and out_name as the output map. 
        """
        template_p = self.get_params(is_pop, None)
        p = {}

        # If this event has a fixed input specified
        if self.fixed_input is not None:
            in_name = self.fixed_input

        # Parameter names for input and output maps
        in_param = self.get_input_name()
        if in_param is not None:
            template_p[in_param] = ("IN", in_name)
        out_param = self.get_output_name()
        if out_param is not None:
            template_p[out_param] = ("OUT", out_name)

        # Some commands report some interesting information to aggregate,
        # like r.mdig.survival's AREA_EVALUATED
        report_file = None

        s_name = rep.instance.strategy
        s = rep.instance.experiment.get_management_strategy(s_name)
        # TODO strategies should be pre initialised with instances
        if s is not None:
            s.set_instance(rep.instance)
        for p_name, value in template_p.items():
            p_type, p_value = value
            # print 'name is', p_name,
            # print 'value is', value
            if p_type == "VAR":
                instance_value = rep.instance.get_var(p_value)
                instance_map = None
                treatments = []
                if s:
                    treatments = s.get_treatments_for_param(
                        p_value, rep.current_t)
                    if treatments:
                        self.log.debug("treatments for variable %s are: %s" %
                                       (p_value, repr(treatments)))
                        # TODO support blending of multiple treatments on param
                        # (move below operations from treatment to strategy)
                        assert len(
                            treatments
                        ) == 1, "MDiG does not currently support multiple treatments to a parameter"
                        instance_map = treatments[0].get_variable_map(
                            p_value, instance_value, rep)
                        if instance_map is None:
                            instance_value = treatments[
                                0].get_altered_variable_value(
                                    p_value, instance_value)
                            assert instance_value is not None
                if instance_value:
                    p[p_name] = instance_value
                    self.log.debug(
                        "Variable %s has value %s for this instance" %
                        (p_name, instance_value))
                elif instance_map:
                    p[p_name] = instance_map
                    self.log.debug("Variable %s is map %s for this instance" %
                                   (p_name, instance_map))
                else:
                    self.log.debug(
                        "Variable %s has None value for this instance" %
                        p_name)
            elif p_type == "SEED":
                p[p_name] = rep.random.randint(-2.14748e+09, 2.14748e+09)
            elif p_type == "REPORT_FILE":
                report_file = trm.temp_filename(prefix='mdig_event_report')
                p[p_name] = report_file
            elif p_type in ["VALUE", "MAP", "IN", "OUT"]:
                p[p_name] = p_value
            elif p_type == "FLAG":
                p[p_name] = "FLAG"
            else:
                raise Exception("Unknown parameter type %s" % p_type)

        cmd = self.create_cmd_string(p)

        grass.get_g().remove_map(out_name)
        grass.get_g().run_command(cmd)

        metrics = {}
        if report_file:
            metrics = self.read_report_file(report_file)
        return metrics
Esempio n. 4
0
    def run(self, in_name, out_name, rep, is_pop):
        """
        Run the event using in_name as the input map and out_name as the output map. 
        """
        template_p = self.get_params(is_pop,None)
        p = {}
        
        # If this event has a fixed input specified
        if self.fixed_input is not None:
            in_name = self.fixed_input

        # Parameter names for input and output maps
        in_param = self.get_input_name()
        if in_param is not None:
            template_p[in_param] = ("IN", in_name)
        out_param = self.get_output_name()
        if out_param is not None:
            template_p[out_param] = ("OUT", out_name)

        # Some commands report some interesting information to aggregate,
        # like r.mdig.survival's AREA_EVALUATED
        report_file = None

        s_name = rep.instance.strategy
        s = rep.instance.experiment.get_management_strategy(s_name)
        # TODO strategies should be pre initialised with instances
        if s is not None:
            s.set_instance(rep.instance)
        for p_name, value in template_p.items():
            p_type, p_value = value
            # print 'name is', p_name,
            # print 'value is', value
            if p_type == "VAR":
                instance_value = rep.instance.get_var(p_value)
                instance_map = None
                treatments = []
                if s:
                    treatments = s.get_treatments_for_param(p_value,rep.current_t)
                    if treatments:
                        self.log.debug("treatments for variable %s are: %s" % (p_value, repr(treatments)))
                        # TODO support blending of multiple treatments on param
                        # (move below operations from treatment to strategy)
                        assert len(treatments) == 1, "MDiG does not currently support multiple treatments to a parameter"
                        instance_map = treatments[0].get_variable_map(p_value, instance_value, rep)
                        if instance_map is None:
                            instance_value = treatments[0].get_altered_variable_value(p_value,instance_value)
                            assert instance_value is not None
                if instance_value:
                    p[p_name]=instance_value
                    self.log.debug("Variable %s has value %s for this instance" %
                            (p_name, instance_value))
                elif instance_map:
                    p[p_name]=instance_map
                    self.log.debug("Variable %s is map %s for this instance" %
                            (p_name, instance_map))
                else:
                    self.log.debug("Variable %s has None value for this instance" %
                            p_name)
            elif p_type == "SEED":
                p[p_name] = rep.random.randint(-2.14748e+09,2.14748e+09)
            elif p_type == "REPORT_FILE":
                report_file = trm.temp_filename(prefix='mdig_event_report')
                p[p_name] = report_file
            elif p_type in ["VALUE", "MAP", "IN", "OUT"]:
                p[p_name] = p_value
            elif p_type == "FLAG":
                p[p_name] = "FLAG"
            else: 
                raise Exception("Unknown parameter type %s" % p_type)
        
        cmd=self.create_cmd_string(p)
        
        grass.get_g().remove_map(out_name)
        grass.get_g().run_command(cmd)

        metrics = {}
        if report_file:
            metrics = self.read_report_file(report_file)
        return metrics