001package com.astrolabsoftware.FinkBrowser.Januser;
002
003import com.Lomikel.Utils.LomikelException;
004import com.astrolabsoftware.FinkBrowser.FinkPortalClient.FPC;
005
006// org.json
007import org.json.JSONArray;
008import org.json.JSONObject;
009
010// Java
011import java.util.Set;
012import java.util.TreeSet;
013import java.util.Map;
014import java.util.TreeMap;
015import java.util.List;
016import java.util.ArrayList;
017
018// Log4J
019import org.apache.logging.log4j.Logger;
020import org.apache.logging.log4j.LogManager;
021
022/** <code>FinkClassifier</code> classifies sources using XMatch according to
023  * <a href="https://api.fink-portal.org/api">Fink Portal</a> REST service.
024  * @opt attributes
025  * @opt operations
026  * @opt types
027  * @opt visibility
028  * @author <a href="mailto:Julius.Hrivnac@cern.ch">J.Hrivnac</a> */
029public class XMatchClassifier extends Classifier {
030  
031  @Override
032  public void classify(FinkGremlinRecipies recipies,
033                       String              oid) throws LomikelException {
034    JSONArray ja;
035    JSONObject jo;
036    Map<String, Set<String>> allInstances; // cl -> [jd]
037    Map<String, Double>      allWeights;   // jd -> w
038    String cl;
039    String jd;
040    Set<String> jds;
041    String key;
042    ja = FPC.objects(new JSONObject().put("objectId",      oid   ).
043                                      put("output-format", "json"));
044    allInstances = new TreeMap<>();
045    allWeights   = new TreeMap<>();
046    // get all alerts (jd) and their classes
047    for (int i = 0; i < ja.length(); i++) {
048      jo = ja.getJSONObject(i);
049      cl = jo.getString("v:classification");
050      jd = String.valueOf(jo.getDouble("i:jd"));
051      if (!cl.equals("Unknown") && !FinkClassifier.finkClasses().contains(cl)) {
052        if (allInstances.containsKey(cl)) {
053          jds = allInstances.get(cl);
054          jds.add(jd);
055          }
056        else {
057          jds = new TreeSet<String>();
058          jds.add(jd);
059          allInstances.put(cl, jds);
060          }
061        allWeights.put(jd, 1.0);
062        }
063      }
064    // rearrange instances and weights and register
065    double weight;
066    double totalWeight;
067    double w;
068    totalWeight = 0;
069    List<String> instancesL;
070    List<Double> weightsL;
071    for (Map.Entry<String, Set<String>> cls : allInstances.entrySet()) {
072      for (String instance : cls.getValue()) {
073        totalWeight += allWeights.get(instance);
074        }
075      }
076    for (Map.Entry<String, Set<String>> cls : allInstances.entrySet()) {
077      key = cls.getKey();
078      instancesL = new ArrayList<String>(cls.getValue());
079      weightsL   = new ArrayList<Double>();
080      w = 0;
081      for (String instance : instancesL) {
082        weightsL.add(allWeights.get(instance));
083        w += allWeights.get(instance);
084        }
085      weight = w / totalWeight;
086      recipies.registerSoI(this, key, oid, weight, instancesL, weightsL);
087      }
088    }
089
090  /** Logging . */
091  private static Logger log = LogManager.getLogger(XMatchClassifier.class);
092  
093  }
094           
095