# Path: classification/metadata.py
# PRIMARY_VESSEL_CLASS_COLUMN = 'label'
# VESSEL_CLASS_DETAILED_NAMES = sorted(atomic(schema))
# VESSEL_CATEGORIES = sorted(categories(schema))
# TRAINING_SPLIT = 'Training'
# TEST_SPLIT = 'Test'
#     MAX_WEIGHT = 100.0
# def atomic(obj):
# def categories(obj, include_atomic=True):
# def stable_hash(x):
#     def __init__(self,
#                  metadata_dict,
#                  fishing_ranges_map):
#     def vessel_weight(self, id_):
#     def vessel_label(self, label_name, id_):
#     def ids_for_split(self, split):
#     def weighted_training_list(self,
#                                random_state,
#                                split,
#                                max_replication_factor,
#                                row_filter=lambda row: True,
#                                boundary=1):
#     def fishing_range_only_list(self, random_state, split):
# def read_vessel_time_weighted_metadata_lines(available_ids, lines,
#                                              fishing_range_dict, split):
# def read_vessel_time_weighted_metadata(available_ids,
#                                        metadata_file,
#                                        fishing_range_dict={},
#                                        split=0):
# def read_vessel_multiclass_metadata_lines(available_ids, lines,
#                                           fishing_range_dict):
# def metadata_file_reader(metadata_file):
# def read_vessel_multiclass_metadata(available_ids,
#                                     metadata_file,
#                                     fishing_range_dict={}):
# def find_available_ids(feature_path):
# def parse_date(date):
# def read_fishing_ranges(fishing_range_file):
# def build_multihot_lookup_table():
# def multihot_encode(label):
# class VesselMetadata(object):
# 
# Path: classification/metrics/ydump.py
# def precision_score(y_true, y_pred):
# def recall_score(y_true, y_pred):
# def f1_score(y_true, y_pred):
# def accuracy_score(y_true, y_pred, weights=None):
# def weights(labels, y_true, y_pred, max_weight=200):
# def base_confusion_matrix(y_true, y_pred, labels):
# def ydump_confusion_matrix(doc, cm, labels, **kwargs):
# def ydump_table(doc, headings, rows, **kwargs):
# def ydump_attrs(doc, results):
#     def RMS(a, b):
#     def MAE(a, b):
#     def RMS_MAE_by_label(true_attrs, pred_attrs, true_labels):
# def ydump_metrics(doc, results):
# def ydump_fishing_localisation(doc, results):


import os
import csv
import subprocess
import numpy as np
import pandas as pd
import pandas_gbq
import dateutil.parser
import logging
import argparse
import sys
import yattag
import gzip
import dateutil.parser
import datetime
import pytz
import six
from collections import namedtuple, defaultdict
from classification.metadata import VESSEL_CLASS_DETAILED_NAMES, VESSEL_CATEGORIES, schema, atomic
from .ydump import css, ydump_table
                --inference-table machine_learning_dev_ttl_120d.fishing_detection_vid_features_v20190509_  \
                --label-path classification/data/det_info_v20190507.csv \
                --dest-path ./test_fishing_inference_0509.html \
                --fishing-ranges classification/data/det_ranges_v20190507.csv

"""
from __future__ import division
from __future__ import absolute_import
from __future__ import print_function



coarse_categories = [
    'cargo_or_tanker', 'passenger', 'seismic_vessel', 'tug', 'other_fishing', 
    'drifting_longlines', 'seiners', 'fixed_gear', 'squid_jigger', 'trawlers', 
    'other_not_fishing']

coarse_mapping = defaultdict(set)
for k0, extra in [('fishing', 'other_fishing'), 
                  ('non_fishing', 'other_not_fishing')]:
    for k1, v1 in schema['unknown'][k0].items():
        key = k1 if (k1 in coarse_categories) else extra
        if v1 is None:
            coarse_mapping[key] |= {k1}
        else:
            coarse_mapping[key] |= set(atomic(v1))

coarse_mapping = [(k, coarse_mapping[k]) for k in coarse_categories]

fishing_mapping = [

# Path: classification/metadata.py
# PRIMARY_VESSEL_CLASS_COLUMN = 'label'
# VESSEL_CLASS_DETAILED_NAMES = sorted(atomic(schema))
# VESSEL_CATEGORIES = sorted(categories(schema))
# TRAINING_SPLIT = 'Training'
# TEST_SPLIT = 'Test'
#     MAX_WEIGHT = 100.0
# def atomic(obj):
# def categories(obj, include_atomic=True):
# def stable_hash(x):
#     def __init__(self,
#                  metadata_dict,
#                  fishing_ranges_map):
#     def vessel_weight(self, id_):
#     def vessel_label(self, label_name, id_):
#     def ids_for_split(self, split):
#     def weighted_training_list(self,
#                                random_state,
#                                split,
#                                max_replication_factor,
#                                row_filter=lambda row: True,
#                                boundary=1):
#     def fishing_range_only_list(self, random_state, split):
# def read_vessel_time_weighted_metadata_lines(available_ids, lines,
#                                              fishing_range_dict, split):
# def read_vessel_time_weighted_metadata(available_ids,
#                                        metadata_file,
#                                        fishing_range_dict={},
#                                        split=0):
# def read_vessel_multiclass_metadata_lines(available_ids, lines,
#                                           fishing_range_dict):
# def metadata_file_reader(metadata_file):
# def read_vessel_multiclass_metadata(available_ids,
#                                     metadata_file,
#                                     fishing_range_dict={}):
# def find_available_ids(feature_path):
# def parse_date(date):
# def read_fishing_ranges(fishing_range_file):
# def build_multihot_lookup_table():
# def multihot_encode(label):
# class VesselMetadata(object):
# 
# Path: classification/metrics/ydump.py
# def precision_score(y_true, y_pred):
# def recall_score(y_true, y_pred):
# def f1_score(y_true, y_pred):
# def accuracy_score(y_true, y_pred, weights=None):
# def weights(labels, y_true, y_pred, max_weight=200):
# def base_confusion_matrix(y_true, y_pred, labels):
# def ydump_confusion_matrix(doc, cm, labels, **kwargs):
# def ydump_table(doc, headings, rows, **kwargs):
# def ydump_attrs(doc, results):
#     def RMS(a, b):
#     def MAE(a, b):
#     def RMS_MAE_by_label(true_attrs, pred_attrs, true_labels):
# def ydump_metrics(doc, results):
# def ydump_fishing_localisation(doc, results):


# Path: classification/metrics/compute_fishing_metrics.py
import os
import csv
import subprocess
import numpy as np
import pandas as pd
import pandas_gbq
import dateutil.parser
import logging
import argparse
import sys
import yattag
import gzip
import dateutil.parser
import datetime
import pytz
import six
from collections import namedtuple, defaultdict
from classification.metadata import VESSEL_CLASS_DETAILED_NAMES, VESSEL_CATEGORIES, schema, atomic
from .ydump import css, ydump_table

                --inference-table machine_learning_dev_ttl_120d.fishing_detection_vid_features_v20190509_  \
                --label-path classification/data/det_info_v20190507.csv \
                --dest-path ./test_fishing_inference_0509.html \
                --fishing-ranges classification/data/det_ranges_v20190507.csv

"""
from __future__ import division
from __future__ import absolute_import
from __future__ import print_function



coarse_categories = [
    'cargo_or_tanker', 'passenger', 'seismic_vessel', 'tug', 'other_fishing', 
    'drifting_longlines', 'seiners', 'fixed_gear', 'squid_jigger', 'trawlers', 
    'other_not_fishing']

coarse_mapping = defaultdict(set)
for k0, extra in [('fishing', 'other_fishing'), 
                  ('non_fishing', 'other_not_fishing')]:
    for k1, v1 in schema['unknown'][k0].items():
        key = k1 if (k1 in coarse_categories) else extra
        if v1 is None:
            coarse_mapping[key] |= {k1}
        else:
            coarse_mapping[key] |= set(atomic(v1))

coarse_mapping = [(k, coarse_mapping[k]) for k in coarse_categories]

fishing_mapping = [

    ['fishing', set(atomic(schema['unknown']['fishing']))],