{ "cells": [ { "cell_type": "markdown", "id": "3f81a577", "metadata": { "pycharm": { "name": "#%% md\n" } }, "source": [ "# Python API\n", "\n", "You can use polarityjam within python to do your analysis. A good tool to get an impression for the data you are about to analyse is jupyter notebook https://jupyter.org/). Paired with python micromamba and conda-forge it is a powerful option. The following will show how to use polarityjam in a python shell, or preferably in a jupyter notebook. What follows is an example of such a notebook. It can be found [here!](https://github.com/polarityjam/polarityjam/blob/main/docs/notebooks/polarityjam-notebook.ipynb)\n", "\n", "Lets first look at the API. There is four major parts to it:\n", "- Parameter\n", "- Segmentation\n", "- Extraction\n", "- Visualization\n", "\n", "We will walk you through each of them in here. First, familiarize with the API structure.\n", "\n", "\n" ] }, { "cell_type": "code", "id": "460d0f4d", "metadata": { "pycharm": { "name": "#%%\n" }, "ExecuteTime": { "end_time": "2025-04-16T11:46:14.127196Z", "start_time": "2025-04-16T11:46:14.124462Z" } }, "source": [ "# must run in an environment where polarityJaM is installed\n", "from polarityjam import Extractor, Plotter, PropertiesCollection\n", "from polarityjam import RuntimeParameter, PlotParameter, SegmentationParameter, ImageParameter, SegmentationMode\n", "from polarityjam import PolarityJamLogger\n", "from polarityjam import load_segmenter\n", "from polarityjam.utils.io import read_image\n", "\n", "from pathlib import Path" ], "outputs": [], "execution_count": 63 }, { "cell_type": "code", "id": "65f8625430f8c0ea", "metadata": { "ExecuteTime": { "end_time": "2025-04-16T11:46:14.192833Z", "start_time": "2025-04-16T11:46:14.187556Z" } }, "source": [ "# Setup a logger to only report WARNINGS, Put \"INFO\" or \"DEBUG\" to get more information\n", "plog = PolarityJamLogger(\"WARNING\")" ], "outputs": [], "execution_count": 64 }, { "cell_type": "markdown", "id": "1285ac7b", "metadata": { "pycharm": { "name": "#%% md\n" } }, "source": [ "## Parameter\n", "\n", "First, lets setup the data we want to use and the parameters describing what we want to do. You find the images in our example dataset that are part of Polarity-JaM.\n", "Here is the link to the [example data!](https://github.com/polarityjam/polarityjam/blob/main/src/polarityjam/test/resources/data.zip) Simply extract the zip and have it in the same directory as this notebook." ] }, { "cell_type": "code", "id": "0880cbee", "metadata": { "pycharm": { "name": "#%%\n" }, "ExecuteTime": { "end_time": "2025-04-16T11:46:14.232527Z", "start_time": "2025-04-16T11:46:14.229442Z" } }, "source": [ "### ADAPT ME ###\n", "path_root = Path(\"\")\n", "input_file = path_root.joinpath(\"data/golgi_nuclei/set_2/060721_EGM2_18dyn_02.tif\")\n", "output_path = path_root.joinpath(\"polarityjam_out/\")\n", "output_file_prefix = \"060721_EGM2_18dyn_02\"\n", "### ADAPT ME ###" ], "outputs": [], "execution_count": 65 }, { "cell_type": "markdown", "id": "8fe21a9c", "metadata": { "pycharm": { "name": "#%% md\n" } }, "source": [ "An image can be read in and needs to be further specified which channels hold what kind of information. For that the `ImageParameter` class should be used." ] }, { "cell_type": "code", "id": "bf565abe", "metadata": { "pycharm": { "name": "#%%\n" }, "ExecuteTime": { "end_time": "2025-04-16T11:46:14.301159Z", "start_time": "2025-04-16T11:46:14.283086Z" } }, "source": [ "# read input\n", "img = read_image(input_file)\n", "\n", "# describe your image with ImageParameter\n", "params_image = ImageParameter()\n", "\n", "# set the channels\n", "params_image.channel_organelle = 0 # golgi channel\n", "params_image.channel_nucleus = 2\n", "params_image.channel_junction = 3\n", "params_image.channel_expression_marker = 3\n", "params_image.pixel_to_micron_ratio = 2.4089\n", "\n", "print(params_image)" ], "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "ImageParameter: \n", "channel_junction 3 \n", "channel_nucleus 2 \n", "channel_organelle 0 \n", "channel_expression_marker 3 \n", "pixel_to_micron_ratio 2.4089 \n", "\n" ] } ], "execution_count": 66 }, { "cell_type": "markdown", "id": "691c8898", "metadata": { "pycharm": { "name": "#%% md\n" } }, "source": [ "Other parameters that should now be specified are `RuntimeParameter`, `PlotParameter`." ] }, { "cell_type": "code", "id": "73316822", "metadata": { "pycharm": { "name": "#%%\n" }, "ExecuteTime": { "end_time": "2025-04-16T11:46:14.347647Z", "start_time": "2025-04-16T11:46:14.337221Z" } }, "source": [ "# define other parameters, use default values\n", "params_runtime = RuntimeParameter()\n", "params_plot = PlotParameter()" ], "outputs": [], "execution_count": 67 }, { "cell_type": "markdown", "id": "d71dda3a", "metadata": { "pycharm": { "name": "#%% md\n" } }, "source": [ "Let's look at our parameter default values:" ] }, { "cell_type": "code", "id": "59f0365e", "metadata": { "pycharm": { "name": "#%%\n" }, "ExecuteTime": { "end_time": "2025-04-16T11:46:14.393792Z", "start_time": "2025-04-16T11:46:14.391118Z" } }, "source": [ "print(params_runtime)\n", "print(params_plot)" ], "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "RuntimeParameter: \n", "extract_group_features False \n", "extract_morphology_features True \n", "extract_polarity_features True \n", "extract_intensity_features True \n", "membrane_thickness 5 \n", "junction_threshold -1 \n", "feature_of_interest cell_area \n", "min_cell_size 50 \n", "min_nucleus_size 10 \n", "min_organelle_size 10 \n", "dp_epsilon 5 \n", "cue_direction 0 \n", "connection_graph True \n", "segmentation_algorithm CellposeSegmenter \n", "remove_small_objects_size 10 \n", "clear_border True \n", "save_sc_image False \n", "keyfile_condition_cols ['short_name'] \n", "\n", "PlotParameter: \n", "plot_junctions True \n", "plot_polarity True \n", "plot_elongation True \n", "plot_circularity True \n", "plot_marker True \n", "plot_ratio_method True \n", "plot_symmetry True \n", "plot_shape_orientation True \n", "plot_foi True \n", "plot_sc_image False \n", "plot_threshold_masks None \n", "plot_sc_partitions False \n", "show_statistics False \n", "show_polarity_angles True \n", "show_graphics_axis False \n", "show_scalebar True \n", "outline_width 2 \n", "length_scalebar_microns 20.0 \n", "length_unit pixel \n", "graphics_output_format ['png'] \n", "dpi 300 \n", "graphics_width 5 \n", "graphics_height 5 \n", "membrane_thickness 5 \n", "fontsize_text_annotations 6 \n", "font_color w \n", "marker_size 2 \n", "alpha 0.7 \n", "alpha_cell_outline 1.0 \n", "\n" ] } ], "execution_count": 68 }, { "cell_type": "markdown", "id": "e1d881a6", "metadata": { "pycharm": { "name": "#%% md\n" } }, "source": [ "Changing an of these is easy - and can be done as shown below:" ] }, { "cell_type": "code", "id": "73a51be21aa75547", "metadata": { "nbsphinx": "hidden", "pycharm": { "name": "#%%\n" }, "tags": [ "hide-cell", "\"nbsphinx\":", "\"hidden\"" ], "ExecuteTime": { "end_time": "2025-04-16T11:46:14.572622Z", "start_time": "2025-04-16T11:46:14.570655Z" } }, "source": "params_plot.dpi = 200", "outputs": [], "execution_count": 69 }, { "cell_type": "code", "id": "d9416b75", "metadata": { "pycharm": { "name": "#%%\n" }, "ExecuteTime": { "end_time": "2025-04-16T11:46:14.625894Z", "start_time": "2025-04-16T11:46:14.623465Z" } }, "source": [ "# change some parameters\n", "params_runtime.membrane_thickness = 6\n", "params_runtime.min_organelle_size = 9" ], "outputs": [], "execution_count": 70 }, { "cell_type": "markdown", "id": "aa6c864f", "metadata": { "pycharm": { "name": "#%% md\n" } }, "source": [ "to see all possible parameters and what values they can take see the Usage section of the documentation." ] }, { "cell_type": "markdown", "id": "6b003388", "metadata": { "pycharm": { "name": "#%% md\n" } }, "source": [ "### All parameters from single file\n", "\n", "Because there are a lot of parameters to set, we can also load all parameters from a single file. For every parameter that is not content of this file the default value is automatically taken. The code would look like this:" ] }, { "cell_type": "markdown", "id": "55397d0b", "metadata": { "pycharm": { "name": "#%% md\n" } }, "source": [ "