Skip to content

Commit

Permalink
Merge pull request #89 from BodenmillerGroup/sort-analysis-stack-chan…
Browse files Browse the repository at this point in the history
…nels-by-mass

sort analysis stack channels by mass (closes #88)
  • Loading branch information
nilseling authored Apr 11, 2022
2 parents 804401f + 83a81d1 commit 8f236a8
Show file tree
Hide file tree
Showing 8 changed files with 233 additions and 192 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
# Changelog

## [3.2]

- sort channels by metal tag when creating the ilastik and full stacks

## [3.1]

- fixed git submodule issue
Expand Down
156 changes: 82 additions & 74 deletions scripts/download_examples.ipynb

Large diffs are not rendered by default.

4 changes: 3 additions & 1 deletion scripts/download_examples.py
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@
# Ilastik project
ilastik_project = Path("..") / "IMCWorkflow.ilp"
if not ilastik_project.exists():
request.urlretrieve("https://zenodo.org/record/6043544/files/IMCWorkflow.ilp", ilastik_project)
request.urlretrieve("https://zenodo.org/record/6449127/files/IMCWorkflow.ilp", ilastik_project)

# Sample metadata
sample_metadata = Path("..") / "sample_metadata.xlsx"
Expand All @@ -52,3 +52,5 @@

# %%
# !conda list

# %%
201 changes: 108 additions & 93 deletions scripts/imc_preprocessing.ipynb

Large diffs are not rendered by default.

18 changes: 13 additions & 5 deletions scripts/imc_preprocessing.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,16 @@
# !{sys.executable} -m pip install -e {Path.cwd().parent}

# %%
import imcsegpipe
import pandas as pd
import shutil

from pathlib import Path
from tempfile import TemporaryDirectory
from typing import List

import pandas as pd

import imcsegpipe
from imcsegpipe.utils import sort_channels_by_mass

# %% [markdown]
#
# # Preprocessing of IMC data for image segmentation
Expand Down Expand Up @@ -197,15 +199,19 @@
imcsegpipe.create_analysis_stacks(
acquisition_dir=acquisition_dir,
analysis_dir=final_images_dir,
analysis_channels=panel.loc[panel[panel_keep_col] == 1, panel_channel_col].tolist(),
analysis_channels=sort_channels_by_mass(
panel.loc[panel[panel_keep_col] == 1, panel_channel_col].tolist()
),
suffix="_full",
hpf=50.0,
)
# Write ilastik stack
imcsegpipe.create_analysis_stacks(
acquisition_dir=acquisition_dir,
analysis_dir=ilastik_dir,
analysis_channels=panel.loc[panel[panel_ilastik_col] == 1, panel_channel_col].tolist(),
analysis_channels=sort_channels_by_mass(
panel.loc[panel[panel_ilastik_col] == 1, panel_channel_col].tolist()
),
suffix="_ilastik",
hpf=50.0,
)
Expand Down Expand Up @@ -244,3 +250,5 @@

# %%
# !conda list

# %%
13 changes: 6 additions & 7 deletions src/imcsegpipe/__init__.py
Original file line number Diff line number Diff line change
@@ -1,16 +1,15 @@
from ._imcsegpipe import (
extract_zip_file,
match_txt_files,
extract_mcd_file,
create_analysis_stacks,
export_to_histocat,
extract_mcd_file,
extract_zip_file,
match_txt_files,
)


__all__ = [
"extract_zip_file",
"match_txt_files",
"extract_mcd_file",
"create_analysis_stacks",
"export_to_histocat",
"extract_mcd_file",
"extract_zip_file",
"match_txt_files",
]
16 changes: 8 additions & 8 deletions src/imcsegpipe/_imcsegpipe.py
Original file line number Diff line number Diff line change
@@ -1,19 +1,19 @@
import imageio
import logging
import numpy as np
import pandas as pd
import re
import shutil
import tifffile
import xtiff

from os import PathLike
from pathlib import Path
from readimc import MCDFile, TXTFile
from readimc.data import Acquisition, Panorama, Slide
from typing import Dict, List, Optional, Sequence, Union
from zipfile import ZipFile

import imageio
import numpy as np
import pandas as pd
import tifffile
import xtiff
from readimc import MCDFile, TXTFile
from readimc.data import Acquisition, Panorama, Slide

from .utils import AcquisitionMetadata, filter_hot_pixels, get_acquisition_ome_xml


Expand Down
13 changes: 9 additions & 4 deletions src/imcsegpipe/utils.py
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@
import re
from dataclasses import dataclass
from typing import List, Optional, Sequence
from xml.etree import ElementTree as ET

import numpy as np
import xtiff

from dataclasses import dataclass
from readimc import MCDFile
from readimc.data import Acquisition
from scipy.ndimage import maximum_filter
from typing import Optional, Sequence
from xml.etree import ElementTree as ET


@dataclass
Expand Down Expand Up @@ -156,3 +157,7 @@ def filter_hot_pixels(img: np.ndarray, thres: float) -> np.ndarray:
kernel[0, 1, 1] = False
max_neighbor_img = maximum_filter(img, footprint=kernel, mode="mirror")
return np.where(img - max_neighbor_img > thres, max_neighbor_img, img)


def sort_channels_by_mass(channels: Sequence[str]) -> List[str]:
return sorted(channels, key=lambda channel: int(re.sub("[^0-9]", "", channel) or 0))

0 comments on commit 8f236a8

Please sign in to comment.