001package com.astrolabsoftware.FinkBrowser.Januser;
002
003import com.Lomikel.HBaser.HBaseClient;
004import com.Lomikel.Utils.LomikelException;
005import com.astrolabsoftware.FinkBrowser.FinkPortalClient.FPC;
006
007// org.json
008import org.json.JSONArray;
009import org.json.JSONObject;
010
011// Java
012import java.util.Set;
013import java.util.TreeSet;
014import java.util.Map;
015import java.util.TreeMap;
016import java.util.List;
017import java.util.ArrayList;
018
019// Log4J
020import org.apache.logging.log4j.Logger;
021import org.apache.logging.log4j.LogManager;
022
023/** <code>FinkClassifier</code> classifies sources using Fink according to
024  * <a href="https://api.ztf.fink-portal.org/api">Fink Portal</a> REST service.
025  * @opt attributes
026  * @opt operations
027  * @opt types
028  * @opt visibility
029  * @author <a href="mailto:Julius.Hrivnac@cern.ch">J.Hrivnac</a> */
030public class FinkLSSTClassifier extends LSSTClassifier {
031  
032  @Override
033  public void classify(FinkGremlinRecipies recipies,
034                       String              oid) throws LomikelException {
035    // get all alerts (mjd) and their classes (cl)
036    Map<String, Map<String, String>> results;
037    Map<String, Set<String>> allInstances; // cl -> [mjd]
038    Map<String, Double>      allWeights;   // mjd -> w
039    String cl;
040    String mjd;
041    Set<String> jds;
042    allInstances = new TreeMap<>();
043    allWeights   = new TreeMap<>();
044    for (Map.Entry<String, HBaseClient> client : CLIENTS.entrySet()) {
045      results = client.getValue().scan(null,
046                                       "key:key:" + oid + ":substring",
047                                       "r:midpointMjdTai",
048                                       0,
049                                       false,
050                                       false);
051      if (results.size() > 0) {
052        cl = client.getKey();
053        for (Map.Entry<String, Map<String, String>> result : results.entrySet()) {
054          mjd = result.getValue().get("r:midpointMjdTai");
055          if (allInstances.containsKey(cl)) {
056            jds = allInstances.get(cl);
057            jds.add(mjd);
058            }
059          else {
060            jds = new TreeSet<String>();
061            jds.add(mjd);
062            allInstances.put(cl, jds);
063            }
064          allWeights.put(mjd, 1.0);          
065          }
066        }
067      }
068    // rearrange instances and weights and register
069    String key;
070    double weight;
071    double totalWeight;
072    double w;
073    totalWeight = 0;
074    List<String> instancesL;
075    List<Double> weightsL;
076    for (Map.Entry<String, Set<String>> cls : allInstances.entrySet()) {
077      for (String instance : cls.getValue()) {
078        totalWeight += allWeights.get(instance);
079        }
080      }
081    for (Map.Entry<String, Set<String>> cls : allInstances.entrySet()) {
082      key = cls.getKey();
083      instancesL = new ArrayList<String>(cls.getValue());
084      weightsL   = new ArrayList<Double>();
085      w = 0;
086      for (String instance : instancesL) {
087        weightsL.add(allWeights.get(instance));
088        w += allWeights.get(instance);
089        }
090      weight = w / totalWeight;
091      //log.info(key + " " + oid + " " + weight + " " + instancesL + " " + weightsL);
092      recipies.registerOCol(this, key, oid, weight, instancesL, weightsL);
093      }
094    }
095    
096  /** Initialise HBase connections. */
097  public static void init() {
098    CLIENTS = new TreeMap<String, HBaseClient>();
099    HBaseClient client;
100    for (String cls : CLASSES) {
101      try {
102        client = new HBaseClient("cchbase1.in2p3.fr", 2183);
103        client.connect(cls, "schema_4.1_8.39.0");
104        CLIENTS.put(cls, client);
105        }
106      catch (LomikelException e) {
107        log.error("Cannot connect to " + cls + " table");
108        }
109      }
110    }  
111  
112  private static Map<String, HBaseClient> CLIENTS;
113
114  private static Set<String> CLASSES = Set.of("rubin.tag_early_snia_candidate",
115                                              "rubin.tag_extragalactic_lt20mag_candidate",
116                                              "rubin.tag_extragalactic_new_candidate",
117                                              "rubin.tag_good_quality",
118                                              "rubin.tag_hostless_candidate",
119                                              "rubin.tag_in_tns",
120                                              "rubin.tag_sn_near_galaxy_candidate");
121   
122  /** Logging . */
123  private static Logger log = LogManager.getLogger(FinkLSSTClassifier.class);
124  
125  }
126           
127