Skip to content

Viz Module API Reference

Widget Classes

Module for visualization

CORSHTTPRequestHandler

Bases: SimpleHTTPRequestHandler

Custom HTTP request handler with CORS support.

Source code in src/celldega/viz/local_server.py
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
class CORSHTTPRequestHandler(SimpleHTTPRequestHandler):
    """Custom HTTP request handler with CORS support."""

    def end_headers(self) -> None:
        """Add CORS headers to the response."""
        self.send_header("Access-Control-Allow-Origin", "*")
        self.send_header("Access-Control-Allow-Methods", "GET, OPTIONS")
        self.send_header(
            "Access-Control-Allow-Headers", "X-Requested-With, content-type, Authorization"
        )
        self.send_header("Access-Control-Allow-Credentials", "true")
        super().end_headers()

    def do_OPTIONS(self) -> None:
        """Handle OPTIONS requests for CORS preflight."""
        self.send_response(200)
        self.end_headers()

    def log_message(self, format: str, *args) -> None:
        """Override log_message to prevent logging to the console."""
        pass

do_OPTIONS()

Handle OPTIONS requests for CORS preflight.

Source code in src/celldega/viz/local_server.py
18
19
20
21
def do_OPTIONS(self) -> None:
    """Handle OPTIONS requests for CORS preflight."""
    self.send_response(200)
    self.end_headers()

end_headers()

Add CORS headers to the response.

Source code in src/celldega/viz/local_server.py
 8
 9
10
11
12
13
14
15
16
def end_headers(self) -> None:
    """Add CORS headers to the response."""
    self.send_header("Access-Control-Allow-Origin", "*")
    self.send_header("Access-Control-Allow-Methods", "GET, OPTIONS")
    self.send_header(
        "Access-Control-Allow-Headers", "X-Requested-With, content-type, Authorization"
    )
    self.send_header("Access-Control-Allow-Credentials", "true")
    super().end_headers()

log_message(format, *args)

Override log_message to prevent logging to the console.

Source code in src/celldega/viz/local_server.py
23
24
25
def log_message(self, format: str, *args) -> None:
    """Override log_message to prevent logging to the console."""
    pass

Landscape

Bases: AnyWidget

A widget for interactive visualization of spatial omics data. This widget currently supports iST (Xenium and MERSCOPE) and sST (Visium HD data)

Parameters:

Name Type Description Default
ini_x float

The initial x-coordinate of the view.

required
ini_y float

The initial y-coordinate of the view.

required
ini_zoom float

The initial zoom level of the view.

required
token str

The token traitlet.

required
base_url str

The base URL for the widget.

required
dataset_name str

The name of the dataset to visualize. This will show up in the user interface bar.

required

Attributes:

Name Type Description
component str

The name of the component.

technology str

The technology used.

base_url str

The base URL for the widget.

token str

The token traitlet.

ini_x float

The initial x-coordinate of the view.

ini_y float

The initial y-coordinate of the view.

ini_z float

The initial z-coordinate of the view.

ini_zoom float

The initial zoom level of the view.

dataset_name str

The name of the dataset to visualize.

update_trigger dict

The dictionary to trigger updates.

cell_clusters dict

The dictionary containing cell cluster information.

Returns:

Name Type Description
Landscape

A widget for visualizing a 'landscape' view of spatial omics data.

Source code in src/celldega/viz/widget.py
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
class Landscape(anywidget.AnyWidget):
    """
    A widget for interactive visualization of spatial omics data. This widget
    currently supports iST (Xenium and MERSCOPE) and sST (Visium HD data)

    Args:
        ini_x (float): The initial x-coordinate of the view.
        ini_y (float): The initial y-coordinate of the view.
        ini_zoom (float): The initial zoom level of the view.
        token (str): The token traitlet.
        base_url (str): The base URL for the widget.
        dataset_name (str, optional): The name of the dataset to visualize. This will show up in the user interface bar.

    Attributes:
        component (str): The name of the component.
        technology (str): The technology used.
        base_url (str): The base URL for the widget.
        token (str): The token traitlet.
        ini_x (float): The initial x-coordinate of the view.
        ini_y (float): The initial y-coordinate of the view.
        ini_z (float): The initial z-coordinate of the view.
        ini_zoom (float): The initial zoom level of the view.
        dataset_name (str): The name of the dataset to visualize.
        update_trigger (dict): The dictionary to trigger updates.
        cell_clusters (dict): The dictionary containing cell cluster information.

    Returns:
        Landscape: A widget for visualizing a 'landscape' view of spatial omics data.
    """

    _esm = pathlib.Path(__file__).parent / "../static" / "widget.js"
    _css = pathlib.Path(__file__).parent / "../static" / "widget.css"
    component = traitlets.Unicode("Landscape").tag(sync=True)

    technology = traitlets.Unicode("sst").tag(sync=True)
    base_url = traitlets.Unicode("").tag(sync=True)
    token = traitlets.Unicode("").tag(sync=True)
    creds = traitlets.Dict({}).tag(sync=True)
    ini_x = traitlets.Float().tag(sync=True)
    ini_y = traitlets.Float().tag(sync=True)
    ini_z = traitlets.Float().tag(sync=True)
    ini_zoom = traitlets.Float(0).tag(sync=True)
    square_tile_size = traitlets.Float(1.4).tag(sync=True)
    dataset_name = traitlets.Unicode("").tag(sync=True)
    region = traitlets.Dict({}).tag(sync=True)
    nbhd = traitlets.Dict({}).tag(sync=True)

    meta_cell = traitlets.Dict({}).tag(sync=True)
    meta_cluster = traitlets.Dict({}).tag(sync=True)
    umap = traitlets.Dict({}).tag(sync=True)
    landscape_state = traitlets.Unicode("spatial").tag(sync=True)

    update_trigger = traitlets.Dict().tag(sync=True)
    cell_clusters = traitlets.Dict().tag(sync=True)

    segmentation = traitlets.Unicode("default").tag(sync=True)

    width = traitlets.Int(0).tag(sync=True)
    height = traitlets.Int(800).tag(sync=True)

    def trigger_update(self, new_value):
        # This method updates the update_trigger traitlet with a new value
        # You can pass any information necessary for the update, or just a timestamp
        self.update_trigger = new_value

    def update_cell_clusters(self, new_clusters):
        # Convert the new_clusters to a JSON serializable format if necessary
        self.cell_clusters = new_clusters

Matrix

Bases: AnyWidget

A widget for interactive visualization of a hierarchically clustered matrix.

Parameters:

Name Type Description Default
value int

The value traitlet.

required
component str

The component traitlet.

required
network dict

The network traitlet.

required
click_info dict

The click_info traitlet.

required

Attributes:

Name Type Description
component str

The name of the component.

network dict

The network dictionary.

click_info dict

The click_info dictionary.

Returns:

Name Type Description
Matrix

A widget for visualizing a hierarchically clustered matrix.

Source code in src/celldega/viz/widget.py
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
class Matrix(anywidget.AnyWidget):
    """
    A widget for interactive visualization of a hierarchically clustered matrix.

    Args:
        value (int): The value traitlet.
        component (str): The component traitlet.
        network (dict): The network traitlet.
        click_info (dict): The click_info traitlet.

    Attributes:
        component (str): The name of the component.
        network (dict): The network dictionary.
        click_info (dict): The click_info dictionary.

    Returns:
        Matrix: A widget for visualizing a hierarchically clustered matrix.
    """

    _esm = pathlib.Path(__file__).parent / "../static" / "widget.js"
    _css = pathlib.Path(__file__).parent / "../static" / "widget.css"
    value = traitlets.Int(0).tag(sync=True)
    component = traitlets.Unicode("Matrix").tag(sync=True)

    network = traitlets.Dict({}).tag(sync=True)

    width = traitlets.Int(600).tag(sync=True)
    height = traitlets.Int(600).tag(sync=True)
    click_info = traitlets.Dict({}).tag(sync=True)

get_local_server()

Start a local HTTP server with CORS support and return the port number.

Returns:

Name Type Description
int int

The port number on which the server is running.

Source code in src/celldega/viz/local_server.py
28
29
30
31
32
33
34
35
36
37
38
39
40
def get_local_server() -> int:
    """Start a local HTTP server with CORS support and return the port number.

    Returns:
        int: The port number on which the server is running.
    """
    server = HTTPServer(("", 0), CORSHTTPRequestHandler)
    print(f"Server running on port {server.server_address[1]}")

    service = thr.Thread(target=server.serve_forever)
    service.start()

    return server.server_address[1]

landscape_matrix(landscape, mat, width='600px', height='700px')

Display a Landscape widget and a Matrix widget side by side.

Parameters:

Name Type Description Default
landscape Landscape

A Landscape widget.

required
mat Matrix

A Matrix widget.

required
width str

The width of the widgets.

'600px'
height str

The height of the widgets.

'700px'

Returns:

Type Description

Visualization display

Example: See example Landscape-Matrix_Xenium notebook

Source code in src/celldega/viz/__init__.py
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
def landscape_matrix(landscape, mat, width='600px', height='700px'):
    """
    Display a `Landscape` widget and a `Matrix` widget side by side.

    Args:
        landscape (Landscape): A `Landscape` widget.
        mat (Matrix): A `Matrix` widget.
        width (str): The width of the widgets.
        height (str): The height of the widgets.

    Returns:
        Visualization display

    Example:
    See example [Landscape-Matrix_Xenium](../../../examples/brief_notebooks/Landscape-Matrix_Xenium) notebook
    """

    # Use `jslink` to directly link `click_info` from `mat` to `trigger_value` in `landscape_ist`
    jslink((mat, 'click_info'), (landscape, 'update_trigger'))

    # Set layouts for the widgets
    mat.layout = Layout(width=width)  # Adjust as needed
    landscape.layout = Layout(width=width, height=height)  # Adjust as needed

    # Display widgets side by side
    widgets_side_by_side = HBox([landscape, mat])

    display(widgets_side_by_side)