-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathloaddata_utils.py
106 lines (92 loc) · 3.59 KB
/
loaddata_utils.py
1
2
3
4
5
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
74
75
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
105
106
"""
This file contains functions to create different LoadData csvs for specific CellProfiler pipelines
and to edit these outputted csvs.
"""
import subprocess
import pathlib
import pandas as pd
def create_loaddata_csv(
index_directory: pathlib.Path,
config_path: pathlib.Path,
path_to_output: pathlib.Path,
):
"""
Create LoadData csv for CellProfiler (used for illum or zproj pipelines)
Parameters
----------
index_directory : pathlib.Path
path to the `Index.idx.xml` file for the plate (normally located in the /Images folder)
config_path : pathlib.Path
path to the `config.yml' file for pe2loaddata to process the csv
path_to_output : pathlib.Path
path to the `wave1_loaddata.csv' file used for generating the illumination correction functions for each channel
"""
command = [
"pe2loaddata",
"--index-directory",
str(index_directory),
str(config_path),
str(path_to_output),
]
subprocess.run(command, check=True)
print(f"{path_to_output.name} is created!")
def create_loaddata_illum_csv(
index_directory: pathlib.Path,
config_path: pathlib.Path,
path_to_output: pathlib.Path,
illum_directory: pathlib.Path,
plate_id: str,
illum_output_path: pathlib.Path,
):
"""
Create LoadData csv with illum correction functions for CellProfiler (used for analysis pipelines)
Parameters
----------
index_directory : pathlib.Path
path to the `Index.idx.xml` file for the plate (normally located in the /Images folder)
config_path : pathlib.Path
path to the `config.yml' file for pe2loaddata to process the csv
path_to_output : pathlib.Path
path to the `wave1_loaddata.csv' file used for generating the illumination correction functions for each channel
illum_directory : pathlib.Path
path to folder where the illumination correction functions (.npy files) are located
plate_id : str
string of the name of the plate to create the csv
illum_output_path : pathlib.Path
path to where the new csv will be created along with the name (e.g. path/to/wave1_loaddata_with_illum.csv)
"""
command = [
"pe2loaddata",
"--index-directory",
str(index_directory),
str(config_path),
str(path_to_output),
"--illum",
"--illum-directory",
str(illum_directory),
"--plate-id",
plate_id,
"--illum-output",
str(illum_output_path),
]
subprocess.run(command, check=True)
print(f"{illum_output_path.name} is created!")
def edit_loaddata_csv(path_to_loaddata_csv: pathlib.Path):
"""
This function loads in the loaddata csv and edit it to remove unnessecary rows and
correct the paths to the maximum projection images.
Parameters
----------
path_to_loaddata_csv : pathlib.Path
path to the loaddata csv to be edited
"""
loaddata_df = pd.read_csv(path_to_loaddata_csv)
# finds the last z-plane value and assigns it as a variable
# Metadata_PlaneID values can be any range of values, but this finds the max value
final_z = max(loaddata_df["Metadata_PlaneID"].unique())
# create df with only the rows with the last z-plane ID and edit path to the maximum projected images
loaddata_df = loaddata_df.loc[loaddata_df["Metadata_PlaneID"] == final_z]
loaddata_df = loaddata_df.replace(regex=r"Images", value="Maximum_Images")
# save the loaddata csv back to the same path
loaddata_df.to_csv(path_to_loaddata_csv, index=False)
print(f"{path_to_loaddata_csv.name} has been corrected!")