lbl-srg / BuildingsPy

Python modules for automating Modelica simulations and for running unit test for the Buildings library
80 stars 46 forks source link

Encountering ValueError: read length must be non-negative or -1 when running buildingspy reader #572

Closed hoxj180 closed 2 weeks ago

hoxj180 commented 2 weeks ago

Dear buildingspy developers,

I simulated a model in Dymola and am trying to read my exported .mat file results in Python. The results look fine when I plot them in Dymola, and I am able to read the .mat files back into dymola and interact with results when I drag the .mat file into the Plot window of a new Dymola instance. However, when I try to read them into Python using Reader from buildingspy, I get the error ValueError: read length must be non-negative or -1.

The code I am using to read in the result is the following:

import os
from buildingspy.io.outputfile import Reader

filepath = r'C:\...\results'
r = Reader(os.path.join(filepath, 'file.mat'), "dymola")

I have tried to resimulate the model and export the new results, but get the same ValueError. I have also toggled through a few of the variables in the Dymola plot window to see if anything looks odd, but all the variables I looked at are plotted fine and look realistic. When I read in other versions of this model's outputs into Python using this code, it reads successfully.

Have you seen this error before, and do you have any insights about how to resolve it? It seems to me like there is something in my results that is preventing buildingspy from reading it, but is not preventing successful simulation or from being plotted in Dymola. Thanks so much.

mwetter commented 2 weeks ago

I haven't seen this error before. Can you post the full error message and a sample .mat file that reproduces it.

hoxj180 commented 2 weeks ago

Below is the full error. I have shared the file with you since it is too large to fit here, and smaller timeframes do not reproduce the same error.

Traceback (most recent call last):

  File ~\AppData\Local\anaconda3\envs\postProcess\Lib\site-packages\spyder_kernels\py3compat.py:356 in compat_exec
    exec(code, globals, locals)

  File c:\...\untitled0.py:24
    r = Reader(os.path.join(file, '72trb40.mat'), "dymola")

  File ~\AppData\Local\anaconda3\envs\postProcess\Lib\site-packages\buildingspy\io\outputfile.py:179 in __init__
    self._data_ = DyMatFile(fileName)

  File ~\AppData\Local\anaconda3\envs\postProcess\Lib\site-packages\buildingspy\thirdParty\dymat\DyMat\__init__.py:44 in __init__
    self.mat = scipy.io.loadmat(fileName, chars_as_strings=False)

  File ~\AppData\Local\anaconda3\envs\postProcess\Lib\site-packages\scipy\io\matlab\_mio.py:227 in loadmat
    matfile_dict = MR.get_variables(variable_names)

  File ~\AppData\Local\anaconda3\envs\postProcess\Lib\site-packages\scipy\io\matlab\_mio4.py:404 in get_variables
    mdict[name] = self.read_var_array(hdr)

  File ~\AppData\Local\anaconda3\envs\postProcess\Lib\site-packages\scipy\io\matlab\_mio4.py:379 in read_var_array
    return self._matrix_reader.array_from_header(header, process)

  File ~\AppData\Local\anaconda3\envs\postProcess\Lib\site-packages\scipy\io\matlab\_mio4.py:141 in array_from_header
    arr = self.read_full_array(hdr)

  File ~\AppData\Local\anaconda3\envs\postProcess\Lib\site-packages\scipy\io\matlab\_mio4.py:211 in read_full_array
    return self.read_sub_array(hdr)

  File ~\AppData\Local\anaconda3\envs\postProcess\Lib\site-packages\scipy\io\matlab\_mio4.py:177 in read_sub_array
    buffer = self.mat_stream.read(int(num_bytes))

ValueError: read length must be non-negative or -1
mwetter commented 2 weeks ago

I don't think there is anything we can do here. Essentially the Modelica tool writes a .mat file, and scipy.io.matlab can't read it. Neither of these tools is in our control. You may try to isolate it further and contact one of these repositories.

hoxj180 commented 2 weeks ago

Thank you Dr. Wetter! I appreciate you looking into it.