Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

model.load() IndexError: list index out of range #41

Open
ZmuX1n opened this issue Aug 22, 2023 · 13 comments
Open

model.load() IndexError: list index out of range #41

ZmuX1n opened this issue Aug 22, 2023 · 13 comments

Comments

@ZmuX1n
Copy link

ZmuX1n commented Aug 22, 2023

After I save the trained model and execute model.load(), it fails with "list index out of range". Here is my code. Can you tell me what's the problem?
model.train([data_df])
model.save("model")
model = model.load("model")

@yangwenz
Copy link
Collaborator

Hi, could you provide more information about this problem, e.g., what is "model"?

@ZmuX1n
Copy link
Author

ZmuX1n commented Aug 23, 2023

Thanks for your reply. Here is my complete code:
model = BayesianNetwork(config=BayesianNetwork.config_class(graph=graph_df))
model.train([data_df])
model.save("model")
model = model.load("model")
Then, it fails with the IndexError above.
Besides, I tried for "model = BayesianNetwork.load("model")", but failed with the same error

@ZmuX1n
Copy link
Author

ZmuX1n commented Aug 23, 2023

If I don't use save-->load function, like:
model = BayesianNetwork(config=BayesianNetwork.config_class(graph=graph_df))
model.train([data_df])
model.find_root_cause(["..."])
It performs well. So I think there should be something wrong with the "load" function

@yangwenz
Copy link
Collaborator

Hi, could you copy all the error messages here? It will help to locate the problem.

@ZmuX1n
Copy link
Author

ZmuX1n commented Aug 23, 2023

Ok, here are all the error messages:
IndexError Traceback (most recent call last)
Cell In[7], line 1
----> 1 model = BayesianNetwork.load("model")

File D:\Anaconda3\envs\python38\lib\site-packages\pyrca\analyzers\bayesian.py:442, in BayesianNetwork.load(cls, directory, filename, **kwargs)
439 @classmethod
440 def load(cls, directory, filename="bn", **kwargs):
441 model = cls.new(cls)
--> 442 reader = BIFReader(os.path.join(directory, f"{filename}.bif"))
443 model.bayesian_model = reader.get_model()
444 with open(os.path.join(directory, f"{filename}_info.pkl"), "rb") as f:

File D:\Anaconda3\envs\python38\lib\site-packages\pyrca\thirdparty\pgmpy\readwrite\BIF.py:90, in BIFReader.init(self, path, string, include_properties, n_jobs)
88 self.probability_expr, self.cpd_expr = self.get_probability_grammar()
89 self.network_name = self.get_network_name()
---> 90 self.variable_names = self.get_variables()
91 self.variable_states = self.get_states()
92 if self.include_properties:

File D:\Anaconda3\envs\python38\lib\site-packages\pyrca\thirdparty\pgmpy\readwrite\BIF.py:200, in BIFReader.get_variables(self)
198 variable_names = []
199 for block in self.variable_block():
--> 200 name = self.name_expr.searchString(block)[0][0]
201 variable_names.append(name)
203 return variable_names

File D:\Anaconda3\envs\python38\lib\site-packages\pyparsing\results.py:193, in ParseResults.getitem(self, i)
191 def getitem(self, i):
192 if isinstance(i, (int, slice)):
--> 193 return self._toklist[i]
194 else:
195 if i not in self._all_names:

IndexError: list index out of range

@yangwenz
Copy link
Collaborator

The problem comes from BIFReader. Is it OK to share the dumped .bif file so that we can examine this issue?

@ZmuX1n
Copy link
Author

ZmuX1n commented Aug 23, 2023

I'm glad to share the .bif file.
Should I send the file to your email or by some other methods?

@yangwenz
Copy link
Collaborator

Thanks! You can send me email (yangwenzhuo08@gmail.com) or share it via Dropbox or Google drive.

@ZmuX1n
Copy link
Author

ZmuX1n commented Aug 23, 2023

@yangwenz
Copy link
Collaborator

The problem comes from the naming issue. The metric names must be English or utf-8 encoded. Otherwise, the parser of pgmpy will fail.

@ZmuX1n
Copy link
Author

ZmuX1n commented Aug 23, 2023

I got it. So, how can I resolve the problem if my dataset must be Chinese?

@yangwenz
Copy link
Collaborator

You can rename the dataframe columns as x1, x2, ..., and make a mapping from chinese names to these names.

@ZmuX1n
Copy link
Author

ZmuX1n commented Aug 23, 2023

It's a good idea! Thanks for your guidance. I will try this method, and I am looking forward to the follow-up communication with you!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants