dagghe / pyOMA2

Python module for conducting operational modal analysis
MIT License
20 stars 6 forks source link

plot_data not working if there's only 1 column #20

Open riegerflo opened 1 month ago

riegerflo commented 1 month ago

When trying to use the plot_data method of SingleSetup and there is only one column in the data, the following error occurs:

TypeError                                 Traceback (most recent call last)
Cell In[116], [line 3](vscode-notebook-cell:?execution_count=116&line=3)
      [1](vscode-notebook-cell:?execution_count=116&line=1) sample = df["Z"].to_numpy()[:, None]
      [2](vscode-notebook-cell:?execution_count=116&line=2) ss = SingleSetup(sample, fs=SAMPLE_RATE)
----> [3](vscode-notebook-cell:?execution_count=116&line=3) fig, ax = ss.plot_data()

File ~/kiwa_anomaly_detection/venv/lib/python3.10/site-packages/pyoma2/setup/single.py:148, in SingleSetup.plot_data(self, nc, names, unit, show_rms)
    [146](https://vscode-remote+ssh-002dremote-002bdgxa100.vscode-resource.vscode-cdn.net/home/rieger/kiwa_anomaly_detection/notebooks/Operational%20Modal%20Analysis/~/kiwa_anomaly_detection/venv/lib/python3.10/site-packages/pyoma2/setup/single.py:146) unit = unit  # str label for the y-axis (unit of measurement)
    [147](https://vscode-remote+ssh-002dremote-002bdgxa100.vscode-resource.vscode-cdn.net/home/rieger/kiwa_anomaly_detection/notebooks/Operational%20Modal%20Analysis/~/kiwa_anomaly_detection/venv/lib/python3.10/site-packages/pyoma2/setup/single.py:147) show_rms = show_rms  # wheter to show or not the rms acc in the plot
--> [148](https://vscode-remote+ssh-002dremote-002bdgxa100.vscode-resource.vscode-cdn.net/home/rieger/kiwa_anomaly_detection/notebooks/Operational%20Modal%20Analysis/~/kiwa_anomaly_detection/venv/lib/python3.10/site-packages/pyoma2/setup/single.py:148) fig, ax = plot.plt_data(data, fs, nc, names, unit, show_rms)
    [149](https://vscode-remote+ssh-002dremote-002bdgxa100.vscode-resource.vscode-cdn.net/home/rieger/kiwa_anomaly_detection/notebooks/Operational%20Modal%20Analysis/~/kiwa_anomaly_detection/venv/lib/python3.10/site-packages/pyoma2/setup/single.py:149) return fig, ax

File ~/kiwa_anomaly_detection/venv/lib/python3.10/site-packages/pyoma2/functions/plot.py:1267, in plt_data(data, fs, nc, names, unit, show_rms)
   [1264](https://vscode-remote+ssh-002dremote-002bdgxa100.vscode-resource.vscode-cdn.net/home/rieger/kiwa_anomaly_detection/notebooks/Operational%20Modal%20Analysis/~/kiwa_anomaly_detection/venv/lib/python3.10/site-packages/pyoma2/functions/plot.py:1264)         kk += 1
   [1265](https://vscode-remote+ssh-002dremote-002bdgxa100.vscode-resource.vscode-cdn.net/home/rieger/kiwa_anomaly_detection/notebooks/Operational%20Modal%20Analysis/~/kiwa_anomaly_detection/venv/lib/python3.10/site-packages/pyoma2/functions/plot.py:1265) # if there is only 1 column
   [1266](https://vscode-remote+ssh-002dremote-002bdgxa100.vscode-resource.vscode-cdn.net/home/rieger/kiwa_anomaly_detection/notebooks/Operational%20Modal%20Analysis/~/kiwa_anomaly_detection/venv/lib/python3.10/site-packages/pyoma2/functions/plot.py:1266) else:
-> [1267](https://vscode-remote+ssh-002dremote-002bdgxa100.vscode-resource.vscode-cdn.net/home/rieger/kiwa_anomaly_detection/notebooks/Operational%20Modal%20Analysis/~/kiwa_anomaly_detection/venv/lib/python3.10/site-packages/pyoma2/functions/plot.py:1267)     ax = axs[ii]
   [1268](https://vscode-remote+ssh-002dremote-002bdgxa100.vscode-resource.vscode-cdn.net/home/rieger/kiwa_anomaly_detection/notebooks/Operational%20Modal%20Analysis/~/kiwa_anomaly_detection/venv/lib/python3.10/site-packages/pyoma2/functions/plot.py:1268)     ax.plot(time, data[:, kk], c="k")
   [1269](https://vscode-remote+ssh-002dremote-002bdgxa100.vscode-resource.vscode-cdn.net/home/rieger/kiwa_anomaly_detection/notebooks/Operational%20Modal%20Analysis/~/kiwa_anomaly_detection/venv/lib/python3.10/site-packages/pyoma2/functions/plot.py:1269)     if names is not None:

TypeError: 'Axes' object is not subscriptable

I think the problem is, that the matplotlib subplots method doesn't return a list of axes if theres only one subplot, but a single axe instance instead. So trying to use an index on this instance will of course fail.

On the other hand, if a one dimensional numpy array is passed, the following error occurs:

IndexError                                Traceback (most recent call last)
Cell In[5], [line 2](vscode-notebook-cell:?execution_count=5&line=2)
      [1](vscode-notebook-cell:?execution_count=5&line=1) sample = df["Z_kistler"][:NUM_SAMPLES].to_numpy()
----> [2](vscode-notebook-cell:?execution_count=5&line=2) ss = SingleSetup(sample, fs=SAMPLE_RATE)
      [3](vscode-notebook-cell:?execution_count=5&line=3) fig, ax = ss.plot_data()

File ~/kiwa_anomaly_detection/venv/lib/python3.10/site-packages/pyoma2/setup/single.py:81, in SingleSetup.__init__(self, data, fs)
     [78](https://vscode-remote+ssh-002dremote-002bdgxa100.vscode-resource.vscode-cdn.net/home/rieger/kiwa_anomaly_detection/notebooks/Operational%20Modal%20Analysis/~/kiwa_anomaly_detection/venv/lib/python3.10/site-packages/pyoma2/setup/single.py:78) self.data = data  # data
     [79](https://vscode-remote+ssh-002dremote-002bdgxa100.vscode-resource.vscode-cdn.net/home/rieger/kiwa_anomaly_detection/notebooks/Operational%20Modal%20Analysis/~/kiwa_anomaly_detection/venv/lib/python3.10/site-packages/pyoma2/setup/single.py:79) self.fs = fs  # sampling frequency [Hz]
---> [81](https://vscode-remote+ssh-002dremote-002bdgxa100.vscode-resource.vscode-cdn.net/home/rieger/kiwa_anomaly_detection/notebooks/Operational%20Modal%20Analysis/~/kiwa_anomaly_detection/venv/lib/python3.10/site-packages/pyoma2/setup/single.py:81) self._initialize_data(data=data, fs=fs)

File ~/kiwa_anomaly_detection/venv/lib/python3.10/site-packages/pyoma2/setup/single.py:94, in SingleSetup._initialize_data(self, data, fs)
     [91](https://vscode-remote+ssh-002dremote-002bdgxa100.vscode-resource.vscode-cdn.net/home/rieger/kiwa_anomaly_detection/notebooks/Operational%20Modal%20Analysis/~/kiwa_anomaly_detection/venv/lib/python3.10/site-packages/pyoma2/setup/single.py:91) self._initial_fs = fs
     [93](https://vscode-remote+ssh-002dremote-002bdgxa100.vscode-resource.vscode-cdn.net/home/rieger/kiwa_anomaly_detection/notebooks/Operational%20Modal%20Analysis/~/kiwa_anomaly_detection/venv/lib/python3.10/site-packages/pyoma2/setup/single.py:93) self.dt = 1 / fs  # sampling interval
---> [94](https://vscode-remote+ssh-002dremote-002bdgxa100.vscode-resource.vscode-cdn.net/home/rieger/kiwa_anomaly_detection/notebooks/Operational%20Modal%20Analysis/~/kiwa_anomaly_detection/venv/lib/python3.10/site-packages/pyoma2/setup/single.py:94) self.Nch = data.shape[1]  # number of channels
     [95](https://vscode-remote+ssh-002dremote-002bdgxa100.vscode-resource.vscode-cdn.net/home/rieger/kiwa_anomaly_detection/notebooks/Operational%20Modal%20Analysis/~/kiwa_anomaly_detection/venv/lib/python3.10/site-packages/pyoma2/setup/single.py:95) self.Ndat = data.shape[0]  # number of data points
     [96](https://vscode-remote+ssh-002dremote-002bdgxa100.vscode-resource.vscode-cdn.net/home/rieger/kiwa_anomaly_detection/notebooks/Operational%20Modal%20Analysis/~/kiwa_anomaly_detection/venv/lib/python3.10/site-packages/pyoma2/setup/single.py:96) self.T = self.dt * self.Ndat  # Period of acquisition [sec]

IndexError: tuple index out of range

Of course, that's because the array only has one dimension.

dagghe commented 1 month ago

Dear @riegerflo thank you for your feedback, we will look into this!