# Path: pyfacades/labelme/annotation.py
# class Annotation(object):
#     def __init__(self, path=None, images_root=None, xml_root=None, collection=None):
#         super(Annotation, self).__init__()
# 
#         self.filename = ""
#         """The filename of the image that  was annotated"""
# 
#         self.folder = ""
#         """The folder on the LabelMe server that contains the image"""
# 
#         self.source = Source()
#         """Source of this annotation"""
# 
#         self.objects = []
#         """List of objects that have been annotated"""
# 
#         self.imagesize = ImageSize()
#         """The size fo the image"""
# 
#         if collection is None:
#             collection = Collection()
#             if path is not None:
#                 collection.guess_from_path(path)
# 
#         self.collection = collection
#         """The paths to local annotation and image files"""
#         assert isinstance(self.collection, Collection)
# 
#         if images_root is not None:
#             self.collection.image_root = images_root
# 
#         if xml_root is not None:
#             self.collection.xml_root = xml_root
# 
#         self.path = path
#         """The last annotation file we opened (iff we got it from a file)"""
# 
#         if path is not None:
#             with open(path) as f:
#                 self.parse_xml(f)
# 
#     def parse_xml(self, f, path=None):
#         """
# 
#         :param f: Either a string of XML or a file-like-object
#         :return:
#         """
#         data = xmltodict.parse(f)
# 
#         self.path = path
# 
#         annotation = data['annotation']
#         self.filename = annotation['filename']
#         self.folder = annotation['folder']
# 
#         if 'source' in annotation:
#             self.source.from_dict(annotation['source'])
# 
#         objects = annotation.get('object', [])
#         if isinstance(objects, dict):
#             objects = [objects]
# 
#         self.imagesize.set_from_dict(annotation.get('imagesize', {}))
# 
#         for o in objects:
#             new_object = Object()
#             if not isinstance(o, dict):
#                 print o
#                 print type(o)
#                 import ipdb; ipdb.set_trace()
#             new_object.set_from_dict(o)
#             self.objects.append(new_object)
# 
#     def remove_deleted(self):
#         self.objects = [o for o in self.objects if not o.deleted]
# 
#     def get_image_path(self):
#         """ Get the path to the image that corresponds to this annotation
#         """
#         root = self.collection.image_root
#         folder = self.folder
#         filename = self.filename
#         return os.path.join(root, folder, filename)
# 
#     def get_image(self):
#         """ Get the PIL image for this annotation
#         :rtype: Image.Image
#         :except IOError: If the image file cannot be found or the image cannot be opened.
#         """
#         return Image.open(self.get_image_path())
# 
#     def update_image_size(self):
#         """
#         :except IOError: If the file cannot be found or the image cannot be opened.
#         """
#         image = self.get_image()
#         self.imagesize.nrows = image.height
#         self.imagesize.ncols = image.width
# 
#     def __iter__(self):
#         for o in self.objects:
#             if not o.deleted:
#                 yield o


import os
import numpy as np
import pyfacades
from unittest import TestCase
from os.path import dirname
from pyfacades.labelme.annotation import Annotation
    def test_remove_deleted(self):
        a = Annotation()

        xml = open(ANNOTATION_PATH)
        a.parse_xml(xml)
        a.remove_deleted()
        self.assertEqual(len(a.objects), 83)
        self.assertEqual(a.objects[-1].name, 'balcony')
        self.assertEqual(a.objects[-2].name, 'obstruction')
        self.assertEqual(a.objects[0].name, 'window')

        # Make sure we have the expected set of names after removing the deleted items
        d = {}
        for o in a.objects:
            if o.name not in d:
                d[o.name] = []
            d[o.name].append(o.polygon.points)

        self.assertEqual(set(d.keys()),
                         {u'shop', u'door', u'molding', u'sill', u'sky', u'obstruction', u'window', u'facade',
                          u'cornice', u'balcony'})

        # Make sure we have the expected number of windows after removing deleted items
        self.assertEqual(len(d['window']), 34)


class TestPolygon(TestCase):
    def setUp(self):
        super(TestPolygon, self).setUp()
        self.annotation = Annotation(path=ANNOTATION_PATH, images_root=DATA_ROOT)

# Path: pyfacades/labelme/annotation.py
# class Annotation(object):
#     def __init__(self, path=None, images_root=None, xml_root=None, collection=None):
#         super(Annotation, self).__init__()
# 
#         self.filename = ""
#         """The filename of the image that  was annotated"""
# 
#         self.folder = ""
#         """The folder on the LabelMe server that contains the image"""
# 
#         self.source = Source()
#         """Source of this annotation"""
# 
#         self.objects = []
#         """List of objects that have been annotated"""
# 
#         self.imagesize = ImageSize()
#         """The size fo the image"""
# 
#         if collection is None:
#             collection = Collection()
#             if path is not None:
#                 collection.guess_from_path(path)
# 
#         self.collection = collection
#         """The paths to local annotation and image files"""
#         assert isinstance(self.collection, Collection)
# 
#         if images_root is not None:
#             self.collection.image_root = images_root
# 
#         if xml_root is not None:
#             self.collection.xml_root = xml_root
# 
#         self.path = path
#         """The last annotation file we opened (iff we got it from a file)"""
# 
#         if path is not None:
#             with open(path) as f:
#                 self.parse_xml(f)
# 
#     def parse_xml(self, f, path=None):
#         """
# 
#         :param f: Either a string of XML or a file-like-object
#         :return:
#         """
#         data = xmltodict.parse(f)
# 
#         self.path = path
# 
#         annotation = data['annotation']
#         self.filename = annotation['filename']
#         self.folder = annotation['folder']
# 
#         if 'source' in annotation:
#             self.source.from_dict(annotation['source'])
# 
#         objects = annotation.get('object', [])
#         if isinstance(objects, dict):
#             objects = [objects]
# 
#         self.imagesize.set_from_dict(annotation.get('imagesize', {}))
# 
#         for o in objects:
#             new_object = Object()
#             if not isinstance(o, dict):
#                 print o
#                 print type(o)
#                 import ipdb; ipdb.set_trace()
#             new_object.set_from_dict(o)
#             self.objects.append(new_object)
# 
#     def remove_deleted(self):
#         self.objects = [o for o in self.objects if not o.deleted]
# 
#     def get_image_path(self):
#         """ Get the path to the image that corresponds to this annotation
#         """
#         root = self.collection.image_root
#         folder = self.folder
#         filename = self.filename
#         return os.path.join(root, folder, filename)
# 
#     def get_image(self):
#         """ Get the PIL image for this annotation
#         :rtype: Image.Image
#         :except IOError: If the image file cannot be found or the image cannot be opened.
#         """
#         return Image.open(self.get_image_path())
# 
#     def update_image_size(self):
#         """
#         :except IOError: If the file cannot be found or the image cannot be opened.
#         """
#         image = self.get_image()
#         self.imagesize.nrows = image.height
#         self.imagesize.ncols = image.width
# 
#     def __iter__(self):
#         for o in self.objects:
#             if not o.deleted:
#                 yield o


# Path: pyfacades/labelme/test_annotation.py
import os
import numpy as np
import pyfacades
from unittest import TestCase
from os.path import dirname
from pyfacades.labelme.annotation import Annotation

    def test_remove_deleted(self):
        a = Annotation()

        xml = open(ANNOTATION_PATH)
        a.parse_xml(xml)
        a.remove_deleted()
        self.assertEqual(len(a.objects), 83)
        self.assertEqual(a.objects[-1].name, 'balcony')
        self.assertEqual(a.objects[-2].name, 'obstruction')
        self.assertEqual(a.objects[0].name, 'window')

        # Make sure we have the expected set of names after removing the deleted items
        d = {}
        for o in a.objects:
            if o.name not in d:
                d[o.name] = []
            d[o.name].append(o.polygon.points)

        self.assertEqual(set(d.keys()),
                         {u'shop', u'door', u'molding', u'sill', u'sky', u'obstruction', u'window', u'facade',
                          u'cornice', u'balcony'})

        # Make sure we have the expected number of windows after removing deleted items
        self.assertEqual(len(d['window']), 34)


class TestPolygon(TestCase):
    def setUp(self):
        super(TestPolygon, self).setUp()
        self.annotation = Annotation(path=ANNOTATION_PATH, images_root=DATA_ROOT)

        assert isinstance(self.annotation, Annotation)