Features.py 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  1. import pandas as pd
  2. import os.path
  3. from image_features_extraction import MyException
  4. class Features(object):
  5. """
  6. contain the dataframe
  7. """
  8. def __init__(self, data_frame):
  9. self.__data_frame = data_frame
  10. self.__class_name = ''
  11. self.__class_value=None
  12. def set_class_name(self, class_name):
  13. self.__class_name = class_name
  14. def set_class_value(self, class_value):
  15. self.__class_value = class_value
  16. def get_class_name(self):
  17. return self.__class_name
  18. def get_class_value(self):
  19. return self.__class_value
  20. def merge(self, Features_Obj, how_in='inner'):
  21. """
  22. Merges in the current Features object a second Features object (ex. obtained from the Voronoi Object)
  23. :param Features_Obj: External Features Object
  24. :type Features_Obj: Features (image_features_extraction package)
  25. :param how_in: 'inner', 'outer'
  26. :type how_in: string
  27. >>> import image_features_extraction.Images as fe
  28. >>> IMGS = fe.Images('../images/CA/1')
  29. >>> vor = IMG.Voronoi()
  30. >>> features1 = IMG.features(['area','perimeter','centroid','bbox', 'eccentricity'])
  31. >>> features2 = vor.features(['area','perimeter','centroid','bbox', 'eccentricity'])
  32. >>> features3 = features1.merge(features2, how_in='inner')
  33. >>> features3.get_dataframe().head()
  34. """
  35. df1 = Features_Obj.get_dataframe();
  36. df2 = pd.merge(self.__data_frame, df1, on='id', how=how_in)
  37. return Features(df2)
  38. def save(self, storage_name, type_storage='file', do_append=True):
  39. """
  40. save the current dataframe into the type of storage given in input.
  41. :param storage_name: storage name, (e.g., file name if ' storage type is 'file')
  42. :type storage_name: string
  43. :param type_storage: type of storage (default is 'file') ('db', 'json' will be future implementations)
  44. :type type_storage: string
  45. :param do_append: if True it appends to existing storage. If False it creates a new storage
  46. :type do_append: boolean
  47. if 'do_append=True': This version of the method does not check whether the new data are consistent with presisitng data into
  48. the existing storage. It just tries to append the date and might fail or not depending on the type of storage
  49. :returns: 1 if sucessuful otherwise 0
  50. :rtype: int
  51. :example:
  52. >>> import image_features_extraction as fe
  53. >>> imgs = fe.Images(folder_name)
  54. >>> img = imgs.item(1)
  55. >>> reg = img.Regions().item(1)
  56. >>> features = regs.get_features(['label', 'area','perimeter', 'centroid'], class_value=1)
  57. >>> features.save('my_file_name')
  58. """
  59. try:
  60. if type_storage == 'file':
  61. return self.__save_file(storage_name, do_append)
  62. else:
  63. raise MyException.MyException("error: storage type no specified or not found")
  64. return 0
  65. except Exception as e:
  66. print("one or more input labels might be wrong:{}".format(e))
  67. return 0
  68. def __save_file(self, file_name, do_append):
  69. """
  70. load the data from the local directory with the name indicated
  71. """
  72. if do_append==True:
  73. add_header = False
  74. if os.path.isfile(file_name) == False:
  75. add_header = True
  76. with open(file_name, 'a') as f: # it appends or creates a new file
  77. self.__data_frame.to_csv(f, header=add_header)
  78. else:
  79. with open(file_name, 'w') as f: # it creates a new file
  80. self.__data_frame.to_csv(f, header=True)
  81. return 1
  82. def get_dataframe(self, include_class=False):
  83. """
  84. creates a panda data frame contining the feature values
  85. :param include_class: if True if includes the class name and value as the last column
  86. :type include_class: Boolean
  87. """
  88. try:
  89. df = self.__data_frame.copy()
  90. if include_class == True:
  91. if self.__class_name == '':
  92. raise MyException.MyException("error: class name not set")
  93. if self.__class_value is None:
  94. raise MyException.MyException("error: class value not set")
  95. df[self.__class_name]=self.__class_value
  96. return df
  97. except MyException.MyException as e:
  98. print(e.args)
  99. return None