# Reversing the 'directionality' of classifiers created in SimBA

<p align="center">
<img src="https://github.com/sgoldenlab/simba/blob/master/images/reversering_1.PNG" />
</p>

### BACKGROUND

In this scenario, the user has already created a classifier in SimBA within a two-animal protocol. The classifier the user created has a "direction". This means that the created classifer takes into account which **specific** animal is doing the classified behavior, or alternative, the specific relationship between the two animals within a classified social interaction. Now the user wants a straighforward way of creating a second classifier with the reversed "direction", for example: 

* The original classifier detects when **animal 1** rears, but not when **animal 2** rears. The user wants a second classifier that detects when **animal 2** rears, but not **animal 1** rears. The user will then run the classifiers in tandem to get metrics for when either animals rears seperately. 

* The original classifier detects when **animal 1** pursues **animal 2**. The user wants a second classifier that detects when **animal 2** pursues **animal 1**. The user will then run the classifiers in tandem to get metrics for when either animal pursues the other seperatey.

* The original classifier detects when **animal 1** sniffs **animal 2**. The user wants a second classifier that detects when **animal 2** sniffs **animal 1**. The user will then run the classifiers in tandem to get metrics for when either animal sniffs the other seperatey. 

For example, [this video](https://www.youtube.com/watch?v=0OIFysQvUCI&list=PLi5Vwf0hhy1R6NDQJ3U28MOUJPfl2YWYl&index=17) contains 8 classifiers 
(approaching, sniffing, walking away, and rearing, for each of the two animals) that were created from annotations for only 4 classifiers (approaching, sniffing, walking away, and rearing, for animal 1 only). The directionality of each of these 4 classifiers were reversed in SimBA using the tool documented here, and then these new files were used to create 4 additional classifiers. 

>Note 1: This tool will only work if the behaviour of interest looks the same regardless of the directionality of the behavior. For example, if the sniffing/rearing/pursuit behavior of animal 1 isn't **the same** as when performed by animal 2, then a simple reversal as documented here won't work, and you need to go back and [create a new classifier from scratch](https://github.com/sgoldenlab/simba/blob/master/docs/Scenario1.md) with annotations for the reversed behavior. An example of this situation could be that the animals are different strains/species/sizes and rearing therefore looks different when performed by the two animals.

>Note 2: This tool will only work if the original classifier has inherent directionality in the behavior it classifies. For example, if the first, original, classifier was generated by using human-annotations of when both animal 1 and animal 2 sniffs/rears/pursuits, a simple reversal won't work as as there is nothing to reverse. You would need to go back and [create a new classifier from scratch](https://github.com/sgoldenlab/simba/blob/master/docs/Scenario1.md) with annotations for the behavior with directionality in mind.

>Note 3: How this direction reversal works can sometimes be a struggle to understand - I lose my train of thought when trying to explain it more often than not. However, in short, all the features that correlated with the human-made annotations to successfully train the original classifier remains the same, it's just that we assign those features to another animal. In other words, the strong correlations between the annotations and features remain, it's just that the names of those columns have been switched in the newly created - reversed - classifier.  


### Step 1 

Reversing a classifier requires there to be an original classifier, or muliple classifiers, within your project to reverse. Thus, you need to first have created a classifier as documented [HERE](https://github.com/sgoldenlab/simba/blob/master/docs/Scenario1.md).

Within you project folder for the classifier (or multiple classifiers) you want to reverse, make sure the `project_folder/csv` sub-directories are populated with files. Specifically, the (i) `project_folder/csv/outlier_corrected_movement_location`, (ii) `project_folder/csv/features_extracted`, and (iii)`project_folder/csv/targets_inserted` directories needs to contain data files, with one file in each directory representing the videos you want to reverse your annotations for. 

The files in the `project_folder/csv/outlier_corrected_movement_location` directory should have been created when you [corrected, or clicked to skip the outlier correction](https://github.com/sgoldenlab/simba/blob/master/docs/tutorial.md#step-4-outlier-correction), for your original classifier. 

The files in the `project_folder/csv/features_extracted` directory should have been created when you [clicked to peform feature extraction](https://github.com/sgoldenlab/simba/blob/master/docs/tutorial.md#step-5-extract-features), for your original classifier. 

The files in the `project_folder/csv/targets_inserted` directory should have been created when you [labelled behaviours using the SimBA behavioral annotation interface](https://github.com/sgoldenlab/simba/blob/master/docs/tutorial.md#step-6-label-behavior) or [appended third-party annotations](https://github.com/sgoldenlab/simba/blob/master/docs/third_party_annot.md), for your original classifier. 

### Step 2

[Load your SimBA project](https://github.com/sgoldenlab/simba/blob/master/docs/tutorial.md#step-1-load-project-config) and navigate to the tab `Further imports (data/video/frames)` tab. Locate the `Reverse tracking identity button`, similar to the one shown below. 

below. Click on `Reverse ID`
<p align="left">
<img src=/images/reverseid.PNG />
</p>

Before clicking the button, it may be a good idea to back up your project - reversing tracking data, especially when reversed and reversed back, can quickly cause confusing when you don't remember which data has been reversed and which hasn't. 

Clicking the button will cayse the identy of the animals to become reversed, and new files will be generated in your `project_folder/csv/outlier_corrected_movement_location`, `project_folder/csv/features_extracted`, and `project_folder/csv/targets_inserted` directories. The original, non-reversed files, will be but into a back-up subfolder in each of these directories named `Non_reversed` followed by a timestamp - just in case you decided not to back-up and this gives you a chance to access them later - as shown in this image below. 

<p align="center">
<img src=/images/reversebackend.PNG />
</p>

### Step 3

Navigate to the the `[Train machine model]` tab and train your new machine model, as documented [HERE](https://github.com/sgoldenlab/simba/blob/master/docs/tutorial.md#step-7-train-machine-model). SimBA will use your newly created reversed data by default. If you have a previous classifier with the same name in the project_folder, we recommend renaming the classifier `.sav` file and saving it somewhere else so not to become confused with which classifier has which directionality. 

After training your reversed classifier, you can go ahead and use it on new data as documented [HERE](https://github.com/sgoldenlab/simba/blob/master/docs/Scenario2.md#part-3-run-the-classifier-on-new-data)



#
Author [Simon N](https://github.com/sronilsson), [JJ Choong](https://github.com/inoejj)



