Importing and Exporting Single-Case Data#

The functions read_scd and write_scd allow users to import and export single-case experimental design (SCED) data from various file formats such as CSV and Excel. These functions ensure that the data structure remains consistent for analysis.

Importing Data with read_scd#

The read_scd function reads a dataset and formats it correctly for analysis. It supports CSV and Excel files and allows customization of column names and phase labels.

Required Arguments:#

  • file: Path to the dataset.

Optional Arguments:#

  • cvar: Name of the column containing case identifiers (default "case").

  • pvar: Name of the column containing phase labels (default "phase").

  • dvar: Name of the column containing dependent values (default "values").

  • mvar: Name of the column containing measurement time (default "mt").

  • sort_cases: If True, cases are sorted in the output.

  • phase_names: Allows renaming phases (e.g., ["A", "B"]).

  • file_type: Manually specify the file type ("csv", "xls", "xlsx"), if needed.

  • na_values: Defines which values should be treated as missing (default ["", "NA"]).

If no phase information is provided, the function keeps the original structure.

import scia as sc
📖 scia 1.101.0.dev6 - For Documentation, visit: https://ahsankhodami.github.io/scia/intro.html

Example 1: Reading a CSV File#

To import a dataset from a CSV file while keeping default settings:

df = sc.read_scd("sample.csv")
print(df.head())
Imported 2 cases
  case         phase  values  mt
0    A      Baseline       2   1
1    A      Baseline       3   2
2    A  Intervention       5   3
3    B      Baseline       1   1
4    B      Baseline       4   2

Example 2: Reading an Excel File with Phase Renaming#

This example imports an Excel file while renaming the phases.

df = sc.read_scd("sample.xlsx", phase_names=["Baseline", "Intervention"])
print(df.head())
Imported 2 cases
  case         phase  values  mt
0    A      Baseline       2   1
1    A      Baseline       3   2
2    A  Intervention       5   3
3    B      Baseline       1   1
4    B      Baseline       4   2

Example 3: Sorting Cases Automatically#

To ensure cases are sorted in the output:

df = sc.read_scd("sample.csv", sort_cases=True)
print(df.head())
Imported 2 cases
  case         phase  values  mt
0    A      Baseline       2   1
1    A      Baseline       3   2
2    A  Intervention       5   3
3    B      Baseline       1   1
4    B      Baseline       4   2

Exporting Data with write_scd#

The write_scd function allows saving SCED data to a CSV or Excel file.

Required Arguments:#

  • data: A Pandas DataFrame containing SCED data.

  • filename: Path where the file should be saved.

Optional Arguments:#

  • sep: Separator for CSV files (default ",").

  • dec: Decimal format (default ".", can be changed to "," for certain locales).

  • file_type: Manually specify file type ("csv", "xls", "xlsx"), if needed.

The function prints a message when the file is saved successfully.

Example 4: Saving Data to a CSV File#

To save the dataset as a CSV file:

sc.write_scd(df, "sample_output.csv")
Data successfully saved to sample_output.csv

Example 5: Saving Data to an Excel File with a Custom Decimal Format#

To save the dataset with decimal points replaced by commas:

sc.write_scd(df, "sample_output.xlsx", dec=",")
Data successfully saved to sample_output.xlsx
D:\Python Packages\scia\scia\io.py:75: FutureWarning: DataFrame.applymap has been deprecated. Use DataFrame.map instead.
  data = data.applymap(lambda x: str(x).replace(".", ",") if isinstance(x, float) else x)