Skip to content

WAXS integrator - csv output from zarr #23

@junspark

Description

@junspark

I made the following csv file output script based on the zarr file.
Can we add this feature (or something similar since the csv file output from below is a bit messy) as an option?


import zarr
import pandas as pd
import argparse
import os

def zarr_to_csv(zarr_path, csv_output_dir):
"""
Convert datasets in a Zarr file to CSV files.

Parameters:
zarr_path (str): The path to the Zarr file.
csv_output_dir (str): The directory where the CSV files will be saved.
"""
# Load the Zarr file
zarr_file = zarr.open(zarr_path, mode='r')

# Ensure the output directory exists
os.makedirs(csv_output_dir, exist_ok=True)

# Function to process groups and arrays recursively
def process_node(node, path=""):
    for key, item in node.items():
        current_path = os.path.join(path, key)
        if isinstance(item, zarr.Group):
            # Recursively process groups
            process_node(item, current_path)
        elif isinstance(item, zarr.Array):
            # Process arrays (datasets)
            data = item[:]
            # Flatten data if multidimensional
            if data.ndim > 2:
                data = data.reshape(data.shape[0], -1)
            elif data.ndim == 1:
                data = data.reshape(-1, 1)

            # Convert to DataFrame
            df = pd.DataFrame(data)
            # Define CSV file path
            csv_filename = f"{current_path.replace('/', '_')}.csv"
            csv_filepath = os.path.join(csv_output_dir, csv_filename)
            # Save to CSV
            df.to_csv(csv_filepath, index=False)
            print(f"Saved {csv_filepath}")

# Start processing from the root of the Zarr file
process_node(zarr_file)
print("Conversion complete.")

if name == "main":
parser = argparse.ArgumentParser(description="Convert a Zarr file to CSV files.")
parser.add_argument("-z", "--zarr_path", required=True, help="Path to the Zarr file")
parser.add_argument("-c", "--csv_output_dir", required=True, help="Directory where CSV files will be saved")

args = parser.parse_args()

zarr_to_csv(args.zarr_path, args.csv_output_dir)

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions