py-why / causal-learn

Causal Discovery in Python. It also includes (conditional) independence tests and score functions.
https://causal-learn.readthedocs.io/en/latest/
MIT License
1.18k stars 193 forks source link

TypeError ufunc 'isnan' / 'isinf' #199

Open smarianimore opened 1 month ago

smarianimore commented 1 month ago

Hello, I'm trying to use causal-learn (pc and fci at the moment) but I'm stuck with input data errors.

My dataframe has no nans nor infs, as checked with both pandas and numpy, but running either algorithm reports error

TypeError: ufunc 'isnan' (or 'isinf') not supported for the input types, and the inputs could not be safely coerced to any supported types according to the casting rule ''safe''

The full stack trace is as follows (where data types can also be seen):

reading csv...
                df.dtypes=cpu_utilization    Float64
memory_usage       Float64
pixel                Int64
fps                  Int64
consumption          Int64
dtype: object
                df.isnull().values.any()=np.False_
                np.isinf(df).values.any()=np.False_
        learning structure...
                df.shape=(12748, 5)
0it [00:00, ?it/s]
Traceback (most recent call last):
  File "/home/stefano/PyCharm-projects/Playground-causal-learn/scripts/tu_wien_stuff.py", line 126, in <module>
    process_file(f"{args.filepath}/{file}", columns, args)
  File "/home/stefano/PyCharm-projects/Playground-causal-learn/scripts/tu_wien_stuff.py", line 81, in process_file
    model = learn_structure(np_data, args.nrows, filepath)
            ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/home/stefano/PyCharm-projects/Playground-causal-learn/scripts/tu_wien_stuff.py", line 46, in learn_structure
    model_graph, model_edges = fci(df, mvpc=True, indep_test="mv_fisherz")  # TODO ufunc 'isnan' not supported for the input types, and the inputs could not be safely coerced to any supported types according to the casting rule ''safe''
                               ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/home/stefano/.pyenv/versions/3.11.3/lib/python3.11/site-packages/causallearn/search/ConstraintBased/FCI.py", line 738, in fci
    independence_test_method = CIT(dataset, method=independence_test_method, **kwargs)
                               ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/home/stefano/.pyenv/versions/3.11.3/lib/python3.11/site-packages/causallearn/utils/cit.py", line 32, in CIT
    return FisherZ(data, **kwargs)
           ^^^^^^^^^^^^^^^^^^^^^^^
  File "/home/stefano/.pyenv/versions/3.11.3/lib/python3.11/site-packages/causallearn/utils/cit.py", line 142, in __init__
    self.assert_input_data_is_valid()
  File "/home/stefano/.pyenv/versions/3.11.3/lib/python3.11/site-packages/causallearn/utils/cit.py", line 81, in assert_input_data_is_valid
    assert allow_nan or not np.isnan(self.data).any(), "Input data contains NaN. Please check."
                            ^^^^^^^^^^^^^^^^^^^
TypeError: ufunc 'isnan' not supported for the input types, and the inputs could not be safely coerced to any supported types according to the casting rule ''safe''
kunwuz commented 2 weeks ago

Hi, if possible, could you please provide a minimal example of code and data to reproduce this error? I haven't got this on my end.

smarianimore commented 5 days ago

Sorry for the latency, here it is a gist with a MWE and example dataset that generates the error: https://gist.github.com/smarianimore/3e78a8ddc5d642b0432c324d495ede22

FYI, in the MWE I'm using causallearn.search.ConstraintBased.PC, but the same error happens with fci. As an aside, the same MWE but using ges or lingam gives a different error: AttributeError:np.matwas removed in the NumPy 2.0 release. Usenp.asmatrixinstead. (I have numpy 2.1.1 installed)