//Source file: D:\\Projects\\src\\yi\\iu\\imgdb\\learn\\ObjGMMBayCIAdapter.java

package yi.iu.imgdb.learn;

import java.util.*;
import yi.stat.em.*;
import yi.util.data.*;


public class ObjGMMBayCIAdapter extends ObjGMMBayAdapter
{

  public ObjGMMBayCIAdapter(ImgDataCenter data_center, LinkedList img_list, LinkedList ft_list, LinkedList obj_list)
  {
    super();
    // copy from  StatModelAdapter(ImgDataCenter data_center, LinkedList img_list, LinkedList ft_list, LinkedList obj_list)
    m_imgDataCenter = data_center;
    m_featureList = (ft_list != null)? ft_list: data_center.getFeatureList();
    m_imageList = (img_list != null)? img_list: data_center.getImageList();
    m_objectList = (obj_list != null)? obj_list: data_center.getObjectList();


    // this method only learns on one feature
    m_feature = (String)m_featureList.getFirst();

    int feature_len = data_center.getFeatureLength(m_feature);
//    int feature_len = m_imgDataCenter.getFeatureLength(m_feature);

    // get the image->objects map and images->feature map
    HashMap feature_map = new HashMap();
    HashMap object_map = new HashMap();
    for (Iterator i = m_imageList.iterator(); i.hasNext(); ) {
      String img_name = (String) i.next();
      // get the image feature vectors
      double[][] feature_vects = data_center.getImgFeatureList(img_name,
          m_feature).as2DDoubleArray();
      LinkedList ft_vt_list = Conversion.toLinedList(feature_vects);
      feature_map.put(img_name, ft_vt_list);

      // get the image description keywords
      LinkedList desc_list = (LinkedList) data_center.getImgDescriptions(
          img_name).clone();
      for (Iterator j = desc_list.iterator(); j.hasNext(); ) {
        String desc = (String) j.next();
        // check whether the description is in the object list to learn
        // if not, just remove it
        if (!m_objectList.contains(desc))
          j.remove();
      }
      object_map.put(img_name, desc_list);

    }

    // learn the objects one by one
    for (Iterator o = m_objectList.iterator(); o.hasNext(); ) {
      Object object = o.next();

      System.out.println();
      System.out.println("learning on \"" + object + "\"" + "\n");

      // get training set
      LinkedList posTrainImgs = getPositiveInstances(img_list, object);

      // number of images having the object (it will be used for p(o)
      m_weights.putScore(object, posTrainImgs.size());

      GaussianMixtureModel gmm =
          new ControlledInitMultiGMM(feature_map, object_map, m_objectList, object); // suppose the data is within [0,1)
      gmm.learnTheModel();

      m_models.put(object, gmm);

    } // end loop of objects

  }
}
