Saving and loading domains#

MuSpAn provides some additional functionality to save domains so that they can be easily loaded without regenerating them from scratch. This is especially useful when a domain has many labels, especially ones derived from spatial analyses (see later tutorials), which would be computationally expensive to repeat. In this tutorial, we briefly discuss saving and loading domains.

Let’s start by making a domain with some points chosen uniformly at random. We’ll add a label to each point at random.

[1]:
# Import necessary libraries
import muspan as ms
import numpy as np
import matplotlib.pyplot as plt

# Define the number of points
n_points = 500

# Generate random points
points = 100 * np.random.rand(n_points, 2)

# Create a domain and add points to it
domain = ms.domain('Random data')
domain.add_points(points, 'Random points')

# Generate random labels for the points
random_labels = np.random.rand(n_points)
domain.add_labels('Random label', random_labels)

# Visualize the domain with the random labels
plt.figure(figsize=(8, 5))
ms.visualise.visualise(domain, 'Random label', ax=plt.gca())
plt.show()
../../_images/_collections_getting_started_Getting_Started_-_4_-_Saving_and_loading_domains_2_0.png

Saving the domain is a straightforward process using the muspan.io module. We simply call ms.io.save_domain() with the domain we want to save, and the details about where we’d like to save it:

[2]:
# Define the directory and filename for saving the domain
directory = '.'  # Current directory
filename = 'Random_domain'

# Save the domain using muspan.io module
ms.io.save_domain(domain, path_to_save=directory, name_of_file=filename)
Domain Random_domain saved successfully to current directory.

This has generated a file named Random_domain.muspan, saved in your current directory. We can also use muspan.io to load in a previous saved domain. Let’s reload this domain, with a different name.

[3]:
# Load the previously saved domain from the file
another_domain = ms.io.load_domain('./Random_domain.muspan')
MuSpAn domain loaded successfully. Domain summary:
Domain name: Random data
Number of objects: 500
Collections: ['Random points']
Labels: ['Random label']
Networks: []
Distance matrices: []