def load_file(self, filename):
        ''' Loads variables in from a properties file. '''
        from sqltools import Gate, Filter, OldFilter
        
        self.clear()
        self._filename        = filename
        self._groups          = {}
        self._filters         = ObservableDict()
        self.gates            = ObservableDict()

        f = open(filename, 'U')
        lines = f.read()
        self._textfile = lines     # store raw file
        lines = lines.split('\n')

        for idx in xrange(len(lines)):
            line = lines[idx]
            # skip commented and empty lines
            if not line.strip().startswith('#') and line.strip() != '':
                try:
                    (name, val) = line.split('=', 1)
                    name = name.strip()
                    val = val.strip()
                except ValueError:
                    raise Exception('PROPERTIES ERROR: Could not parse line #%d\n'
                                    '\t"%s"\n'
                                    'Did you accidentally load your training set instead of your properties file?'%(idx + 1, line))
                
                if name in string_vars:
                    self.__dict__[name] = val or None
                
                elif name in list_vars:
                    self.__dict__[name] = self.parse_list_value(val) or None
                    
                elif name.startswith('group_SQL_'):
                    group_name = name[10:]
                    if group_name == '':
                        raise Exception, ('PROPERTIES ERROR (%s): "group_SQL_" should be followed by a group name.\n'
                                          'Example: "group_SQL_MyGroup = <QUERY>" would define a group named "MyGroup" defined by\n'
                                          'a MySQL query "<QUERY>". See the README.'%(name))
                    if group_name in self._groups.keys():
                        raise Exception, 'Group "%s" is defined twice in properties file.'%(group_name)
                    if group_name in self._filters.keys():
                        raise Exception, 'Name "%s" is already taken for a filter.'%(group_name)
                    if not val:
                        logging.warn('PROPERTIES WARNING (%s): Undefined group'%(name))
                        continue
                    self._groups[group_name] = val
                    
                elif name.startswith('filter_SQL_'):
                    # Load old-style SQL filters:
                    filter_name = name[11:]
                    if filter_name == '':
                        raise Exception, ('PROPERTIES ERROR (%s): "filter_SQL_" should be followed by a filter name.\n'
                                          'Example: "filter_SQL_MyFilter = <QUERY>" would define a filter named "MyFilter" defined by\n'
                                          'a MySQL query "<QUERY>". See the README.'%(name))
                    if filter_name in self._filters.keys():
                        raise Exception, 'Filter "%s" is defined twice in properties file.'%(filter_name)
                    if filter_name in self._groups.keys():
                        raise Exception, 'Name "%s" is already taken for a group.'%(filter_name)
                    if re.search('\W', filter_name):
                        raise Exception, 'PROPERTIES ERROR (%s): Filter names may only contain alphanumeric characters and "_".'%(filter_name)
                    if not val:
                        logging.warn('PROPERTIES WARNING (%s): Undefined filter'%(name))
                        continue
                    self._filters[filter_name] = OldFilter(val)
                
                elif name == 'groups':
                    logging.warn('PROPERTIES WARNING (%s): This field is no longer necessary in the properties file.\n'
                              'Only the group_SQL_XXX and filter_SQL_XXX fields are needed when defining groups and filters.'%(name))
                    
                elif name == 'filters':
                    # Load new-style filters
                    if val.strip() == '':
                        logging.warn('PROPERTIES WARNING (filters): Field should not be left blank')
                        continue
                    d = eval(val)
                    if type(d) != dict:
                        raise Exception, 'PROPERTIES ERROR (filters): Error parsing filters. Check the "filters" field in your properties file.'
                    for k, v in d.items():
                        self._filters[k] = Filter.decode(v)
                    del d
                        
                elif name == 'gates':
                    d = eval(val)
                    if type(d) != dict:
                        raise Exception, 'PROPERTIES ERROR (gates): Error parsing gates. Check the "gates" field in your properties file.'
                    for k, v in d.items():
                        self.gates[k] = Gate.decode(v)
                    del d
                    
                else:
                    logging.warn('PROPERTIES WARNING: Unrecognized field "%s" in properties file'%(name))
                
        f.close()
        self.Validate()
        self._initialized = True
Example #2
0
    def load_file(self, filename):
        ''' Loads variables in from a properties file. '''
        from sqltools import Gate, Filter, OldFilter
        
        self.clear()
        self._filename        = filename
        self._groups          = {}
        self._filters         = ObservableDict()
        self.gates            = ObservableDict()

        f = open(filename, 'U')
        lines = f.read()
        self._textfile = lines     # store raw file
        lines = lines.split('\n')

        for idx in xrange(len(lines)):
            line = lines[idx]
            # skip commented and empty lines
            if not line.strip().startswith('#') and line.strip() != '':
                try:
                    (name, val) = line.split('=', 1)
                    name = name.strip()
                    val = val.strip()
                except ValueError:
                    raise Exception('PROPERTIES ERROR: Could not parse line #%d\n'
                                    '\t"%s"\n'
                                    'Did you accidentally load your training set instead of your properties file?'%(idx + 1, line))
                
                if name in string_vars:
                    self.__dict__[name] = val or None
                
                elif name in list_vars:
                    self.__dict__[name] = self.parse_list_value(val) or None
                    
                elif name.startswith('group_SQL_'):
                    group_name = name[10:]
                    if group_name == '':
                        raise Exception, ('PROPERTIES ERROR (%s): "group_SQL_" should be followed by a group name.\n'
                                          'Example: "group_SQL_MyGroup = <QUERY>" would define a group named "MyGroup" defined by\n'
                                          'a MySQL query "<QUERY>". See the README.'%(name))
                    if group_name in self._groups.keys():
                        raise Exception, 'Group "%s" is defined twice in properties file.'%(group_name)
                    if group_name in self._filters.keys():
                        raise Exception, 'Name "%s" is already taken for a filter.'%(group_name)
                    if not val:
                        logging.warn('PROPERTIES WARNING (%s): Undefined group'%(name))
                        continue
                    self._groups[group_name] = val
                    
                elif name.startswith('filter_SQL_'):
                    # Load old-style SQL filters:
                    filter_name = name[11:]
                    if filter_name == '':
                        raise Exception, ('PROPERTIES ERROR (%s): "filter_SQL_" should be followed by a filter name.\n'
                                          'Example: "filter_SQL_MyFilter = <QUERY>" would define a filter named "MyFilter" defined by\n'
                                          'a MySQL query "<QUERY>". See the README.'%(name))
                    if filter_name in self._filters.keys():
                        raise Exception, 'Filter "%s" is defined twice in properties file.'%(filter_name)
                    if filter_name in self._groups.keys():
                        raise Exception, 'Name "%s" is already taken for a group.'%(filter_name)
                    if re.search('\W', filter_name):
                        raise Exception, 'PROPERTIES ERROR (%s): Filter names may only contain alphanumeric characters and "_".'%(filter_name)
                    if not val:
                        logging.warn('PROPERTIES WARNING (%s): Undefined filter'%(name))
                        continue
                    self._filters[filter_name] = OldFilter(val)
                
                elif name == 'groups':
                    logging.warn('PROPERTIES WARNING (%s): This field is no longer necessary in the properties file.\n'
                              'Only the group_SQL_XXX and filter_SQL_XXX fields are needed when defining groups and filters.'%(name))
                    
                elif name == 'filters':
                    # Load new-style filters
                    if val.strip() == '':
                        logging.warn('PROPERTIES WARNING (filters): Field should not be left blank')
                        continue
                    d = eval(val)
                    if type(d) != dict:
                        raise Exception, 'PROPERTIES ERROR (filters): Error parsing filters. Check the "filters" field in your properties file.'
                    for k, v in d.items():
                        self._filters[k] = Filter.decode(v)
                    del d
                        
                elif name == 'gates':
                    d = eval(val)
                    if type(d) != dict:
                        raise Exception, 'PROPERTIES ERROR (gates): Error parsing gates. Check the "gates" field in your properties file.'
                    for k, v in d.items():
                        self.gates[k] = Gate.decode(v)
                    del d
                    
                else:
                    logging.warn('PROPERTIES WARNING: Unrecognized field "%s" in properties file'%(name))
                
        f.close()
        self.Validate()
        self._initialized = True
    def load_file(self, filename):
        ''' Loads variables in from a properties file. '''
        from sqltools import Gate, Filter, OldFilter
        
        self.clear()
        self._filename        = filename
        self._groups          = {}
        self._filters         = ObservableDict()
        self.gates            = ObservableDict()

        f = open(filename, 'U')
        lines = f.read()
        self._textfile = lines     # store raw file
        lines = lines.split('\n')

        for idx in xrange(len(lines)):
            line = lines[idx]
            # skip commented and empty lines
            if not line.strip().startswith('#') and line.strip() != '':
                try:
                    (name, val) = line.split('=', 1)
                    name = name.strip()
                    val = val.strip()
                except ValueError:
                    raise Exception('PROPERTIES ERROR: Could not parse line #%d\n'
                                    '\t"%s"\n'
                                    'Did you accidentally load your training set instead of your properties file?'%(idx + 1, line))
                
                if name in string_vars:
                    self.__dict__[name] = val or None
                
                elif name in list_vars:
                    self.__dict__[name] = self.parse_list_value(val) or None
                    
                elif name.startswith('group_SQL_'):
                    group_name = name[10:]
                    if group_name == '':
                        raise Exception('PROPERTIES ERROR (%s): "group_SQL_" should be followed by a group name.\n'
                                        'Example: "group_SQL_MyGroup = <QUERY>" would define a group named "MyGroup" defined by\n'
                                        'a MySQL query "<QUERY>". See the README.'%(name))
                    if group_name in self._groups.keys():
                        raise Exception('Group "%s" is defined twice in properties file.'%(group_name))
                    if group_name in self._filters.keys():
                        raise Exception('Name "%s" is already taken for a filter.'%(group_name))
                    if not val:
                        logging.warn('PROPERTIES WARNING (%s): Undefined group'%(name))
                        continue
                    self._groups[group_name] = val
                    
                elif name.startswith('filter_SQL_'):
                    # Load old-style SQL filters:
                    filter_name = name[11:]
                    if filter_name == '':
                        raise Exception('PROPERTIES ERROR (%s): "filter_SQL_" should be followed by a filter name.\n'
                                        'Example: "filter_SQL_MyFilter = <QUERY>" would define a filter named "MyFilter" defined by\n'
                                        'a MySQL query "<QUERY>". See the README.'%(name))
                    if filter_name in self._filters.keys():
                        raise Exception('Filter "%s" is defined twice in properties file.'%(filter_name))
                    if filter_name in self._groups.keys():
                        raise Exception('Name "%s" is already taken for a group.'%(filter_name))
                    if re.search('\W', filter_name):
                        raise Exception('PROPERTIES ERROR (%s): Filter names may only contain alphanumeric characters and "_".'%(filter_name))
                    if not val:
                        logging.warn('PROPERTIES WARNING (%s): Undefined filter'%(name))
                        continue
                    self._filters[filter_name] = OldFilter(val)
                
                elif name == 'groups':
                    logging.warn('PROPERTIES WARNING (%s): This field is no longer necessary in the properties file.\n'
                              'Only the group_SQL_XXX and filter_SQL_XXX fields are needed when defining groups and filters.'%(name))
                    
                elif name == 'filters':
                    # Load new-style filters
                    if val.strip() == '':
                        logging.warn('PROPERTIES WARNING (filters): Field should not be left blank')
                        continue
                    d = eval(val)
                    if type(d) != dict:
                        raise Exception('PROPERTIES ERROR (filters): Error parsing filters. Check the "filters" field in your properties file.')
                    for k, v in d.items():
                        self._filters[k] = Filter.decode(v)
                    del d
                        
                elif name == 'gates':
                    d = eval(val)
                    if type(d) != dict:
                        raise Exception('PROPERTIES ERROR (gates): Error parsing gates. Check the "gates" field in your properties file.')
                    for k, v in d.items():
                        self.gates[k] = Gate.decode(v)
                    del d
                    
                else:
                    logging.warn('PROPERTIES WARNING: Unrecognized field "%s" in properties file'%(name))
                
        f.close()
        
        #if classification_type is defined
        if self.field_defined('classification_type') and self.classification_type.lower() in ['image']:
            self.classification_type = 'image'
            #2 cases:
            # object_table/object_id/cell_x_loc/cell_y_loc exist and point to a table/view of object tables
            # object_table/object_id/cell_x_loc/cell_y_loc don't exist

            self.object_table = self.image_table + '_objecttable'

            self.object_id = 'ObjectNumber'
            self.cell_x_loc = 'Image_x_loc'
            self.cell_y_loc = 'Image_y_loc'
            self.object_name = ['image','images']

            # image_width and image_height refer to the full image width and height while 
            # image_tile_size refers to the size of tiles for classification
            # NB: if image_width and image_height are defined, they override image_tile_size
            if self.field_defined('image_width') and self.field_defined('image_height'):
                self.image_width, self.image_height = int(self.image_width), int(self.image_height)
                self.image_tile_size = min([self.image_width,self.image_height])

            self.image_tile_size = int(self.image_tile_size)
        else:
            self.classification_type = "object"

        # For image gallery
        if self.field_defined('image_size'):
            self.image_size = int(self.image_size)
        else:
            self.image_size = self.image_tile_size

        self.Validate()

        #if check_tables is defined
        if self.field_defined('check_tables') and self.check_tables.lower() in ['yes']:
            self.object_table = self.object_table + '_checked'
        self._initialized = True
    def load_file(self, filename):
        ''' Loads variables in from a properties file. '''
        from sqltools import Gate, Filter, OldFilter

        self.clear()
        self._filename = filename
        self._groups = {}
        self._filters = ObservableDict()
        self.gates = ObservableDict()

        f = open(filename, 'U')
        lines = f.read()
        self._textfile = lines  # store raw file
        lines = lines.split('\n')

        for idx in xrange(len(lines)):
            line = lines[idx]
            # skip commented and empty lines
            if not line.strip().startswith('#') and line.strip() != '':
                try:
                    (name, val) = line.split('=', 1)
                    name = name.strip()
                    val = val.strip()
                except ValueError:
                    raise Exception(
                        'PROPERTIES ERROR: Could not parse line #%d\n'
                        '\t"%s"\n'
                        'Did you accidentally load your training set instead of your properties file?'
                        % (idx + 1, line))

                if name in string_vars:
                    self.__dict__[name] = val or None

                elif name in list_vars:
                    self.__dict__[name] = self.parse_list_value(val) or None

                elif name.startswith('group_SQL_'):
                    group_name = name[10:]
                    if group_name == '':
                        raise Exception, (
                            'PROPERTIES ERROR (%s): "group_SQL_" should be followed by a group name.\n'
                            'Example: "group_SQL_MyGroup = <QUERY>" would define a group named "MyGroup" defined by\n'
                            'a MySQL query "<QUERY>". See the README.' %
                            (name))
                    if group_name in self._groups.keys():
                        raise Exception, 'Group "%s" is defined twice in properties file.' % (
                            group_name)
                    if group_name in self._filters.keys():
                        raise Exception, 'Name "%s" is already taken for a filter.' % (
                            group_name)
                    if not val:
                        logging.warn(
                            'PROPERTIES WARNING (%s): Undefined group' %
                            (name))
                        continue
                    self._groups[group_name] = val

                elif name.startswith('filter_SQL_'):
                    # Load old-style SQL filters:
                    filter_name = name[11:]
                    if filter_name == '':
                        raise Exception, (
                            'PROPERTIES ERROR (%s): "filter_SQL_" should be followed by a filter name.\n'
                            'Example: "filter_SQL_MyFilter = <QUERY>" would define a filter named "MyFilter" defined by\n'
                            'a MySQL query "<QUERY>". See the README.' %
                            (name))
                    if filter_name in self._filters.keys():
                        raise Exception, 'Filter "%s" is defined twice in properties file.' % (
                            filter_name)
                    if filter_name in self._groups.keys():
                        raise Exception, 'Name "%s" is already taken for a group.' % (
                            filter_name)
                    if re.search('\W', filter_name):
                        raise Exception, 'PROPERTIES ERROR (%s): Filter names may only contain alphanumeric characters and "_".' % (
                            filter_name)
                    if not val:
                        logging.warn(
                            'PROPERTIES WARNING (%s): Undefined filter' %
                            (name))
                        continue
                    self._filters[filter_name] = OldFilter(val)

                elif name == 'groups':
                    logging.warn(
                        'PROPERTIES WARNING (%s): This field is no longer necessary in the properties file.\n'
                        'Only the group_SQL_XXX and filter_SQL_XXX fields are needed when defining groups and filters.'
                        % (name))

                elif name == 'filters':
                    # Load new-style filters
                    if val.strip() == '':
                        logging.warn(
                            'PROPERTIES WARNING (filters): Field should not be left blank'
                        )
                        continue
                    d = eval(val)
                    if type(d) != dict:
                        raise Exception, 'PROPERTIES ERROR (filters): Error parsing filters. Check the "filters" field in your properties file.'
                    for k, v in d.items():
                        self._filters[k] = Filter.decode(v)
                    del d

                elif name == 'gates':
                    d = eval(val)
                    if type(d) != dict:
                        raise Exception, 'PROPERTIES ERROR (gates): Error parsing gates. Check the "gates" field in your properties file.'
                    for k, v in d.items():
                        self.gates[k] = Gate.decode(v)
                    del d

                else:
                    logging.warn(
                        'PROPERTIES WARNING: Unrecognized field "%s" in properties file'
                        % (name))

        f.close()

        #if classification_type is defined
        if self.field_defined('classification_type'
                              ) and self.classification_type.lower() in [
                                  'image'
                              ]:
            self.classification_type = 'image'
            #2 cases:
            # object_table/object_id/cell_x_loc/cell_y_loc exist and point to a table/view of object tables
            # object_table/object_id/cell_x_loc/cell_y_loc don't exist

            self.object_table = self.image_table + '_ObjectTable'

            self.object_id = 'ObjectNumber'
            self.cell_x_loc = 'Image_x_loc'
            self.cell_y_loc = 'Image_y_loc'
            self.object_name = ['image', 'images']

            # image_width and image_height refer to the full image width and height while
            # image_tile_size refers to the size of tiles for classification
            # NB: if image_width and image_height are defined, they override image_tile_size
            if self.field_defined('image_width') and self.field_defined(
                    'image_height'):
                self.image_width, self.image_height = int(
                    self.image_width), int(self.image_height)
                self.image_tile_size = min(
                    [self.image_width, self.image_height])

            self.image_tile_size = int(self.image_tile_size)
        else:
            self.classification_type = "object"

        # For image gallery
        if self.field_defined('image_size'):
            self.image_size = int(self.image_size)
        else:
            self.image_size = self.image_tile_size

        self.Validate()

        #if check_tables is defined
        if self.field_defined(
                'check_tables') and self.check_tables.lower() in ['yes']:
            self.object_table = self.object_table + '_Checked'
        self._initialized = True