Skip to content

Commit

Permalink
update file saving docs
Browse files Browse the repository at this point in the history
  • Loading branch information
rwood-97 committed Apr 23, 2024
1 parent e5d2312 commit 867b83a
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 41 deletions.
42 changes: 22 additions & 20 deletions docs/source/User-guide/Classify/Infer.rst
Original file line number Diff line number Diff line change
Expand Up @@ -124,46 +124,48 @@ As with the "test" dataset, to see a sample of your predictions, use:
my_classifier.show_inference_sample_results(label="railspace", set_name="infer")
Add predictions to metadata and save
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
To add your predictions to your patch metadata (saved in ``patch_df.csv``), you will need to add your predictions and confidence values to your ``infer`` dataset's dataframe.
Save predictions
~~~~~~~~~~~~~~~~~

This dataframe is saved as the datasets ``patch_df`` attribute.
To view it, use:
To save your predictions, use the ``.save_predictions()`` method.
e.g. to save your predictions on the "infer" dataset:

.. code-block:: python
infer.patch_df
my_classifier.save_predictions(set_name="infer")
To add your predictions and confidence values to this dataframe use:
.. code-block:: python
Add predictions to metadata and save
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

To add your predictions to your patch metadata (saved in ``patch_df.csv``), you will need to load your predictions as metadata in the ``MapImages`` object.

To do this, you will need to create a new ``MapImages`` object and load in your patches and parent images:

import numpy as np
.. code-block:: python
infer.patch_df['predicted_label'] = my_classifier.pred_label
infer.patch_df['pred'] = my_classifier.pred_label_indices
infer.patch_df['conf'] = np.array(my_classifier.pred_conf).max(axis=1)
from mapreader import load_patches
If you view your dataframe again (by running ``infer.patch_df`` as above), you will see your predictions and confidence values have been added as columns.
my_maps = load_patches(patch_paths = "./path/to/patches/*png", parent_paths="./path/to/parents/*png")
From here, you can either save your results using:
You can then add your predictions to the metadata using the ``.add_metadata()`` method:

.. code-block:: python
infer.patch_df.to_csv("predictions_patch_df.csv", sep=",")
my_maps.add_metadata("path_to_predictions_patch_df.csv", tree_level='patch') # add dataframe as metadata
Or, you can use the ``MapImages`` object to create some visualizations of your results:
For example, to load the predictions for the "infer" dataset:

.. code-block:: python
from mapreader import load_patches
#EXAMPLE
my_maps.add_metadata("./infer_predictions_patch_df.csv", tree_level='patch')
my_maps = load_patches(patch_paths = "./path/to/patches/*png", parent_paths="./path/to/parents/*png")
From here, you can use the ``.show_parent()`` method to visualize your predictions on the parent images as shown in the :doc:`Load </User-guide/Load>` user guide:

.. code-block:: python
infer_df = infer.patch_df.reset_index(names="image_id") # ensure image_id is one of the columns
my_maps.add_metadata(infer_df, tree_level='patch') # add dataframe as metadata
my_maps.add_shape()
parent_list = my_maps.list_parents()
Expand Down
43 changes: 22 additions & 21 deletions docs/source/User-guide/Classify/Train.rst
Original file line number Diff line number Diff line change
Expand Up @@ -631,46 +631,47 @@ As with the "test" dataset, to see a sample of your predictions, use:
my_classifier.show_inference_sample_results(label="railspace", set_name="infer")
Add predictions to metadata and save
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Save predictions
~~~~~~~~~~~~~~~~~

To add your predictions to your patch metadata (saved in ``patch_df.csv``), you will need to add your predictions and confidence values to your ``infer`` dataset's dataframe.

This dataframe is saved as the datasets ``patch_df`` attribute.
To view it, use:
To save your predictions, use the ``.save_predictions()`` method.
e.g. to save your predictions on the "infer" dataset:

.. code-block:: python
infer.patch_df
my_classifier.save_predictions(set_name="infer")
To add your predictions and confidence values to this dataframe use:
.. code-block:: python
Add predictions to metadata and save
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

To add your predictions to your patch metadata (saved in ``patch_df.csv``), you will need to load your predictions as metadata in the ``MapImages`` object.

To do this, you will need to create a new ``MapImages`` object and load in your patches and parent images:

import numpy as np
.. code-block:: python
infer.patch_df['predicted_label'] = my_classifier.pred_label
infer.patch_df['pred'] = my_classifier.pred_label_indices
infer.patch_df['conf'] = np.array(my_classifier.pred_conf).max(axis=1)
from mapreader import load_patches
If you view your dataframe again (by running ``infer.patch_df`` as above), you will see your predictions and confidence values have been added as columns.
my_maps = load_patches(patch_paths = "./path/to/patches/*png", parent_paths="./path/to/parents/*png")
From here, you can either save your results using:
You can then add your predictions to the metadata using the ``.add_metadata()`` method:

.. code-block:: python
infer.patch_df.to_csv("predictions_patch_df.csv", sep=",")
my_maps.add_metadata("path_to_predictions_patch_df.csv", tree_level='patch') # add dataframe as metadata
Or, you can use the ``MapImages`` object to create some visualizations of your results:
For example, to load the predictions for the "infer" dataset:

.. code-block:: python
from mapreader import load_patches
#EXAMPLE
my_maps.add_metadata("./infer_predictions_patch_df.csv", tree_level='patch')
my_maps = load_patches(patch_paths = "./path/to/patches/*png", parent_paths="./path/to/parents/*png")
From here, you can use the ``.show_parent()`` method to visualize your predictions on the parent images as shown in the :doc:`Load </User-guide/Load>` user guide:

.. code-block:: python
infer_df = infer.patch_df.reset_index(names="image_id") # ensure image_id is one of the columns
my_maps.add_metadata(infer_df, tree_level='patch') # add dataframe as metadata
my_maps.add_shape()
parent_list = my_maps.list_parents()
Expand Down

0 comments on commit 867b83a

Please sign in to comment.