from skmultilearn.dataset import load_dataset_dump, save_dataset_dump

"""
Explanation: Dataset handling
Scikit-multilearn provides methods to load, save and manipulate multi-label data sets in two formats:

a scikit-multilearn pickle of data set in scipy sparse format
the traditional ARFF file format

The functionality is provided in the :mod:skmultilearn.dataset module.
Scikit-multilearn also provides a repository of most popular benchmark data sets in the scipy sparse format and convienience functions to access them.
scikit-multilearn format
End of explanation
"""


X, y, feature_names, label_names = load_dataset_dump('_static/example.pkl.bz2')

X, y, feature_names[:3], label_names[:3]

save_dataset_dump(X[:10,:4], y[:10, :3], feature_names[:4], label_names[:3], filename=None)

"""
Explanation: Loading scikit-multilearn data format is easier as it stores more information than the ARFF file, all you need to do is specify the path to the data set file.
End of explanation
"""


from skmultilearn.dataset import available_data_sets

"""
Explanation: If the filename argument is not None this dictionary is saved as a bzip2 compressed pickle and the function does not return anything.
scikit-multilearn repository
End of explanation
"""


set([x[0] for x in available_data_sets().keys()])

"""
Explanation: The following benchmark data sets, originally provided in the MULAN data repository are provided in train, test, and undivided variants. The undivided variant contains the complete data set, before the train/test split.
End of explanation
"""


set([x[1] for x in available_data_sets().keys()])

"""
Explanation: Variants:
End of explanation
"""


from skmultilearn.dataset import load_dataset

X, y, feature_names, label_names = load_dataset('scene', 'train')

X, y, feature_names[:3], label_names[:3]

"""
Explanation: Scikit-multilearn can automatically download the data sets for you, similar to scikit-learn's data set API.
The data is stored by default in the subfolder scikit_ml_learn_data of your SCIKIT_ML_LEARN_DATA environment variable. If the variable is not set, the data is stored in ~/scikit_ml_learn_data.
To download a data set use the :meth:load_dataset function.
End of explanation
"""


from skmultilearn.dataset import load_from_arff

"""
Explanation: ARFF files
The most common way for storing multi-label data is the ARFF file format created by the WEKA library. You can find many benchmark data sets in ARFF format on the MULAN data repository.
Loading both dense and sparse ARFF files is simple in scikit-multilearn, just use :func:load_from_arff, like this:
End of explanation
"""


path_to_arff_file = '_static/example.arff'
label_count = 7

"""
Explanation: Loading multi-label ARFF files requires additional information as the number or placement of labels, is not indicated in the format directly.
End of explanation
"""


label_location="end"

"""
Explanation: Different software expects labels in different parts of the ARFF file:

MEKA expects labels to appear at the beginning of the file
MULAN expects labels to appear at the end of the file

As the example.arff comes from MULAN, we set the label location to end.
End of explanation
"""


arff_file_is_sparse = False

X, y = load_from_arff(
    path_to_arff_file, 
    label_count=label_count, 
    label_location=label_location,
    load_sparse=arff_file_is_sparse
)

"""
Explanation: There are two ways to save ARFF data:
- dense, where the file contains a complete dump of the data set row by row, including places where the value is 0
- sparse, as a dictionary of keys, where for each row the non-zero elements are listed with their index
The example file is not sparse, that's why we set the load_sparse argument to False
End of explanation
"""


X, y, feature_names, label_names = load_from_arff(
    path_to_arff_file, 
    label_count=label_count, 
    label_location=label_location,
    load_sparse=arff_file_is_sparse,
    return_attribute_definitions=True
)

"""
Explanation: Or if you also want the metadata: feature and label names:
End of explanation
"""


X, y, feature_names[:3], label_names[:3]

"""
Explanation: As you can see scikit-multilearn encodes nominal types by default as integers, and converts the input space to floats, while the output space to binary indicators 0/1 represented as integers. To change this behavior specify your own params to load_from_arff as described in the API documentation.
End of explanation
"""


from skmultilearn.dataset import save_to_arff

"""
Explanation: If you want to save ARFF files, you can use the :meth:save_arff function, which can both return a string containing an ARFF dump of the data set, or save it to a provided file when the filename argument is passed.
End of explanation
"""


print(save_to_arff(X[:10,:4], y[:10, :3], label_location='start', save_sparse=True))

"""
Explanation: Let's say we want to save a subset of the data in a sparse format and with labels at the begining of the file.
End of explanation
"""


