codecov[bot]: # [Codecov](https://codecov.io/gh/pgmpy/pgmpy/pull/1285?src=pr&el=h1) Report
> Merging [#1285](https://codecov.io/gh/pgmpy/pgmpy/pull/1285?src=pr&el=desc) into [dev](https://codecov.io/gh/pgmpy/pgmpy/commit/f379c8c3ca58651f4309b20289a09e7636fc0157&el=desc) will **increase** coverage by `0.15%`.
> The diff coverage is `100.00%`.

[![Impacted file tree graph](https://codecov.io/gh/pgmpy/pgmpy/pull/1285/graphs/tree.svg?width=650&height=150&src=pr&token=UaJMCdHaEF)](https://codecov.io/gh/pgmpy/pgmpy/pull/1285?src=pr&el=tree)

```diff
@@            Coverage Diff             @@
##              dev    #1285      +/-   ##
==========================================
+ Coverage   92.41%   92.56%   +0.15%     
==========================================
  Files         130      130              
  Lines       12838    12836       -2     
==========================================
+ Hits        11864    11882      +18     
+ Misses        974      954      -20     
```


| [Impacted Files](https://codecov.io/gh/pgmpy/pgmpy/pull/1285?src=pr&el=tree) | Coverage Δ | |
|---|---|---|
| [pgmpy/readwrite/BIF.py](https://codecov.io/gh/pgmpy/pgmpy/pull/1285/diff?src=pr&el=tree#diff-cGdtcHkvcmVhZHdyaXRlL0JJRi5weQ==) | `96.33% <100.00%> (+1.37%)` | :arrow_up: |
| [pgmpy/readwrite/XMLBIF.py](https://codecov.io/gh/pgmpy/pgmpy/pull/1285/diff?src=pr&el=tree#diff-cGdtcHkvcmVhZHdyaXRlL1hNTEJJRi5weQ==) | `96.93% <100.00%> (ø)` | |
| [pgmpy/tests/test\_readwrite/test\_BIF.py](https://codecov.io/gh/pgmpy/pgmpy/pull/1285/diff?src=pr&el=tree#diff-cGdtcHkvdGVzdHMvdGVzdF9yZWFkd3JpdGUvdGVzdF9CSUYucHk=) | `100.00% <100.00%> (+7.76%)` | :arrow_up: |
| [pgmpy/tests/test\_readwrite/test\_XMLBIF.py](https://codecov.io/gh/pgmpy/pgmpy/pull/1285/diff?src=pr&el=tree#diff-cGdtcHkvdGVzdHMvdGVzdF9yZWFkd3JpdGUvdGVzdF9YTUxCSUYucHk=) | `95.96% <100.00%> (+7.16%)` | :arrow_up: |

------

[Continue to review full report at Codecov](https://codecov.io/gh/pgmpy/pgmpy/pull/1285?src=pr&el=continue).
> **Legend** - [Click here to learn more](https://docs.codecov.io/docs/codecov-delta)
> `Δ = absolute <relative> (impact)`, `ø = not affected`, `? = missing data`
> Powered by [Codecov](https://codecov.io/gh/pgmpy/pgmpy/pull/1285?src=pr&el=footer). Last update [f379c8c...f3b1ee5](https://codecov.io/gh/pgmpy/pgmpy/pull/1285?src=pr&el=lastupdated). Read the [comment docs](https://docs.codecov.io/docs/pull-request-comments).

diff --git a/pgmpy/readwrite/BIF.py b/pgmpy/readwrite/BIF.py
index 5d0c7e22..3095dc5c 100644
--- a/pgmpy/readwrite/BIF.py
+++ b/pgmpy/readwrite/BIF.py
@@ -345,9 +345,14 @@ class BIFReader(object):
         ]
         return edges
 
-    def get_model(self):
+    def get_model(self, state_name_type=str):
         """
-        Returns the fitted bayesian model
+        Returns the Bayesian Model read from the file/str.
+
+        Parameters
+        ----------
+        state_name_type: int, str or bool (default: str)
+            The data type to which to convert the state names of the variables.
 
         Example
         ----------
@@ -366,10 +371,10 @@ class BIFReader(object):
             for var in sorted(self.variable_cpds.keys()):
                 values = self.variable_cpds[var]
                 sn = {
-                    p_var: self.variable_states[p_var]
+                    p_var: list(map(state_name_type, self.variable_states[p_var]))
                     for p_var in self.variable_parents[var]
                 }
-                sn[var] = self.variable_states[var]
+                sn[var] = list(map(state_name_type, self.variable_states[var]))
                 cpd = TabularCPD(
                     var,
                     len(self.variable_states[var]),
diff --git a/pgmpy/readwrite/XMLBIF.py b/pgmpy/readwrite/XMLBIF.py
index 30dbc455..5ce84618 100644
--- a/pgmpy/readwrite/XMLBIF.py
+++ b/pgmpy/readwrite/XMLBIF.py
@@ -201,7 +201,19 @@ class XMLBIFReader(object):
         }
         return variable_property
 
-    def get_model(self):
+    def get_model(self, state_name_type=str):
+        """
+        Returns a Bayesian Model instance from the file/string.
+
+        Parameters
+        ----------
+        state_name_type: int, str, or bool (default: str)
+            The data type to which to convert the state names of the variables.
+
+        Returns
+        -------
+        BayesianModel instance: The read model.
+        """
         model = BayesianModel()
         model.add_nodes_from(self.variables)
         model.add_edges_from(self.edge_list)
@@ -220,7 +232,7 @@ class XMLBIFReader(object):
                 evidence=self.variable_parents[var],
                 evidence_card=evidence_card,
                 state_names={
-                    var: self.state_names[var]
+                    var: list(map(state_name_type, self.state_names[var]))
                     for var in chain([var], self.variable_parents[var])
                 },
             )

Inconsistent reading and writing from files because of state names
By default, pgmpy assigns an `int` type state name to variables if no state names are explicitly specified. This creates a problem when these models are written to a file and read back. Since there is no way to differentiate between an `int` type state or `str` type state (i.e whether the state was `0` or `'0'` when written) when reading from the file, it can lead to unexpected results. The reader functions currently always by default assume the state to be of `str` type, so the state `0` when written and read back will be `'0'`.

A possible solution to this is to change the default state name to `str` type instead of `int`.
diff --git a/pgmpy/tests/test_readwrite/test_BIF.py b/pgmpy/tests/test_readwrite/test_BIF.py
index 8e828441..8ad9d9d7 100644
--- a/pgmpy/tests/test_readwrite/test_BIF.py
+++ b/pgmpy/tests/test_readwrite/test_BIF.py
@@ -422,11 +422,10 @@ probability ( light-on | family-out ) {
         self.maxDiff = None
         self.assertEqual(self.writer.__str__(), self.expected_string)
 
-    @unittest.skip("Fix this when #1221 is resolved")
     def test_write_read_equal(self):
         self.writer.write_bif("test_bif.bif")
         reader = BIFReader("test_bif.bif")
-        read_model = reader.get_model()
+        read_model = reader.get_model(state_name_type=int)
         self.assertEqual(sorted(self.model.nodes()), sorted(read_model.nodes()))
         self.assertEqual(sorted(self.model.edges()), sorted(read_model.edges()))
         for var in self.model.nodes():
diff --git a/pgmpy/tests/test_readwrite/test_XMLBIF.py b/pgmpy/tests/test_readwrite/test_XMLBIF.py
index 7a14d6be..a6e88c85 100644
--- a/pgmpy/tests/test_readwrite/test_XMLBIF.py
+++ b/pgmpy/tests/test_readwrite/test_XMLBIF.py
@@ -355,7 +355,7 @@ class TestXMLBIFWriterMethodsString(unittest.TestCase):
         with open("dog_problem_output.xbif", "r") as f:
             file_text = f.read()
         reader = XMLBIFReader(string=file_text)
-        model = reader.get_model()
+        model = reader.get_model(state_name_type=str)
         self.assert_models_equivelent(self.expected_model, model)
         os.remove("dog_problem_output.xbif")
 
@@ -363,21 +363,13 @@ class TestXMLBIFWriterMethodsString(unittest.TestCase):
         self.writer_stateless.write_xmlbif("grade_problem_output.xbif")
         with open("grade_problem_output.xbif", "r") as f:
             reader = XMLBIFReader(f)
-        model = reader.get_model()
+        model = reader.get_model(state_name_type=int)
         self.assert_models_equivelent(self.model_stateless, model)
         self.assertDictEqual(
-            {
-                "G": ["0", "1", "2"],
-                "I": ["0", "1"],
-                "D": ["0", "1"],
-                "S": ["0", "1"],
-                "L": ["0", "1"],
-            },
-            model.get_cpds("D").state_names,
+            {"D": [0, 1],}, model.get_cpds("D").state_names,
         )
         os.remove("grade_problem_output.xbif")
 
-    @unittest.skip("Fix this when #1221 is resolved")
     def assert_models_equivelent(self, expected, got):
         self.assertSetEqual(set(expected.nodes()), set(got.nodes()))
         for node in expected.nodes():

pip install -e .[all]
parse_log_pytest
requirements.txt
pytest pytest-cov
3.6
requirements/runtime.txt requirements/tests.txt
pytest --no-header -rA --tb=line --color=no -p no:cacheprovider -W ignore::DeprecationWarning
attrs==22.2.0
black==22.8.0
certifi==2021.5.30
charset-normalizer==2.0.12
click==8.0.4
codecov==2.1.13
coverage==6.2
dataclasses==0.8
decorator==4.4.2
idna==3.10
importlib-metadata==4.8.3
importlib-resources==5.4.0
iniconfig==1.1.1
joblib==1.1.1
mock==5.2.0
mypy-extensions==1.0.0
networkx==2.5.1
numpy==1.19.5
packaging==21.3
pandas==1.1.5
pathspec==0.9.0
patsy==1.0.1
-e git+https://github.com/pgmpy/pgmpy.git@f379c8c3ca58651f4309b20289a09e7636fc0157#egg=pgmpy
platformdirs==2.4.0
pluggy==1.0.0
py==1.11.0
pyparsing==3.1.4
pytest==7.0.1
pytest-cov==4.0.0
python-dateutil==2.9.0.post0
pytz==2025.2
requests==2.27.1
scipy==1.5.4
six==1.17.0
statsmodels==0.12.2
tomli==1.2.3
torch==1.10.2
tqdm==4.64.1
typed-ast==1.5.5
typing_extensions==4.1.1
urllib3==1.26.20
xdoctest==1.1.6
zipp==3.6.0

name: pgmpy
channels:
  - defaults
  - https://repo.anaconda.com/pkgs/main
  - https://repo.anaconda.com/pkgs/r
  - conda-forge
dependencies:
  - _libgcc_mutex=0.1=main
  - _openmp_mutex=5.1=1_gnu
  - ca-certificates=2025.2.25=h06a4308_0
  - certifi=2021.5.30=py36h06a4308_0
  - ld_impl_linux-64=2.40=h12ee557_0
  - libffi=3.3=he6710b0_2
  - libgcc-ng=11.2.0=h1234567_1
  - libgomp=11.2.0=h1234567_1
  - libstdcxx-ng=11.2.0=h1234567_1
  - ncurses=6.4=h6a678d5_0
  - openssl=1.1.1w=h7f8727e_0
  - pip=21.2.2=py36h06a4308_0
  - python=3.6.13=h12debd9_1
  - readline=8.2=h5eee18b_0
  - setuptools=58.0.4=py36h06a4308_0
  - sqlite=3.45.3=h5eee18b_0
  - tk=8.6.14=h39e8969_0
  - wheel=0.37.1=pyhd3eb1b0_0
  - xz=5.6.4=h5eee18b_1
  - zlib=1.2.13=h5eee18b_1
  - pip:
      - attrs==22.2.0
      - black==22.8.0
      - charset-normalizer==2.0.12
      - click==8.0.4
      - codecov==2.1.13
      - coverage==6.2
      - dataclasses==0.8
      - decorator==4.4.2
      - idna==3.10
      - importlib-metadata==4.8.3
      - importlib-resources==5.4.0
      - iniconfig==1.1.1
      - joblib==1.1.1
      - mock==5.2.0
      - mypy-extensions==1.0.0
      - networkx==2.5.1
      - numpy==1.19.5
      - packaging==21.3
      - pandas==1.1.5
      - pathspec==0.9.0
      - patsy==1.0.1
      - platformdirs==2.4.0
      - pluggy==1.0.0
      - py==1.11.0
      - pyparsing==3.1.4
      - pytest==7.0.1
      - pytest-cov==4.0.0
      - python-dateutil==2.9.0.post0
      - pytz==2025.2
      - requests==2.27.1
      - scipy==1.5.4
      - six==1.17.0
      - statsmodels==0.12.2
      - tomli==1.2.3
      - torch==1.10.2
      - tqdm==4.64.1
      - typed-ast==1.5.5
      - typing-extensions==4.1.1
      - urllib3==1.26.20
      - xdoctest==1.1.6
      - zipp==3.6.0
prefix: /opt/conda/envs/pgmpy

pgmpy/tests/test_readwrite/test_BIF.py::TestBIFWriter::test_write_read_equal pgmpy/tests/test_readwrite/test_XMLBIF.py::TestXMLBIFWriterMethodsString::test_write_xmlbif_statefull pgmpy/tests/test_readwrite/test_XMLBIF.py::TestXMLBIFWriterMethodsString::test_write_xmlbif_stateless
pgmpy/tests/test_readwrite/test_BIF.py::TestBIFReader::test_get_edges pgmpy/tests/test_readwrite/test_BIF.py::TestBIFReader::test_get_model pgmpy/tests/test_readwrite/test_BIF.py::TestBIFReader::test_get_parents pgmpy/tests/test_readwrite/test_BIF.py::TestBIFReader::test_get_property pgmpy/tests/test_readwrite/test_BIF.py::TestBIFReader::test_get_values pgmpy/tests/test_readwrite/test_BIF.py::TestBIFReader::test_get_values_reordered pgmpy/tests/test_readwrite/test_BIF.py::TestBIFReader::test_get_variables pgmpy/tests/test_readwrite/test_BIF.py::TestBIFReader::test_network_name pgmpy/tests/test_readwrite/test_BIF.py::TestBIFReader::test_states pgmpy/tests/test_readwrite/test_BIF.py::TestBIFReader::test_water_model pgmpy/tests/test_readwrite/test_BIF.py::TestBIFWriter::test_str pgmpy/tests/test_readwrite/test_XMLBIF.py::TestXMLBIFReaderMethods::test_get_edges pgmpy/tests/test_readwrite/test_XMLBIF.py::TestXMLBIFReaderMethods::test_get_parents pgmpy/tests/test_readwrite/test_XMLBIF.py::TestXMLBIFReaderMethods::test_get_property pgmpy/tests/test_readwrite/test_XMLBIF.py::TestXMLBIFReaderMethods::test_get_states pgmpy/tests/test_readwrite/test_XMLBIF.py::TestXMLBIFReaderMethods::test_get_values pgmpy/tests/test_readwrite/test_XMLBIF.py::TestXMLBIFReaderMethods::test_get_variables pgmpy/tests/test_readwrite/test_XMLBIF.py::TestXMLBIFReaderMethods::test_model pgmpy/tests/test_readwrite/test_XMLBIF.py::TestXMLBIFReaderMethodsFile::test_get_edges pgmpy/tests/test_readwrite/test_XMLBIF.py::TestXMLBIFReaderMethodsFile::test_get_parents pgmpy/tests/test_readwrite/test_XMLBIF.py::TestXMLBIFReaderMethodsFile::test_get_property pgmpy/tests/test_readwrite/test_XMLBIF.py::TestXMLBIFReaderMethodsFile::test_get_states pgmpy/tests/test_readwrite/test_XMLBIF.py::TestXMLBIFReaderMethodsFile::test_get_values pgmpy/tests/test_readwrite/test_XMLBIF.py::TestXMLBIFReaderMethodsFile::test_get_variables pgmpy/tests/test_readwrite/test_XMLBIF.py::TestXMLBIFReaderMethodsFile::test_model
pgmpy/readwrite/BIF.py pgmpy/readwrite/XMLBIF.py
pgmpy/readwrite/BIF.py pgmpy/readwrite/XMLBIF.py