Example #1
0
    def load_from_dir(cls, d):
        '''
        This class method returns a FeatureMatrix object that has been
        constructed using data loaded from a feature matrix directory.

        Args:
            | **d** *(str)*: The path to the feature matrix directory.
        Raises:

        '''
        # initilaze empty feature matrix object
        fm = cls()

        # first load object ids, if available
        f = os.path.join(d, cls.OBJECT_IDS_F)
        if(os.path.exists(f)):
            fm.load_object_ids(f)

            # read and add labelings
            lab_d = os.path.join(d, cls.LABELING_D)
            if(os.path.exists(lab_d)):
                for f in glob.glob(os.path.join(lab_d, '*.txt')):
                    lname = os.path.splitext(os.path.basename(f))[0]
                    if not(lname == cls.ONE_CLASS_LABELING):
                        (label_dict, class_names) = file_io.read_labeling(f)
                        fm.add_labeling(lname, label_dict, class_names)

            fids = None
            fnames = None
            featmat = None

            # read feature ids
            f = os.path.join(d, cls.FEATURE_IDS_F)
            if(os.path.exists(f)):
                with open(f, 'r') as fin:
                    fids = [i for i in file_io.read_ids(fin)]

            # read feature names
            f = os.path.join(d, cls.FEATURE_NAMES_F)
            if(os.path.exists(f)):
                with open(f, 'r') as fin:
                    fnames = [n for n in file_io.read_names(fin)]

            # read feature matrix
            f = os.path.join(d, cls.FEATURE_MATRIX_F)
            if(os.path.exists(f)):
                featmat = numpy.loadtxt(f)
                # in case of 1D matrix, reshape to single column 2D matrix
                fm_shape = featmat.shape
                if(len(fm_shape) == 1):
                    n = fm_shape[0]
                    featmat = featmat.reshape((n, 1))

            if not(featmat is None):
                fm.add_features(fids, featmat, fnames)

        return fm
Example #2
0
    def load_from_dir(cls, d):
        '''
        This class method returns a FeatureMatrix object that has been
        constructed using data loaded from a feature matrix directory.

        Args:
            | **d** *(str)*: The path to the feature matrix directory.
        Raises:

        '''
        # initilaze empty feature matrix object
        fm = cls()

        # first load object ids, if available
        f = os.path.join(d, cls.OBJECT_IDS_F)
        if (os.path.exists(f)):
            fm.load_object_ids(f)

            # read and add labelings
            lab_d = os.path.join(d, cls.LABELING_D)
            if (os.path.exists(lab_d)):
                for f in glob.glob(os.path.join(lab_d, '*.txt')):
                    lname = os.path.splitext(os.path.basename(f))[0]
                    if not (lname == cls.ONE_CLASS_LABELING):
                        (label_dict, class_names) = file_io.read_labeling(f)
                        fm.add_labeling(lname, label_dict, class_names)

            fids = None
            fnames = None
            featmat = None

            # read feature ids
            f = os.path.join(d, cls.FEATURE_IDS_F)
            if (os.path.exists(f)):
                with open(f, 'r') as fin:
                    fids = [i for i in file_io.read_ids(fin)]

            # read feature names
            f = os.path.join(d, cls.FEATURE_NAMES_F)
            if (os.path.exists(f)):
                with open(f, 'r') as fin:
                    fnames = [n for n in file_io.read_names(fin)]

            # read feature matrix
            f = os.path.join(d, cls.FEATURE_MATRIX_F)
            if (os.path.exists(f)):
                featmat = numpy.loadtxt(f)
                # in case of 1D matrix, reshape to single column 2D matrix
                fm_shape = featmat.shape
                if (len(fm_shape) == 1):
                    n = fm_shape[0]
                    featmat = featmat.reshape((n, 1))

            if not (featmat is None):
                fm.add_features(fids, featmat, fnames)

        return fm
    def add_labeling(self, labeling_name, labeling_f):

        # read labeling
        try:
            label_dict, class_names = file_io.read_labeling(labeling_f)
        except:
            return 'Error in labeling file.'
        finally:
            labeling_f.close()

        # add to feature matrix
        fm = self.get_feature_matrix()
        try:
            fm.add_labeling(labeling_name, label_dict, class_names)
        except ValueError as e:
            return str(e)

        # save if everything went well
        fm.save_to_dir(self.fm_dir)

        return ''
Example #4
0
    def add_labeling(self, labeling_name, labeling_f):

        # read labeling
        try:
            label_dict, class_names = file_io.read_labeling(labeling_f)
        except:
            return 'Error in labeling file.'
        finally:
            labeling_f.close()

        # add to feature matrix
        fm = self.get_feature_matrix()
        try:
            fm.add_labeling(labeling_name, label_dict, class_names)
        except ValueError as e:
            return str(e)

        # save if everything went well
        fm.save_to_dir(self.fm_dir)

        return ''
Example #5
0
 def load_from_file(cls, labeling_name, f):
     (label_dict, class_names) = file_io.read_labeling(f)
     object_ids = sorted(label_dict.keys())
     labels = [label_dict[oid] for oid in object_ids]
     return cls(labeling_name, object_ids, labels, class_names)
Example #6
0
 def load_from_file(cls, labeling_name, f):
     (label_dict, class_names) = file_io.read_labeling(f)
     object_ids = sorted(label_dict.keys())
     labels = [label_dict[oid] for oid in object_ids]
     return cls(labeling_name, object_ids, labels, class_names)