(fname, annot)
| 1757 | |
| 1758 | |
| 1759 | def _write_annotations_txt(fname, annot): |
| 1760 | content = "# MNE-Annotations\n" |
| 1761 | if annot.orig_time is not None: |
| 1762 | # for backward compat, we do not write tzinfo (assumed UTC) |
| 1763 | content += f"# orig_time : {annot.orig_time.replace(tzinfo=None)}\n" |
| 1764 | content += "# onset, duration, description" |
| 1765 | n_cols = 3 |
| 1766 | data = [annot.onset, annot.duration, annot.description] |
| 1767 | if annot._any_ch_names(): |
| 1768 | n_cols += 1 |
| 1769 | content += ", ch_names" |
| 1770 | data.append( |
| 1771 | [ |
| 1772 | _safe_name_list(ch, "write", f"annot.ch_names[{ci}]") |
| 1773 | for ci, ch in enumerate(annot.ch_names) |
| 1774 | ] |
| 1775 | ) |
| 1776 | if len(extras_columns := annot._extras_columns) > 0: |
| 1777 | n_cols += len(extras_columns) |
| 1778 | for column in extras_columns: |
| 1779 | content += f", {column}" |
| 1780 | values = [extra.get(column, None) for extra in annot.extras] |
| 1781 | if len(dtypes := set(type(v) for v in values)) > 1: |
| 1782 | warn( |
| 1783 | f"Extra field '{column}' contains heterogeneous dtypes ({dtypes}). " |
| 1784 | "Loading these TXT annotations may not return the original dtypes." |
| 1785 | ) |
| 1786 | data.append([val if val is not None else "" for val in values]) |
| 1787 | content += "\n" |
| 1788 | data = np.array(data, dtype=str).T |
| 1789 | assert data.ndim == 2 |
| 1790 | assert data.shape[0] == len(annot.onset) |
| 1791 | assert data.shape[1] == n_cols |
| 1792 | with open(fname, "wb") as fid: |
| 1793 | fid.write(content.encode()) |
| 1794 | np.savetxt(fid, data, delimiter=",", fmt="%s") |
| 1795 | |
| 1796 | |
| 1797 | @fill_doc |
no test coverage detected