Spaces:
Sleeping
Sleeping
Update Background_Substraction.py
Browse files- Background_Substraction.py +125 -1
Background_Substraction.py
CHANGED
@@ -50,6 +50,8 @@ warnings.filterwarnings('ignore', category= UserWarning)
|
|
50 |
|
51 |
#set_name = 'Set_A'
|
52 |
#set_name = 'test'
|
|
|
|
|
53 |
present_dir = os.path.dirname(os.path.realpath(__file__))
|
54 |
|
55 |
input_path = os.path.join(present_dir, 'wetransfer_data-zip_2024-05-17_1431')
|
@@ -104,7 +106,6 @@ print('output_images_dir :', output_images_dir)
|
|
104 |
print('metadata_dir :', metadata_dir)
|
105 |
print('metadata_images_dir :', metadata_images_dir)
|
106 |
|
107 |
-
|
108 |
# ## II.3. FILES
|
109 |
#Don't forget to put your data in the projname_data directory !
|
110 |
# ### II.3.1. METADATA
|
@@ -137,6 +138,67 @@ compare_headers(exp_cols, metadata.columns.values, "Marker metadata file")
|
|
137 |
metadata = metadata.dropna()
|
138 |
metadata.head()
|
139 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
140 |
|
141 |
# ### II.3.2. NOT_INTENSITIES
|
142 |
|
@@ -170,6 +232,68 @@ verify_line_no(filename, len(not_intensities))
|
|
170 |
# Print to console
|
171 |
print("not_intensities =\n", not_intensities)
|
172 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
173 |
|
174 |
# ### II.3.3. FULL_TO_SHORT_COLUMN_NAMES
|
175 |
|
|
|
50 |
|
51 |
#set_name = 'Set_A'
|
52 |
#set_name = 'test'
|
53 |
+
|
54 |
+
'''
|
55 |
present_dir = os.path.dirname(os.path.realpath(__file__))
|
56 |
|
57 |
input_path = os.path.join(present_dir, 'wetransfer_data-zip_2024-05-17_1431')
|
|
|
106 |
print('metadata_dir :', metadata_dir)
|
107 |
print('metadata_images_dir :', metadata_images_dir)
|
108 |
|
|
|
109 |
# ## II.3. FILES
|
110 |
#Don't forget to put your data in the projname_data directory !
|
111 |
# ### II.3.1. METADATA
|
|
|
138 |
metadata = metadata.dropna()
|
139 |
metadata.head()
|
140 |
|
141 |
+
'''
|
142 |
+
import os
|
143 |
+
|
144 |
+
# Get the current script directory
|
145 |
+
present_dir = os.path.dirname(os.path.realpath(__file__))
|
146 |
+
|
147 |
+
# Define the input path
|
148 |
+
input_path = os.path.join(present_dir, 'wetransfer_data-zip_2024-05-17_1431')
|
149 |
+
base_dir = input_path
|
150 |
+
set_path = 'test'
|
151 |
+
selected_metadata_files = ['Slide_B_DD1s1.one_1.tif.csv', 'Slide_B_DD1s1.one_2.tif.csv']
|
152 |
+
ls_samples = ['Ashlar_Exposure_Time.csv', 'new_data.csv', 'DD3S1.csv', 'DD3S2.csv', 'DD3S3.csv', 'TMA.csv']
|
153 |
+
|
154 |
+
set_name = set_path
|
155 |
+
|
156 |
+
# Project and step names
|
157 |
+
project_name = set_name # Project name
|
158 |
+
step_suffix = 'bs' # Current part (here part II)
|
159 |
+
previous_step_suffix_long = "_qc_eda" # Previous part (here QC/EDA NOTEBOOK)
|
160 |
+
|
161 |
+
# Initial input data directory
|
162 |
+
input_data_dir = os.path.join(base_dir, project_name + previous_step_suffix_long)
|
163 |
+
|
164 |
+
# Define writable directory
|
165 |
+
writable_directory = '/tmp'
|
166 |
+
|
167 |
+
# BS output directories redirected to writable directory
|
168 |
+
output_data_dir = os.path.join(writable_directory, project_name + "_" + step_suffix)
|
169 |
+
# BS images subdirectory redirected to writable directory
|
170 |
+
output_images_dir = os.path.join(output_data_dir, "images")
|
171 |
+
|
172 |
+
# Metadata directories
|
173 |
+
metadata_dir = os.path.join(base_dir, project_name + "_metadata")
|
174 |
+
# images subdirectory
|
175 |
+
metadata_images_dir = os.path.join(metadata_dir, "images")
|
176 |
+
|
177 |
+
# Ensure the output directories exist
|
178 |
+
os.makedirs(output_data_dir, exist_ok=True)
|
179 |
+
os.makedirs(output_images_dir, exist_ok=True)
|
180 |
+
|
181 |
+
# Print paths to verify
|
182 |
+
print("Input Data Directory:", input_data_dir)
|
183 |
+
print("Output Data Directory:", output_data_dir)
|
184 |
+
print("Output Images Directory:", output_images_dir)
|
185 |
+
print("Metadata Directory:", metadata_dir)
|
186 |
+
print("Metadata Images Directory:", metadata_images_dir)
|
187 |
+
|
188 |
+
# Example of writing a file to the new output directory
|
189 |
+
example_data = {'Column1': [1, 2, 3], 'Column2': ['A', 'B', 'C']}
|
190 |
+
example_df = pd.DataFrame(example_data)
|
191 |
+
|
192 |
+
output_file_path = os.path.join(output_data_dir, 'example_output.csv')
|
193 |
+
|
194 |
+
try:
|
195 |
+
example_df.to_csv(output_file_path, index=False)
|
196 |
+
print(f"Output written successfully to {output_file_path}")
|
197 |
+
except PermissionError as e:
|
198 |
+
print(f"Permission denied: Unable to write the file at {output_file_path}. Error: {e}")
|
199 |
+
except Exception as e:
|
200 |
+
print(f"An error occurred: {e}")
|
201 |
+
|
202 |
|
203 |
# ### II.3.2. NOT_INTENSITIES
|
204 |
|
|
|
232 |
# Print to console
|
233 |
print("not_intensities =\n", not_intensities)
|
234 |
|
235 |
+
import os
|
236 |
+
import pandas as pd
|
237 |
+
|
238 |
+
# Function to compare headers (assuming you have this function defined in your my_modules.py)
|
239 |
+
def compare_headers(expected, actual, description):
|
240 |
+
missing = [col for col in expected if col not in actual]
|
241 |
+
if missing:
|
242 |
+
print(f"WARNING: Missing expected columns in {description}: {missing}")
|
243 |
+
else:
|
244 |
+
print(f"All expected columns are present in {description}.")
|
245 |
+
|
246 |
+
# Get the current script directory
|
247 |
+
present_dir = os.path.dirname(os.path.realpath(__file__))
|
248 |
+
|
249 |
+
# Define the input path
|
250 |
+
input_path = os.path.join(present_dir, 'wetransfer_data-zip_2024-05-17_1431')
|
251 |
+
base_dir = input_path
|
252 |
+
set_path = 'test'
|
253 |
+
|
254 |
+
# Project and step names
|
255 |
+
project_name = set_path # Project name
|
256 |
+
previous_step_suffix_long = "_qc_eda" # Previous part (here QC/EDA NOTEBOOK)
|
257 |
+
|
258 |
+
# Initial input data directory
|
259 |
+
input_data_dir = os.path.join(base_dir, project_name + previous_step_suffix_long)
|
260 |
+
|
261 |
+
# Metadata directories
|
262 |
+
metadata_dir = os.path.join(base_dir, project_name + "_metadata")
|
263 |
+
metadata_images_dir = os.path.join(metadata_dir, "images")
|
264 |
+
|
265 |
+
# Define writable directory
|
266 |
+
writable_directory = '/tmp'
|
267 |
+
|
268 |
+
# Check and read metadata file
|
269 |
+
filename = "marker_intensity_metadata.csv"
|
270 |
+
filename = os.path.join(metadata_dir, filename)
|
271 |
+
|
272 |
+
# Check if the file exists
|
273 |
+
if not os.path.exists(filename):
|
274 |
+
print("WARNING: Could not find desired file: " + filename)
|
275 |
+
else:
|
276 |
+
print("The", filename, "file was imported for further analysis!")
|
277 |
+
|
278 |
+
# Open, read in information
|
279 |
+
metadata = pd.read_csv(filename)
|
280 |
+
|
281 |
+
# Verify headers
|
282 |
+
exp_cols = ['Round', 'Target', 'Channel', 'target_lower', 'full_column', 'marker', 'localisation']
|
283 |
+
compare_headers(exp_cols, metadata.columns.values, "Marker metadata file")
|
284 |
+
|
285 |
+
metadata = metadata.dropna()
|
286 |
+
print(metadata.head())
|
287 |
+
|
288 |
+
# Example of writing to the writable directory
|
289 |
+
output_file_path = os.path.join(writable_directory, 'processed_metadata.csv')
|
290 |
+
try:
|
291 |
+
metadata.to_csv(output_file_path, index=False)
|
292 |
+
print(f"Processed metadata written successfully to {output_file_path}")
|
293 |
+
except PermissionError as e:
|
294 |
+
print(f"Permission denied: Unable to write the file at {output_file_path}. Error: {e}")
|
295 |
+
except Exception as e:
|
296 |
+
print(f"An error occurred: {e}")
|
297 |
|
298 |
# ### II.3.3. FULL_TO_SHORT_COLUMN_NAMES
|
299 |
|