Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Clean up config file parameters #71

Merged
merged 4 commits into from
May 31, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 1 addition & 2 deletions scripts/configs/config.ini.example
Original file line number Diff line number Diff line change
Expand Up @@ -32,9 +32,8 @@ membrane_seg_channel = ${00BuildExperiment.round_R0:membrane_seg_channel}
[01FeatureExtraction]
excluded_plates = day2
excluded_wells = C02
ovr_channel = C01
name_ovr = regionprops_ovr_
iop_cutoff = 0.6
measure_morphology = True

[02OrganoidLinking]
iou_cutoff = 0.2
Expand Down
31 changes: 18 additions & 13 deletions scripts/prefect/01_feature_extraction.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@
parse_spacing,
summary_csv_path,
spacing_anisotropy_tuple,
str2bool
)
from scmultiplex.features.FeatureExtraction import (
extract_organoid_features,
Expand All @@ -58,10 +59,10 @@ def load_task(exp_path: str, excluded_plates: List[str], excluded_wells: List[st


@task()
def well_feature_extraction_ovr_task(well: WellRecord, ovr_channel: str, name_ovr: str):
def well_feature_extraction_ovr_task(well: WellRecord, org_seg_ch: str):
extract_well_features(
well=well,
ovr_channel=ovr_channel,
ovr_channel=org_seg_ch,
)
return

Expand Down Expand Up @@ -92,8 +93,8 @@ def get_organoids(

@task()
def organoid_feature_extraction_and_linking_task(
organoid, nuc_ending: str, mem_ending: str, mask_ending: str, spacing: List[float],
org_seg_ch, nuc_seg_ch, mem_seg_ch, ovr_channel, iop_cutoff):
organoid, nuc_ending: str, mem_ending: str, mask_ending: str, spacing: List[float], measure_morphology,
org_seg_ch, nuc_seg_ch, mem_seg_ch, iop_cutoff):

set_spacing(spacing)
extract_organoid_features(
Expand All @@ -102,14 +103,14 @@ def organoid_feature_extraction_and_linking_task(
mem_ending=mem_ending,
mask_ending=mask_ending,
spacing=tuple(spacing),
measure_morphology=True,
measure_morphology=measure_morphology,
organoid_seg_channel=org_seg_ch,
nuclear_seg_channel=nuc_seg_ch,
membrane_seg_channel=mem_seg_ch,
)
link_nuc_to_membrane(
organoid=organoid,
ovr_channel=ovr_channel,
ovr_channel=org_seg_ch,
nuc_ending=nuc_ending,
mask_ending=mask_ending,
mem_ending=mem_ending,
Expand All @@ -128,21 +129,20 @@ def run_flow(r_params, cpus):
exp_path = Parameter("exp_path")
excluded_plates = Parameter("excluded_plates")
excluded_wells = Parameter("excluded_wells")
ovr_channel = Parameter("ovr_channel")
mask_ending = Parameter("mask_ending")
nuc_ending = Parameter("nuc_ending")
mem_ending = Parameter("mem_ending")
name_ovr = Parameter("name_ovr")
iop_cutoff = Parameter("iop_cutoff")
spacing = Parameter("spacing")
measure_morphology = Parameter("measure_morphology")
org_seg_ch = Parameter("org_seg_ch")
nuc_seg_ch = Parameter("nuc_seg_ch")
mem_seg_ch = Parameter("mem_seg_ch")

exp, wells = load_task(exp_path, excluded_plates, excluded_wells)

wfeo_t = well_feature_extraction_ovr_task.map(
wells, unmapped(ovr_channel), unmapped(name_ovr)
wells, unmapped(org_seg_ch)
)

organoids = get_organoids(exp, mask_ending, excluded_plates, excluded_wells, upstream_tasks = [wfeo_t])
Expand All @@ -153,10 +153,10 @@ def run_flow(r_params, cpus):
unmapped(mem_ending),
unmapped(mask_ending),
unmapped(spacing),
unmapped(measure_morphology),
unmapped(org_seg_ch),
unmapped(nuc_seg_ch),
unmapped(mem_seg_ch),
unmapped(ovr_channel),
unmapped(iop_cutoff),
upstream_tasks = [organoids],
)
Expand All @@ -174,12 +174,11 @@ def get_config_params(config_file_path):

round_names = get_round_names(config_file_path)
config_params = {
'ovr_channel': ('01FeatureExtraction', 'ovr_channel'),
'name_ovr': ('01FeatureExtraction', 'name_ovr'),
'mask_ending': ('00BuildExperiment', 'mask_ending'),
'measure_morphology': ('01FeatureExtraction', 'measure_morphology'),
}
common_params = get_workflow_params(config_file_path, config_params)

compute_param = {
'excluded_plates': (
commasplit,[
Expand All @@ -196,11 +195,17 @@ def get_config_params(config_file_path):
('01FeatureExtraction', 'iop_cutoff')
]
),
'measure_morphology': (
str2bool,[
('01FeatureExtraction', 'measure_morphology')
]
),
'spacing': (
parse_spacing,[
('00BuildExperiment', 'spacing')
]
),

}
common_params.update(compute_workflow_params(config_file_path, compute_param))

Expand Down
21 changes: 11 additions & 10 deletions scripts/prefect/02_organoid_multiplex.py
Original file line number Diff line number Diff line change
Expand Up @@ -52,10 +52,10 @@ def get_wells(exp: Experiment, excluded_plates: List[str], excluded_wells: List[
return exclude_conditions(exp, excluded_plates, excluded_wells)

@task()
def link_organoids_and_get_stats_task(well, ovr_channel, folder_name, R0, RX, seg_name, RX_name, iou_cutoff, names):
def link_organoids_and_get_stats_task(well, org_seg_ch, folder_name, R0, RX, seg_name, RX_name, iou_cutoff, names):
link_organoids(
well=well,
ovr_channel=ovr_channel,
ovr_channel=org_seg_ch,
folder_name=folder_name,
R0=R0,
RX=RX,
Expand All @@ -69,7 +69,7 @@ def link_organoids_and_get_stats_task(well, ovr_channel, folder_name, R0, RX, se
RX=RX,
iou_cutoff=iou_cutoff,
names=names,
ovr_channel=ovr_channel,
ovr_channel=org_seg_ch,
logger=prefect.context.get("logger"),
)
return
Expand All @@ -87,7 +87,7 @@ def run_flow(r_params, cpus):
excluded_plates = Parameter("excluded_plates")
excluded_wells = Parameter("excluded_wells")
iou_cutoff = Parameter("iou_cutoff")
ovr_channel = Parameter("ovr_channel")
org_seg_ch = Parameter("org_seg_ch")

R0, RX = load_exps(R0_path, RX_path)
names = get_names(RX_name)
Expand All @@ -100,7 +100,7 @@ def run_flow(r_params, cpus):

link_organoids_and_get_stats_task.map(
wells,
unmapped(ovr_channel),
unmapped(org_seg_ch),
unmapped(folder_name),
unmapped(R0),
unmapped(RX),
Expand All @@ -125,10 +125,6 @@ def get_config_params(config_file_path):
if len(round_names) < 2:
raise RuntimeError('At least two rounds are required to perform organoid linking')
rounds_tobelinked = round_names[1:]
config_params = {
'ovr_channel': ('01FeatureExtraction', 'ovr_channel'),
}
common_params = get_workflow_params(config_file_path, config_params)

compute_param = {
'excluded_plates': (
Expand All @@ -153,11 +149,16 @@ def get_config_params(config_file_path):
]
),
}
common_params.update(compute_workflow_params(config_file_path, compute_param))
common_params = compute_workflow_params(config_file_path, compute_param)

round_tobelinked_params = {}
for ro in rounds_tobelinked:
config_params = {
'org_seg_ch': ('00BuildExperiment.round_%s' % ro, 'organoid_seg_channel'),
}
rp = common_params.copy()
rp.update(get_workflow_params(config_file_path, config_params))

rp['RX_name'] = ro
compute_param = {
'RX_path': (
Expand Down
11 changes: 2 additions & 9 deletions scripts/prefect/03_nuclear_multiplex.py
Original file line number Diff line number Diff line change
Expand Up @@ -60,10 +60,9 @@ def get_organoids_task(exp: Experiment, exlude_plates: List[str]):


@task()
def link_nuclei_task(organoid, ovr_channel, segname, rx_name, RX, z_anisotropy, org_seg_ch, nuc_seg_ch):
def link_nuclei_task(organoid, segname, rx_name, RX, z_anisotropy, org_seg_ch, nuc_seg_ch):
link_nuclei(
organoid=organoid,
ovr_channel=ovr_channel,
segname=segname,
rx_name=rx_name,
RX=RX,
Expand All @@ -83,7 +82,6 @@ def run_flow(r_params, cpus):
excluded_plates = Parameter("excluded_plates")
excluded_wells = Parameter("excluded_wells")
nuc_ending = Parameter("nuc_ending")
ovr_channel = Parameter("ovr_channel")
spacing = Parameter("spacing")
org_seg_ch = Parameter("org_seg_ch")
nuc_seg_ch = Parameter("nuc_seg_ch")
Expand All @@ -95,7 +93,6 @@ def run_flow(r_params, cpus):

link_nuclei_task.map(
r0_organoids,
unmapped(ovr_channel),
unmapped(nuc_ending),
unmapped(rx_name),
unmapped(RX),
Expand All @@ -119,10 +116,6 @@ def get_config_params(config_file_path):
if len(round_names) < 2:
raise RuntimeError('At least two rounds are required to perform organoid linking')
rounds_tobelinked = round_names[1:]
config_params = {
'ovr_channel': ('01FeatureExtraction', 'ovr_channel'),
}
common_params = get_workflow_params(config_file_path, config_params)

compute_param = {
'excluded_plates': (
Expand All @@ -147,7 +140,7 @@ def get_config_params(config_file_path):
]
),
}
common_params.update(compute_workflow_params(config_file_path, compute_param))
common_params = compute_workflow_params(config_file_path, compute_param)

# use same z-anisotropy as used during feature extraction
parsed_spacing = common_params['spacing']
Expand Down
4 changes: 4 additions & 0 deletions src/scmultiplex/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,10 @@ def commasplit(cstring):
return cstring.split(',')


def str2bool(bstring):
return bstring.lower() in ('true',)


def parse_spacing(spacing):
return tuple(float(v) for v in commasplit(spacing))

Expand Down
5 changes: 3 additions & 2 deletions src/scmultiplex/features/feature_wrapper.py
Original file line number Diff line number Diff line change
Expand Up @@ -242,13 +242,13 @@ def get_morphology_measurements(labeled_obj, img_shape, spacing, is_2D, min_area
"is_touching_border_z": is_touching_border_z(
labeled_obj, img_shape=img_shape
),
"volume_pix": labeled_obj["area"],
"surface_area": labeled_obj["surface_area_marchingcube"],
}
morphology_measurements.update(morphology_3d_only)

return morphology_measurements


def get_coordinates(labeled_obj, spacing, is_2D):
coordinate_measurements = {
"x_pos_pix": labeled_obj["centroid"][-1],
Expand All @@ -259,7 +259,8 @@ def get_coordinates(labeled_obj, spacing, is_2D):
coordinate_measurements_3D = {
"z_pos_pix_scaled": labeled_obj["centroid"][-3],
"z_pos_pix_img": labeled_obj["centroid"][-3]
/ spacing_anisotropy_scalar(spacing)
/ spacing_anisotropy_scalar(spacing),
"volume_pix": labeled_obj["area"]
}
coordinate_measurements.update(coordinate_measurements_3D)

Expand Down
6 changes: 3 additions & 3 deletions src/scmultiplex/linking/NucleiLinking.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ def load_linking_data(organoid: OrganoidRecord, rx_name: str, org_seg_ch):
return link_org, link_org_dict


def link_nuclei(organoid, ovr_channel, segname, rx_name, RX, z_anisotropy, org_seg_ch, nuc_seg_ch):
def link_nuclei(organoid, segname, rx_name, RX, z_anisotropy, org_seg_ch, nuc_seg_ch):
R0_obj = organoid.organoid_id
R0_id = int(R0_obj.rpartition("_")[2])
well_id = organoid.well.well_id
Expand All @@ -45,7 +45,7 @@ def link_nuclei(organoid, ovr_channel, segname, rx_name, RX, z_anisotropy, org_s
if R0_df_org["abs_min"][0] != 0:
try:
R0_df = organoid.get_measurement("regionprops_nuc_{}".format(nuc_seg_ch))
R0_raw = organoid.get_raw_data(ovr_channel)
R0_raw = organoid.get_raw_data(org_seg_ch)
R0_seg = organoid.get_segmentation(segname)
except Exception as e:
print(e)
Expand All @@ -64,7 +64,7 @@ def link_nuclei(organoid, ovr_channel, segname, rx_name, RX, z_anisotropy, org_s
RX.plates[plate_id]
.wells[well_id]
.organoids[RX_obj]
.get_raw_data(ovr_channel)
.get_raw_data(org_seg_ch)
)
RX_seg = (
RX.plates[plate_id]
Expand Down