Test metadata support with pandas.
(tmp_path, monkeypatch)
| 3949 | |
| 3950 | |
| 3951 | def test_metadata(tmp_path, monkeypatch): |
| 3952 | """Test metadata support with pandas.""" |
| 3953 | pd = pytest.importorskip("pandas") |
| 3954 | data = np.random.randn(10, 2, 2000) |
| 3955 | chs = ["a", "b"] |
| 3956 | info = create_info(chs, 1000) |
| 3957 | meta = np.array( |
| 3958 | [[1.0] * 5 + [3.0] * 5, ["a"] * 2 + ["b"] * 3 + ["c"] * 3 + ["µ"] * 2], |
| 3959 | dtype="object", |
| 3960 | ).T |
| 3961 | meta = pd.DataFrame(meta, columns=["num", "letter"]) |
| 3962 | meta["num"] = np.array(meta["num"], float) |
| 3963 | events = np.arange(meta.shape[0]) |
| 3964 | events = np.column_stack([events, np.zeros([len(events), 2])]).astype(int) |
| 3965 | events[5:, -1] = 1 |
| 3966 | event_id = {"zero": 0, "one": 1} |
| 3967 | with catch_logging() as log: |
| 3968 | epochs = EpochsArray( |
| 3969 | data, info, metadata=meta, events=events, event_id=event_id, verbose=True |
| 3970 | ) |
| 3971 | log = log.getvalue() |
| 3972 | msg = "Adding metadata with 2 columns" |
| 3973 | assert log.count(msg) == 1, f"\nto find:\n{msg}\n\nlog:\n{log}" |
| 3974 | with use_log_level(True): |
| 3975 | with catch_logging() as log: |
| 3976 | epochs.metadata = meta |
| 3977 | log = log.getvalue().strip() |
| 3978 | assert log == "Replacing existing metadata with 2 columns", f"{log}" |
| 3979 | indices = np.arange(len(epochs)) # expected indices |
| 3980 | assert_array_equal(epochs.metadata.index, indices) |
| 3981 | |
| 3982 | assert len(epochs[[1, 2]].events) == len(epochs[[1, 2]].metadata) |
| 3983 | assert_array_equal(epochs[[1, 2]].metadata.index, indices[[1, 2]]) |
| 3984 | assert len(epochs["one"]) == 5 |
| 3985 | |
| 3986 | # Construction |
| 3987 | with pytest.raises(ValueError): |
| 3988 | # Events and metadata must have same len |
| 3989 | epochs_arr = EpochsArray( |
| 3990 | epochs._data, |
| 3991 | epochs.info, |
| 3992 | epochs.events[:-1], |
| 3993 | tmin=0, |
| 3994 | event_id=epochs.event_id, |
| 3995 | metadata=epochs.metadata, |
| 3996 | ) |
| 3997 | |
| 3998 | with pytest.raises(ValueError): |
| 3999 | # Events and data must have same len |
| 4000 | epochs = EpochsArray(data, info, metadata=meta.iloc[:-1]) |
| 4001 | |
| 4002 | for data in [meta.values, meta["num"]]: |
| 4003 | # Metadata must be a DataFrame |
| 4004 | with pytest.raises(ValueError): |
| 4005 | epochs = EpochsArray(data, info, metadata=data) |
| 4006 | |
| 4007 | # Need strings, ints, and floats |
| 4008 | with pytest.raises(ValueError): |
nothing calls this directly
no test coverage detected