Last active
March 9, 2020 12:45
-
-
Save flowernert/6ff35ae57f854afbafb48fcfc1467be8 to your computer and use it in GitHub Desktop.
ImageJ (Fiji) STORM microscopy autocorrelation analysis
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import pandas as pd | |
import os | |
import sys | |
csv_autocor_name = "Recs TS proc (xy16).tif_AC(l1,w0)_Autocorr.csv" | |
def export_to_csv(sheets, analyzis_path): | |
for k, v in sheets.items(): | |
v.to_csv(os.path.join(analyzis_path, k + ".csv"), index=None) | |
def export_to_excel(sheets, doc_name): | |
with pd.ExcelWriter(doc_name) as writer: | |
for k, v in sheets.items(): | |
v.to_excel(writer, sheet_name=k, index=None) | |
def import_data(csv_path): | |
df_csv = pd.read_csv(os.path.abspath(csv_path)) | |
first_col = df_csv.iloc[:, 0] | |
headers = df_csv.columns.to_list()[1:] | |
cond_names = [h.split(sep="_")[0] + " " + h.split(sep="_")[2] for h in headers] | |
conds_indexes = dict(enumerate([c for c in cond_names])) | |
conds_prefixes = sorted(set(conds_indexes.values())) | |
sheets = dict((cp, pd.DataFrame()) for cp in conds_prefixes) # create one df per sheet (or condition) | |
# put data of each condition in a distinct dataframe | |
roi_idx = 1 | |
for idx, cond in conds_indexes.items(): | |
col = df_csv.iloc[:, idx+1] | |
header_mod = str.join(" ", ["ROI" + str(roi_idx).zfill(3), str.join(" ", col.name.split(sep="_")[:3])]) | |
sheets[cond][first_col.name] = first_col.tolist() | |
sheets[cond][header_mod] = col.tolist() | |
roi_idx += 1 | |
return sheets | |
def process_data(sheets, conditions=[]): | |
df = pd.DataFrame() | |
df_m = pd.DataFrame() | |
for idx, (k, v) in enumerate(sheets.items()): | |
col_name = conditions[idx] if conditions and len(conditions) == len(sheets) else k | |
valleys1 = v.iloc[6, 1:].values # line 6 is 0.096 µm | |
valleys2 = v.iloc[18, 1:].values # line 18 is 0.288 µm | |
valleys_mean = (valleys1 + valleys2) / 2 | |
peaks = v.iloc[12, 1:].values # line 12 is 0.192 µm | |
df_p2v = pd.DataFrame({col_name: peaks - valleys1}) | |
df_p2v_m = pd.DataFrame({col_name + "_m": peaks - valleys_mean}) | |
df = pd.concat([df, df_p2v], axis=1) | |
df_m = pd.concat([df_m, df_p2v_m], axis=1) | |
return df, df_m | |
def format_data(sheets, result): | |
sheets["peak-valley"] = result[0] | |
sheets["peak-valleys_mean"] = result[1] | |
return sheets | |
if __name__ == '__main__': | |
if len(sys.argv) > 2: | |
xp_path = sys.argv[1] | |
AIS_subdir = sys.argv[2] | |
analyzis_path = os.path.join(xp_path, AIS_subdir) | |
ac_csv_path = os.path.join(analyzis_path, csv_autocor_name) | |
sheets = import_data(ac_csv_path) | |
result = process_data(sheets) | |
sheets = format_data(sheets, result) | |
export_to_csv(sheets, analyzis_path) | |
else: | |
print("not enough args", sys.stderr) | |
print("usage: python autocorrel_analysis.py /path/to/experiment_dir subfolder") | |
print("where subfolder is in experiment_dir and contains the CSV resulting from ImageJ autocorrel analysis") | |
print("this software assumes file name formatting with prefix like 'Cx_Ny_condname_...'") | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment