Exemple #1
0
    def _saveModel(self, labeledGroupName, clf):
        '''
        Pickle and save the model in the database 
        '''
        # Save pickled mode to the database and relate it to the LABELED data resource
        if self.args.modelBaseName:
            rt, _ = ResourceType.objects.using(
                self.args.database).get_or_create(
                    name='FittedModel', description='SVC(gamma=2, C=1)')
            labeledResource = Resource.objects.using(
                self.args.database).filter(
                    resourcetype__name=labeledGroupName)[0]
            modelValue = pickle.dumps(clf).encode("zip").encode(
                "base64").strip()
            modelResource = Resource(name=self.args.modelBaseName,
                                     value=modelValue,
                                     resourcetype=rt)
            modelResource.save(using=self.args.database)
            rr = ResourceResource(fromresource=labeledResource,
                                  toresource=modelResource)
            rr.save(using=self.args.database)

            if self.args.verbose:
                print('Saved fitted model to the database with name = %s' %
                      self.args.modelBaseName)
                print(
                    'Retrieve with "clf = pickle.loads(r.value.decode("base64").decode("zip"))"'
                )
Exemple #2
0
    def describeClusterLabels(self, clusteredGroupName):
        '''
        To be called after clusters are saved.  Adds description text to appear in UI next to the Attributes button.
        '''
        mprs = MeasuredParameterResource.objects.using(self.args.database).filter(
            resource__resourcetype__name=clusteredGroupName
            ).select_related('resource')

        labels = mprs.values_list('resource__value', flat=True).distinct()

        for label in labels:
            rt, _ = ResourceType.objects.using(self.args.database).get_or_create(
                        name=clusteredGroupName, description='unsupervised classification')
            r, _ = Resource.objects.using(self.args.database).get_or_create(name=LABEL, 
                        value=label, resourcetype=rt)
            description = 'Automated {} classification of {} datavalues from {} features'.format(
                                self.args.algorithm,
                                mprs.filter(resource__value=label).count(),
                                len(self.args.inputs))
            if self.args.verbose:
                print(f'Describing label {label}: {description}')

            rdt, _ = ResourceType.objects.using(self.args.database).get_or_create(name=LABEL, description='metadata')
            rd, _ = Resource.objects.using(self.args.database).get_or_create(name=DESCRIPTION, value=description, resourcetype=rdt)
            rr = ResourceResource(fromresource=r, toresource=rd)
            rr.save(using=self.args.database)
Exemple #3
0
    def saveLabelSet(self, clResource, label, x_ids, y_ids, description, typeName, typeDescription):
        '''
        Save the set of labels in MeasuredParameterResource. Accepts 2 input vectors. (TODO: generalize to N input vectors);
        description is used to describe the criteria for assigning this label. The typeName and typeDecription may be used to
        refer to the grouping, and associate via the grouping the other labels made in the heuristic applied.
        '''
        try:
            # Label
            rt, _ = ResourceType.objects.using(self.args.database).get_or_create(name=typeName, description=typeDescription)
            r, _ = Resource.objects.using(self.args.database).get_or_create(name=LABEL, value=label, resourcetype=rt)
            # Label's description
            rdt, _ = ResourceType.objects.using(self.args.database).get_or_create(name=LABEL, description='metadata')
            rd, _ = Resource.objects.using(self.args.database).get_or_create(name=DESCRIPTION, value=description, resourcetype=rdt)
            rr = ResourceResource(fromresource=r, toresource=rd)
            rr.save(using=self.args.database)
            # Associate with commandlineResource
            ResourceResource.objects.using(self.args.database).get_or_create(fromresource=r, toresource=clResource)

        except IntegrityError as e:
            print(str(e))
            print("Ignoring")

        # Associate MeasuredParameters with Resource
        if self.args.verbose:
            print("  Saving %d values of '%s' with type '%s'" % (len(x_ids), label, typeName))
        for x_id,y_id in zip(x_ids, y_ids):
            a = self.getActivity(x_id, y_id)
            mp_x = MeasuredParameter.objects.using(self.args.database).get(pk=x_id)
            mp_y = MeasuredParameter.objects.using(self.args.database).get(pk=y_id)
            MeasuredParameterResource.objects.using(self.args.database).get_or_create(
                                activity=a, measuredparameter=mp_x, resource=r)
            MeasuredParameterResource.objects.using(self.args.database).get_or_create(
                                activity=a, measuredparameter=mp_y, resource=r)
Exemple #4
0
    def saveLabelSet(self, clResource, label, x_ids, y_ids, description, typeName, typeDescription):
        '''
        Save the set of labels in MeasuredParameterResource. Accepts 2 input vectors. (TODO: generalize to N input vectors);
        description is used to describe the criteria for assigning this label. The typeName and typeDecription may be used to
        refer to the grouping, and associate via the grouping the other labels made in the heuristic applied.
        '''
        try:
            # Label
            rt, created = ResourceType.objects.using(self.args.database).get_or_create(name=typeName, description=typeDescription)
            r, created = Resource.objects.using(self.args.database).get_or_create(name=LABEL, value=label, resourcetype=rt)
            # Label's description
            rdt, created = ResourceType.objects.using(self.args.database).get_or_create(name=LABEL, description='metadata')
            rd, created = Resource.objects.using(self.args.database).get_or_create(name=DESCRIPTION, value=description, resourcetype=rdt)
            rr = ResourceResource(fromresource=r, toresource=rd)
            rr.save(using=self.args.database)
            # Associate with commandlineResource
            ResourceResource.objects.using(self.args.database).get_or_create(fromresource=r, toresource=clResource)

        except IntegrityError as e:
            print e
            print "Ignoring"

        # Associate MeasuredParameters with Resource
        if self.args.verbose:
            print "  Saving %d values of '%s' with type '%s'" % (len(x_ids), label, typeName)
        for x_id,y_id in zip(x_ids, y_ids):
            a = self.getActivity(x_id, y_id)
            mp_x = MeasuredParameter.objects.using(self.args.database).get(pk=x_id)
            mp_y = MeasuredParameter.objects.using(self.args.database).get(pk=y_id)
            mpr_x, created = MeasuredParameterResource.objects.using(self.args.database).get_or_create(
                                activity=a, measuredparameter=mp_x, resource=r)
            mpr_y, created = MeasuredParameterResource.objects.using(self.args.database).get_or_create(
                                activity=a, measuredparameter=mp_y, resource=r)
Exemple #5
0
    def describeClusterLabels(self, clusteredGroupName):
        '''
        To be called after clusters are saved.  Adds description text to appear in UI next to the Attributes button.
        '''
        mprs = MeasuredParameterResource.objects.using(self.args.database).filter(
            resource__resourcetype__name=clusteredGroupName
            ).select_related('resource')

        labels = mprs.values_list('resource__value', flat=True).distinct()

        for label in labels:
            rt, _ = ResourceType.objects.using(self.args.database).get_or_create(
                        name=clusteredGroupName, description='unsupervised classification')
            r, _ = Resource.objects.using(self.args.database).get_or_create(name=LABEL, 
                        value=label, resourcetype=rt)
            description = 'Automated {} classification of {} datavalues from {} features'.format(
                                self.args.algorithm,
                                mprs.filter(resource__value=label).count(),
                                len(self.args.inputs))
            if self.args.verbose:
                print(f'Describing label {label}: {description}')

            rdt, _ = ResourceType.objects.using(self.args.database).get_or_create(name=LABEL, description='metadata')
            rd, _ = Resource.objects.using(self.args.database).get_or_create(name=DESCRIPTION, value=description, resourcetype=rdt)
            rr = ResourceResource(fromresource=r, toresource=rd)
            rr.save(using=self.args.database)
Exemple #6
0
    def _saveModel(self, labeledGroupName, clf):
        '''
        Pickle and save the model in the database 
        '''
        # Save pickled mode to the database and relate it to the LABELED data resource
        if self.args.modelBaseName:
            rt, created = ResourceType.objects.using(self.args.database).get_or_create(name='FittedModel', description='SVC(gamma=2, C=1)')
            labeledResource = Resource.objects.using(self.args.database).filter(resourcetype__name=labeledGroupName)[0]
            modelValue = pickle.dumps(clf).encode("zip").encode("base64").strip()
            modelResource = Resource(name=self.args.modelBaseName, value=modelValue, resourcetype=rt)
            modelResource.save(using=self.args.database)
            rr = ResourceResource(fromresource=labeledResource, toresource=modelResource)
            rr.save(using=self.args.database)

            if self.args.verbose:
                print 'Saved fitted model to the database with name = %s' %self.args.modelBaseName
                print 'Retrieve with "clf = pickle.loads(r.value.decode("base64").decode("zip"))"'