|
2 | 2 |
|
3 | 3 | import os |
4 | 4 |
|
| 5 | +import numpy as np |
5 | 6 | import pytest |
| 7 | +import pandas as pd |
6 | 8 | import bids |
7 | 9 | from bids.exceptions import ConfigError |
8 | 10 |
|
9 | 11 | from ..models import Entity, Config |
10 | | -from ..utils import BIDSMetadata, parse_file_entities, add_config_paths |
| 12 | +from ..utils import BIDSMetadata, PaddedInt, parse_file_entities, add_config_paths |
11 | 13 |
|
12 | 14 |
|
13 | 15 | def test_bidsmetadata_class(): |
@@ -73,3 +75,31 @@ def test_add_config_paths(): |
73 | 75 | add_config_paths(dummy=bids_json) |
74 | 76 | config = Config.load('dummy') |
75 | 77 | assert 'subject' in config.entities |
| 78 | + |
| 79 | + |
| 80 | +def test_PaddedInt_array_comparisons(): |
| 81 | + # Array comparisons should work, not raise exceptions |
| 82 | + arr = np.array([5, 5, 5]) |
| 83 | + assert np.all(arr == PaddedInt(5)) |
| 84 | + assert np.all(arr == PaddedInt("05")) |
| 85 | + assert np.all(PaddedInt(5) == arr) |
| 86 | + assert np.all(PaddedInt("05") == arr) |
| 87 | + |
| 88 | + # If the value gets put into an array, it should be considered an int |
| 89 | + # We lose the padding, but it's unlikely we would try to recover it |
| 90 | + # from an array. |
| 91 | + assert np.array([PaddedInt(5)]).dtype == np.int64 |
| 92 | + |
| 93 | + # Verify that we do get some False results |
| 94 | + assert np.array_equal(np.array([4, 5, 6]) == PaddedInt(5), [False, True, False]) |
| 95 | + |
| 96 | + |
| 97 | +def test_PaddedInt_dataframe_behavior(): |
| 98 | + # Verify that pandas dataframes are not more special than numpy arrays, |
| 99 | + # as far as PaddedInt is concerned |
| 100 | + df = pd.DataFrame({'a': [5, 5, 5]}) |
| 101 | + pidf = pd.DataFrame({'a': [PaddedInt(5)] * 3}) |
| 102 | + assert np.all(df['a'] == PaddedInt(5)) |
| 103 | + assert np.all(df == pidf) |
| 104 | + |
| 105 | + assert pidf['a'].dtype is np.dtype('int64') |
0 commit comments