def remove_features(self):
        '''
            inst_dict: instance name -> Instance()
            meta_info: parsed coseal meta information and command line arguments (meta_info.options)
        '''
        
        feature_steps = self.metainfo.options.feature_steps
        feature_group_dict = self.metainfo.feature_group_dict

        if not feature_steps:
            feature_steps = list(feature_group_dict.keys()) # if no steps are specified, use all
            
        empty_check = set(feature_steps).difference(set(feature_group_dict.keys()))
        if empty_check:
            Printer.print_e("Feature steps (--feature-steps [list]) are not defined in data: %s" %(",".join(empty_check)), -2)

        unused_features = set()
        unused_steps = set(feature_group_dict.keys()).difference(set(feature_steps))
        for u_step in unused_steps:
            not_processed_features = feature_group_dict[u_step]
            unused_features = unused_features.union(set(not_processed_features))
        
        Printer.print_nearly_verbose("Remove features: %s\n" %(",".join(unused_features)))
        used_features = set(self.metainfo.features).difference(unused_features)
        Printer.print_c("Used features: %s\n" %(",".join(used_features)))
        
        unused_index_features = sorted(list(map(str,self.metainfo.features).index(un_feature) for un_feature in unused_features), reverse=True)
        
        for inst_ in self.instances.values():
            for un_feature_indx in unused_index_features:
                inst_._features.pop(un_feature_indx)
            total_cost = 0
            previous_presolved = False
            for f_step in feature_steps:
                if inst_._feature_group_cost_dict.get(f_step) and not previous_presolved: # feature costs are maybe None
                    total_cost += inst_._feature_group_cost_dict[f_step]
                if inst_._features_status[f_step] == "presolved":
                    previous_presolved = True
            for un_step in unused_steps:        # remove step status if unused 
                del inst_._features_status[un_step]
            
            inst_._feature_cost_total = total_cost
            inst_._pre_solved = "presolved" in map(lambda x: x, inst_._features_status.values())
            
        for un_feature_indx in unused_index_features:
            self.metainfo.features.pop(un_feature_indx)        
    def remove_features(self):
        '''
            inst_dict: instance name -> Instance()
            meta_info: parsed coseal meta information and command line arguments (meta_info.options)
        '''

        feature_steps = self.metainfo.options.feature_steps
        feature_group_dict = self.metainfo.feature_group_dict
        features = None  #self.metainfo.options.features

        if features:  #the user specified a subset of features (disables given feature_steps)

            empty_check = set(features).difference(self.metainfo.features)
            if empty_check:
                Printer.print_e(
                    "Features (--features [list]) are not defined in data: %s"
                    % (",".join(empty_check)), -2)

            unused_features = set(self.metainfo.features).difference(features)
            # find the corresponding feature steps
            feature_steps = set()
            for f in features:
                for f_group, f_list in feature_group_dict.iteritems():
                    if f in f_list["provides"]:
                        feature_steps.add(f_group)

            changed = True
            while changed:
                changed = False
                for step in feature_steps:
                    missing_steps = set(feature_group_dict[step].get(
                        "requires", set())).difference(feature_steps)
                    if missing_steps:
                        changed = True
                        feature_steps = feature_steps.union(missing_steps)
                        Printer.print_w(
                            "Adding missing feature step because of a pre-condition: %s"
                            % (",".join(missing_steps)))

            unused_steps = set(feature_group_dict.keys()).difference(
                set(feature_steps))
            Printer.print_c("Used feature steps (%d): %s" %
                            (len(feature_steps), ",".join(feature_steps)))

        else:
            if not feature_steps:
                feature_steps = list(
                    self.metainfo.feature_steps
                )  # if no steps are specified, use default

            empty_check = set(feature_steps).difference(
                set(feature_group_dict.keys()))
            if empty_check:
                Printer.print_e(
                    "Feature steps (--feature-steps [list]) are not defined in data: %s"
                    % (",".join(empty_check)), -2)

            # check preconditions of features
            available_steps = set()
            used_features = set()
            for step in feature_steps:  #TODO: order of feature steps could be an issue
                req_steps = set(feature_group_dict[step].get(
                    "requires", set()))
                miss_steps = req_steps.difference(feature_steps)
                if miss_steps:
                    Printer.print_w(
                        "Feature Step %s does not met his pre-conditions (%s). Adding feature step to set (but not to feature set!)."
                        % (step, ",".join(miss_steps)))

                available_steps.add(step)
                for f in feature_group_dict[step]["provides"]:
                    used_features.add(f)

                for ms in miss_steps:
                    available_steps.add(ms)
                    for f in feature_group_dict[ms]["provides"]:
                        used_features.add(f)

            feature_steps = available_steps
            Printer.print_c("Used Feature Steps (%d): %s" %
                            (len(feature_steps), ",".join(feature_steps)))

            unused_features = set(self.metainfo.features).difference(
                set(used_features))
            unused_steps = set(feature_group_dict.keys()).difference(
                set(available_steps))

        Printer.print_nearly_verbose("Remove features: %s\n" %
                                     (",".join(unused_features)))
        used_features = set(self.metainfo.features).difference(unused_features)
        Printer.print_c("Used features (%d): %s\n" %
                        (len(used_features), ",".join(used_features)))

        if not used_features:
            Printer.print_w(
                "Empty feature set - fall back to default feature set.")
            return False

        unused_index_features = sorted(list(
            map(str, self.metainfo.features).index(un_feature)
            for un_feature in unused_features),
                                       reverse=True)

        # remove unused features
        for inst_ in self.instances.values():
            for un_feature_indx in unused_index_features:
                inst_._features.pop(un_feature_indx)

        # compute feature costs
        for inst_ in self.instances.values():
            total_cost = 0
            previous_presolved = False
            for f_step in feature_steps:
                if inst_._feature_group_cost_dict.get(
                        f_step
                ) and not previous_presolved:  # feature costs are maybe None
                    total_cost += inst_._feature_group_cost_dict[f_step]
                if inst_._features_status[f_step] == "PRESOLVED":
                    previous_presolved = True
            for un_step in unused_steps:  # remove step status if unused
                del inst_._features_status[un_step]

            inst_._feature_cost_total = total_cost
            inst_._pre_solved = "PRESOLVED" in map(
                lambda x: x.upper(), inst_._features_status.values())

        for un_feature_indx in unused_index_features:
            self.metainfo.features.pop(un_feature_indx)

        #=======================================================================
        # if self.metainfo.options.impute == "none":
        #     for inst_ in self.instances.values():
        #         if reduce(lambda x,y: False if ((not x) and y.upper() == "OK") else True, inst_._features_status.values(), False):
        #             inst_._features = None
        #=======================================================================

        return True
Exemple #3
0
    def remove_features(self):
        '''
            inst_dict: instance name -> Instance()
            meta_info: parsed coseal meta information and command line arguments (meta_info.options)
        '''
        
        feature_steps = self.metainfo.options.feature_steps
        feature_group_dict = self.metainfo.feature_group_dict
        features = None #self.metainfo.options.features
            
        if features: #the user specified a subset of features (disables given feature_steps)
            
            empty_check = set(features).difference(self.metainfo.features)
            if empty_check:
                Printer.print_e("Features (--features [list]) are not defined in data: %s" %(",".join(empty_check)), -2)
            
            unused_features = set(self.metainfo.features).difference(features)
            # find the corresponding feature steps
            feature_steps = set()
            for f in features:
                for f_group, f_list in feature_group_dict.iteritems():
                    if f in f_list["provides"]:
                        feature_steps.add(f_group)
                        
            changed = True
            while changed:
                changed = False
                for step in feature_steps:
                    missing_steps = set(feature_group_dict[step].get("requires",set())).difference(feature_steps)
                    if missing_steps:
                        changed = True
                        feature_steps = feature_steps.union(missing_steps)
                        Printer.print_w("Adding missing feature step because of a pre-condition: %s" %(",".join(missing_steps)))
                        
            unused_steps = set(feature_group_dict.keys()).difference(set(feature_steps))
            Printer.print_c("Used feature steps (%d): %s" %(len(feature_steps), ",".join(feature_steps)))
            
        else:
            if not feature_steps:
                feature_steps = list(self.metainfo.feature_steps) # if no steps are specified, use default
            
            empty_check = set(feature_steps).difference(set(feature_group_dict.keys()))
            if empty_check:
                Printer.print_e("Feature steps (--feature-steps [list]) are not defined in data: %s" %(",".join(empty_check)), -2)

            # check preconditions of features
            available_steps = set()
            used_features = set()
            for step in feature_steps: #TODO: order of feature steps could be an issue
                req_steps = set(feature_group_dict[step].get("requires", set()))
                miss_steps = req_steps.difference(feature_steps)
                if miss_steps:
                    Printer.print_w("Feature Step %s does not met his pre-conditions (%s). Adding feature step to set (but not to feature set!)." %(step, ",".join(miss_steps)))
                
                available_steps.add(step)
                for f in feature_group_dict[step]["provides"]:
                    used_features.add(f) 
                    
                for ms in miss_steps:
                    available_steps.add(ms)
                    for f in feature_group_dict[ms]["provides"]:
                        used_features.add(f)            
                        
            feature_steps = available_steps
            Printer.print_c("Used Feature Steps (%d): %s" % (len(feature_steps), ",".join(feature_steps)))        
            
            unused_features = set(self.metainfo.features).difference(set(used_features))
            unused_steps = set(feature_group_dict.keys()).difference(set(available_steps))
            
        
        Printer.print_nearly_verbose("Remove features: %s\n" %(",".join(unused_features)))
        used_features = set(self.metainfo.features).difference(unused_features)
        Printer.print_c("Used features (%d): %s\n" %(len(used_features), ",".join(used_features)))
        
        if not used_features:
            Printer.print_w("Empty feature set - fall back to default feature set.")
            return False
        
        unused_index_features = sorted(list(map(str,self.metainfo.features).index(un_feature) for un_feature in unused_features), reverse=True)
        
        # remove unused features
        for inst_ in self.instances.values():
            for un_feature_indx in unused_index_features:
                inst_._features.pop(un_feature_indx)
                
        # compute feature costs
        for inst_ in self.instances.values():
            total_cost = 0
            previous_presolved = False
            for f_step in feature_steps:
                if inst_._feature_group_cost_dict.get(f_step) and not previous_presolved: # feature costs are maybe None
                    total_cost += inst_._feature_group_cost_dict[f_step]
                if inst_._features_status[f_step] == "PRESOLVED":
                    previous_presolved = True
            for un_step in unused_steps:        # remove step status if unused 
                del inst_._features_status[un_step]
            
            inst_._feature_cost_total = total_cost
            inst_._pre_solved = "PRESOLVED" in map(lambda x: x.upper(), inst_._features_status.values())
            
        for un_feature_indx in unused_index_features:
            self.metainfo.features.pop(un_feature_indx)        
        
        #=======================================================================
        # if self.metainfo.options.impute == "none":
        #     for inst_ in self.instances.values():
        #         if reduce(lambda x,y: False if ((not x) and y.upper() == "OK") else True, inst_._features_status.values(), False):
        #             inst_._features = None
        #=======================================================================
                    
        return True