# Experiments

## Data Prepration and Library Installation
### Download Data
Go to the 'data' folder and run the following command to download binary data.
```bash
bash get_datasets.sh binary
bash get_datasets.sh multilabel
bash get_datasets.sh regression
```

### Required Library Installation
**LIBLINEAR and LIBLINEAR-multicore**  
Navigate to the `liblinear` and `liblinear-multicore-2.47` directories, and run the following command to compile the libraries:
```bash
make
```

* **LIBLINEAR-multicore Python library**  
Navigate to `liblinear-multicore-2.47/python` directories, and run the following command to install python library:
```bash
pip install -e .
```
* **LibMultilabel libarary preparation**
1. **Clone the LibMultiLabel Repository**  
   Run the following command to clone the LibMultiLabel repository:

   ```bash
   git clone https://github.com/ntumlgroup/LibMultiLabel.git
    ```
2. Modify code for accessing pretrained kmeans model if you have pretrained label tree model.
    * Add a new argument kmeans_path in the add_all_arguments function of main.py:
        ```
        # Path
        parser.add_argument(
            "--kmeans_path",
            type=str,
            default=None,
            help="The path to kmeans model.",
        )
        ```
    * Pass the new argument `kmeans_path` to the `linear_train` function in `linear_trainer.py` (at line 57):
        ```
        model = LINEAR_TECHNIQUES[config.linear_technique](
                    datasets["train"]["y"],
                    datasets["train"]["x"],
                    config.liblinear_options,
                    config.tree_degree,
                    config.tree_max_depth,
        +           config.kmeans_path, # New argument
        )
        ```
    * Add the new argument path in the train_tree function of tree.py (lines 113-120):
        ```
        def train_tree(
            y: sparse.csr_matrix,
            x: sparse.csr_matrix,
            options: str = "",
            K=100,
            dmax=10,    
        +   path=None,
            verbose: bool = True,
        ) -> TreeModel:
        ```
        ```
        - root = _build_tree(label_representation, np.arange(y.shape[1]), 0, K, dmax)
        + if kmeans_path is None or kmeans_path == "":
        +    root = _build_tree(label_representation, np.arange(y.shape[1]), 0, K, dmax)
        + else:
        +    import pickle 
        +    with open(kmeans_path, 'rb') as file:
        +        root = pickle.load(file)
        + root.is_root = True 
        ```

3. Modify Saved model name

    In the `get_config` function of `main.py`, comment out the following code:

    ```
    config.run_name = "{}_{}_{}".format(
        config.data_name,
        Path(config.config).stem if config.config else config.model_name,
        datetime.now().strftime("%Y%m%d%H%M%S"),
    )
    ``` 
    Then, replace it with the following code to simplify the saved model name:
    ```
    config.run_name = config.data_name
    ``` 

## Generate the Experimental Results:
After downloading the data sets, you can generate the tables shown in the main paper.

* Table 1: `python gen_simple_observation.py --run --gen`
* Table 2: `python gen_table2.py --run --gen`
* Table 3: `python gen_table3.py`
* Table 4: `python gen_table4.py --run --gen`
* Table 5: `python gen_table5.py --run --gen`
* Table 6: `python gen_table6.py --run --gen`
* Table 7 and 8: `python gen_multilabel_tree.py --run --gen`
* Figure 1: `python plot_primal_dist_for_pruning.py`
* Table 12: `python gen_cddual_multicore.py --run --gen`
* Table 13: `python gen_pegasos_l1svm.py --run --gen`
* Table 14: `python gen_nSV_l1_l2.py --run --gen`
* Table 15 and 16: `python gen_multilabel_tree.py --run --gen`
* Table 17: `python gen_table_lr.py --run --gen`
* Table 18: `python verify_wstart_sparsity_svr.py`
* Table 20: `python gen_table_svr.py --run --gen`