#-*- coding: utf-8 -*-

import object_detector.file_io as file_io
import os
import object_detector.descriptor as desc
import object_detector.extractor as extractor_


def setup_extractor(datafile = None):
    descriptor = desc.HOG(9, [4,4], [2,2])
    extractor = extractor_.FeatureExtractor(descriptor, [32, 16], datafile)
    return extractor 

def setup_params():
    annotation_filename = "../datasets/positive/digitStruct.json"
    negative_dir = "../datasets/negative"
    output_file = "svhn_features.hdf5"
    return annotation_filename, negative_dir, output_file

def test_add_positive_behavior():
    
    extractor = setup_extractor()
    annotation_filename, negative_dir, output_file = setup_params()

    # 2. Get Feature sets
    extractor.add_positive_sets(annotation_file=annotation_filename,
                             sample_ratio=1.0,
                             padding=0,
                             )

    features, labels = extractor.get_dataset(include_hard_negative=True)
    assert features.shape == (8, 756)
    assert labels.shape == (8, 1)
    

def test_add_negative_behavior():

    extractor = setup_extractor()
    annotation_filename, negative_dir, output_file = setup_params()
      
    # Todo : positive sample 숫자에 따라 negative sample 숫자를 자동으로 정할 수 있도록 설정
    extractor.add_negative_sets(image_dir=negative_dir,
                             pattern="*.jpg",
                             n_samples_per_img=10,
                             sample_ratio=1.0)
    
    features, labels = extractor.get_dataset(include_hard_negative=True)
    assert features.shape == (40, 756)
    assert labels.shape == (40, 1)


def test_save_and_load_extractor():
    extractor = setup_extractor()
    annotation_filename, negative_dir, output_file = setup_params()
      
    extractor.add_positive_sets(annotation_file=annotation_filename,
                             sample_ratio=1.0,
                             padding=0,
                             )
    extractor.add_negative_sets(image_dir=negative_dir,
                             pattern="*.jpg",
                             n_samples_per_img=10,
                             sample_ratio=1.0)
    
    # 3. Save dataset
    extractor.save(data_file=output_file)
    
    features, labels = extractor.get_dataset()
    features_loaded = file_io.FileHDF5().read(output_file, "features")
    labels_loaded = file_io.FileHDF5().read(output_file, "labels")
    
    assert features.all() == features_loaded.all()
    assert labels.all() == labels_loaded.all()
    
    extractor_loaded = setup_extractor(output_file)
    features_loaded, labels_loaded = extractor_loaded.get_dataset()

    assert features.all() == features_loaded.all()
    assert labels.all() == labels_loaded.all()
    os.remove(output_file)


if __name__ == "__main__":
    import pytest
    pytest.main([__file__])
    






@@ -579,24 +579,23 @@
 est_add_
-positive
+dataset
 _behavio
@@ -733,16 +733,21 @@
 re sets%0A
+    %0A
     extr
@@ -929,376 +929,12 @@
   )%0A
-%0A
     
-features, labels = extractor.get_dataset(include_hard_negative=True)%0A    assert features.shape == (8, 756)%0A    assert labels.shape == (8, 1)%0A    %0A%0Adef test_add_negative_behavior():%0A%0A    extractor = setup_extractor()%0A    annotation_filename, negative_dir, output_file = setup_params()%0A      %0A    # Todo : positive sample %EC%88%AB%EC%9E%90%EC%97%90 %EB%94%B0%EB%9D%BC negative sample %EC%88%AB%EC%9E%90%EB%A5%BC %EC%9E%90%EB%8F%99%EC%9C%BC%EB%A1%9C %EC%A0%95%ED%95%A0 %EC%88%98 %EC%9E%88%EB%8F%84%EB%A1%9D %EC%84%A4%EC%A0%95
 %0A   
@@ -1126,28 +1126,24 @@
 _ratio=1.0)%0A
-    
 %0A    feature
@@ -1212,78 +1212,306 @@
     
-assert features.shape == (40, 756)%0A    assert labels.shape == (40, 1)%0A
+%0A    n_positive_data = 8%0A    n_negative_data = 40%0A    assert features.shape == (n_positive_data + n_negative_data, 756)%0A    assert labels.shape == (n_positive_data + n_negative_data, 1)%0A    assert len(features%5Blabels %3E 0%5D) == n_positive_data%0A    assert len(features%5Blabels %3C= 0%5D) == n_negative_data
 %0A%0Ade

test code 수정