Ejemplo n.º 1
0
def data_mining_run(designs, behavioral, non_behavioral, context):
    client = DataMiningClient()
    try:
        # Start connection with data_mining
        client.startConnection()

        support_threshold = 0.002
        confidence_threshold = 0.2
        lift_threshold = 1

        # features = client.getDrivingFeatures(behavioral, non_behavioral, designs, support_threshold, confidence_threshold, lift_threshold)
        features = client.runAutomatedLocalSearch(behavioral, non_behavioral,
                                                  designs, support_threshold,
                                                  confidence_threshold,
                                                  lift_threshold)

        # End the connection before return statement
        client.endConnection()

        result = []
        max_features = 3
        if len(features) > 3:
            pass
        else:
            max_features = len(features)

        for i in range(
                max_features):  # Generate answers for the first 3 features
            advice = feature_expression_to_string(features[i]['name'], context)
            result.append({"type": "Analyzer", "advice": advice})
        return result

    except Exception:
        logger.exception('Exception in running data mining')
        client.endConnection()
        return None
Ejemplo n.º 2
0
    def analyst_critic(self, this_design):
        result = []
        client = DataMiningClient()

        problem = self.context.eosscontext.problem
        if problem in self.assignation_problems:
            problem_type = 'binary'
        elif problem in self.partition_problems:
            problem_type = 'discrete'
        else:
            problem_type = 'unknown'

        try:
            # Start connection with data_mining
            client.startConnection()

            support_threshold = 0.02
            confidence_threshold = 0.2
            lift_threshold = 1

            behavioral = []
            non_behavioral = []

            dataset = Design.objects.filter(
                eosscontext_id__exact=self.context.eosscontext.id).all()

            if len(dataset) < 10:
                raise ValueError(
                    "Could not run data mining: the number of samples is less than 10"
                )
            else:

                utopiaPoint = [0.26, 0]
                temp = []
                # Select the top N% archs based on the distance to the utopia point
                for design in dataset:
                    outputs = json.loads(this_design.outputs)
                    id = design.id
                    dist = math.sqrt((outputs[0] - utopiaPoint[0])**2 +
                                     (outputs[1] - utopiaPoint[1])**2)
                    temp.append((id, dist))

                # Sort the list based on the distance to the utopia point
                temp = sorted(temp, key=lambda x: x[1])
                for i in range(len(temp)):
                    if i <= len(
                            temp
                    ) // 10:  # Label the top 10% architectures as behavioral
                        behavioral.append(temp[i][0])
                    else:
                        non_behavioral.append(temp[i][0])

            # Extract feature
            # features = client.getDrivingFeatures(behavioral, non_behavioral, designs, support_threshold, confidence_threshold, lift_threshold)
            features = client.runAutomatedLocalSearch(
                problem, problem_type, behavioral, non_behavioral, dataset,
                support_threshold, confidence_threshold, lift_threshold)

            advices = []
            if not len(features) == 0:

                # Compare features to the current design
                unsatisfied = get_feature_unsatisfied(features[0]['name'],
                                                      this_design,
                                                      self.context)
                satisfied = get_feature_satisfied(features[0]['name'],
                                                  this_design, self.context)

                if type(unsatisfied) is not list:
                    unsatisfied = [unsatisfied]

                if type(satisfied) is not list:
                    satisfied = [satisfied]

                for exp in unsatisfied:
                    if exp == "":
                        continue
                    advices.append(
                        "Based on the data mining result, I advise you to make the following change: "
                        + feature_expression_to_string(
                            exp, is_critique=True, context=self.context))

                for exp in satisfied:
                    if exp == "":
                        continue
                    advices.append(
                        "Based on the data mining result, these are the good features. Consider keeping them: "
                        + feature_expression_to_string(
                            exp, is_critique=False, context=self.context))

            # End the connection before return statement
            client.endConnection()

            for i in range(
                    len(advices)):  # Generate answers for the first 5 features
                advice = advices[i]
                result.append({"type": "Analyst", "advice": advice})
        except Exception as e:
            print("Exc in generating critic from data mining: " + str(e))
            traceback.print_exc(file=sys.stdout)
            client.endConnection()

        return result