Skip to content

Commit

Permalink
Merge pull request #117 from imodpasteur/transform-headers
Browse files Browse the repository at this point in the history
Transform headers
  • Loading branch information
oeway authored Aug 27, 2022
2 parents 94401fe + d191371 commit c895eb3
Show file tree
Hide file tree
Showing 3 changed files with 150 additions and 4 deletions.
2 changes: 1 addition & 1 deletion shareloc_utils/VERSION
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
{
"version": "0.1.8"
"version": "0.1.9"
}
11 changes: 8 additions & 3 deletions shareloc_utils/batch_download.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@

from tqdm import tqdm
from shareloc_utils.smlm_file import read_smlm_file

from shareloc_utils.formats import supported_text_formats

class DownloadProgressBar(tqdm):
def update_to(self, b=1, bsize=1, tsize=None):
Expand All @@ -32,10 +32,15 @@ def resolve_url(rdf_url, path):
return urljoin(os.path.dirname(rdf_url) + "/", path)


def convert_smlm(file_path, delimiter=",", extension=".csv"):
def convert_smlm(file_path, delimiter=",", extension=".csv", format="ThunderSTORM (csv)"):
smlm_info = read_smlm_file(file_path)
converted_files = []
table_count = len(smlm_info["files"])
format_config = supported_text_formats[format];
header_transform = {}
for k, v in format_config["header_transform"].items():
header_transform[v]=k

for tbi, file_info in enumerate(smlm_info["files"]):
cols = file_info["cols"]
rows = file_info["rows"]
Expand All @@ -46,7 +51,7 @@ def convert_smlm(file_path, delimiter=",", extension=".csv"):
)
with open(fp, "w") as f:
for i in range(cols):
f.write(headers[i] + (delimiter if i < cols - 1 else "\n"))
f.write((header_transform.get(headers[i], headers[i])) + (delimiter if i < cols - 1 else "\n"))
for i in tqdm(range(rows), total=rows):
for j in range(cols):
f.write(
Expand Down
141 changes: 141 additions & 0 deletions shareloc_utils/formats.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,141 @@

supported_text_formats = {
"ThunderSTORM (csv)": {
# Required
"type": "table",
"extension": ".csv",
"mode": "text",
"dtype": "float32",
"delimiter": ",",
"comments": "",
# specify which row is the header, set to -1 if there is no header
"header_row": 0,
# specify how to transform the headers to the standard header defined in smlm format,
"header_transform": { "x [nm]": "x", "y [nm]": "y", "z [nm]": "z", "uncertainty_xy [nm]": "uncertainty_xy", "uncertainty_z [nm]": "uncertainty_z" },

# Optional
"name": "ThunderSTORM (csv)",
"description": "a csv format used in thunderSTORM"
},
"ThunderSTORM (xls)": {
# Required
"type": "table",
"extension": ".xls",
"mode": "text",
"dtype": "float32",
"delimiter": "\t",
"comments": "",
# specify which row is the header, set to -1 if there is no header
"header_row": 0,
# specify how to transform the headers to the standard header defined in smlm format,
"header_transform": { "x [nm]": "x", "y [nm]": "y", "z [nm]": "z", "uncertainty_xy [nm]": "uncertainty_xy", "uncertainty_z [nm]": "uncertainty_z" },

# Optional
"name": "ThunderSTORM (xls)",
"description": "a xls format used in thunderSTORM"
},
"ZEISS (txt)": {
# Required
"type": "table",
"extension": ".txt",
"mode": "text",
"dtype": "float32",
"delimiter": "\t",
"comments": "",
# specify which row is the header, set to -1 if there is no header
"header_row": 0,
# specify how to transform the headers to the standard header defined in smlm format,
"header_transform": { "Position X [nm]": "x", "Position Y [nm]": "y", "Position Z [nm]": "z", "First Frame": "frame" },

# Optional
"name": "ZEISS (txt)",
"description": "a txt format used in ZEISS microscope"
},
"ZEISSv1 (csv)": {
# Required
"type": "table",
"extension": ".csv",
"mode": "text",
"dtype": "float32",
"delimiter": ";",
"comments": "",
# specify which row is the header, set to -1 if there is no header
"header_row": 0,
# specify how to transform the headers to the standard header defined in smlm format,
"header_transform": { "Position X [nm]": "x", "Position Y [nm]": "y", "Position Z [nm]": "z", "First Frame": "frame" },

# Optional
"name": "ZEISSv1 (csv)",
"description": "a csv format used in ZEISS microscope"
},
"ZEISSv2 (csv)": {
# Required
"type": "table",
"extension": ".csv",
"mode": "text",
"dtype": "float32",
"delimiter": ";",
"comments": "",
# specify which row is the header, set to -1 if there is no header
"header_row": 0,
# specify how to transform the headers to the standard header defined in smlm format,
"header_transform": { "x [nm]": "x", "y [nm]": "y", "z [nm]": "z", "First Frame": "frame" },

# Optional
"name": "ZEISSv2 (csv)",
"description": "a csv format used for ZEISS microscope"
},
"RapidSTORM (txt)": {
# Required
"type": "table",
"extension": ".txt",
"mode": "text",
"dtype": "float32",
"delimiter": " ",
"comments": "",
# specify which row is the header, set to -1 if there is no header
"header_row": 0,

# specify how to transform the headers to the standard header defined in smlm format,
"header_transform": { "*all*": 'identifier="([\\w\\d\\-_]+)"', "Position-0-0": "x", "Position-1-0": "y", "Position-2-0": "z", "ImageNumber-0-0": "frame" },

# Optional
"name": "RapidSTORM (txt)",
"description": "a text format used in RapidSTORM"
},
"Space Seperated List (txt)": {
# Required
"type": "table",
"extension": ".txt",
"mode": "text",
"dtype": "float32",
"delimiter": " ",
"comments": "",
# specify which row is the header, set to -1 if there is no header
"header_row": 0,

# specify how to transform the headers to the standard header defined in smlm format,
"header_transform": {},

# Optional
"name": "Space Seperated List (txt)",
"description": "A list of coordinates seperated with space."
},
"Nikon NSTORM (txt)": {
# Required
"type": "table",
"extension": ".txt",
"mode": "text",
"dtype": "float32",
"delimiter": "\t",
"comments": "",
# specify which row is the header, set to -1 if there is no header
"header_row": 0,
# specify how to transform the headers to the standard header defined in smlm format,
"header_transform": { "X": "x", "Y": "y", "Z": "z", "Frame": "frame" },

# Optional
"name": "Nikon NSTORM (txt)",
"description": "a txt format exported from Nikon NSTORM software"
},
}

0 comments on commit c895eb3

Please sign in to comment.