johannfaouzi / pyts

A Python package for time series classification
https://pyts.readthedocs.io
BSD 3-Clause "New" or "Revised" License
1.75k stars 163 forks source link

ImportError: cannot import name 'MTF' from 'pyts.image' #125

Closed elcolie closed 2 years ago

elcolie commented 2 years ago

Description

ImportError: cannot import name 'MTF' from 'pyts.image'

Steps/Code to Reproduce

Copy and paste from here

import numpy as np
import matplotlib.pyplot as plt
from pyts.image import MTF

# Parameters
n_samples, n_features = 100, 144

# Toy dataset
rng = np.random.RandomState(41)
X = rng.randn(n_samples, n_features)

# MTF transformation
image_size = 24
mtf = MTF(image_size)
X_mtf = mtf.fit_transform(X)

# Show the results for the first time series
plt.figure(figsize=(8, 8))
plt.imshow(X_mtf[0], cmap='rainbow', origin='lower')
plt.show()

Versions

NumPy 1.21.2 SciPy 1.7.1 Scikit-Learn 0.24.2 Numba 0.53.1 Pyts 0.12.0

johannfaouzi commented 2 years ago

Hi,

The documentation is now hosted on Read the Docs (https://pyts.readthedocs.io/en/stable/index.html). The documentation from the link corresponds to an old version of the package. Here is the new example for Markov Transition Field. MTF has been renamed MarkovTransitionField.

Best, Johann

elcolie commented 2 years ago

Nothing work.

from pyts.classification import BOSSVS
from pyts.datasets import load_gunpoint
X_train, X_test, y_train, y_test = load_gunpoint(return_X_y=True)
clf = BOSSVS(window_size=28)
clf.fit(X_train, y_train)

clf.score(X_test, y_test)

I got

---------------------------------------------------------------------------
ModuleNotFoundError                       Traceback (most recent call last)
Input In [9], in <module>
----> 1 from pyts.classification import BOSSVS
      2 from pyts.datasets import load_gunpoint
      3 X_train, X_test, y_train, y_test = load_gunpoint(return_X_y=True)

ModuleNotFoundError: No module named 'pyts.classification'

==================================================

r"""
==============================
Single Markov transition field
==============================

A Markov transition field is an image obtained from a time series, representing
a field of transition probabilities for a discretized time series.
Different strategies can be used to bin time series.
It is implemented as :class:`pyts.image.MarkovTransitionField`.

In this example, the considered time series is the sequence of the sine
function values for 1000 equally-spaced points in the interval
:math:`[0, 4\pi]`.
One can see on the Markov transition field that the sine function is periodic
with period :math:`2\pi` and smooth (only neighbor bins have a positive
transition probability).

Since the API is designed for machine learning, the
:meth:`~pyts.image.MarkovTransitionField.transform` method of the
:class:`pyts.image.MarkovTransitionField` class expects a data set of time
series as input, so the time series is transformed into a data set with a
single time series (``X = np.array([x])``) and the first element of the data
set of Gramian angular fields is retrieved (``ax_mtf.imshow(X_mtf[0], ...``).
"""

# Author: Johann Faouzi <johann.faouzi@gmail.com>
# License: BSD-3-Clause

import numpy as np
import matplotlib.pyplot as plt
from pyts.image import MarkovTransitionField

# Create a toy time series using the sine function
time_points = np.linspace(0, 4 * np.pi, 1000)
x = np.sin(time_points)
X = np.array([x])

# Compute Gramian angular fields
mtf = MarkovTransitionField(n_bins=8)
X_mtf = mtf.fit_transform(X)

# Plot the time series and its Markov transition field
width_ratios = (2, 7, 0.4)
height_ratios = (2, 7)
width = 6
height = width * sum(height_ratios) / sum(width_ratios)
fig = plt.figure(figsize=(width, height))
gs = fig.add_gridspec(2, 3,  width_ratios=width_ratios,
                      height_ratios=height_ratios,
                      left=0.1, right=0.9, bottom=0.1, top=0.9,
                      wspace=0.05, hspace=0.05)

# Define the ticks and their labels for both axes
time_ticks = np.linspace(0, 4 * np.pi, 9)
time_ticklabels = [r'$0$', r'$\frac{\pi}{2}$', r'$\pi$',
                   r'$\frac{3\pi}{2}$', r'$2\pi$', r'$\frac{5\pi}{2}$',
                   r'$3\pi$', r'$\frac{7\pi}{2}$', r'$4\pi$']
value_ticks = [-1, 0, 1]
reversed_value_ticks = value_ticks[::-1]

# Plot the time series on the left with inverted axes
ax_left = fig.add_subplot(gs[1, 0])
ax_left.plot(x, time_points)
ax_left.set_xticks(reversed_value_ticks)
ax_left.set_xticklabels(reversed_value_ticks, rotation=90)
ax_left.set_yticks(time_ticks)
ax_left.set_yticklabels(time_ticklabels, rotation=90)
ax_left.set_ylim((0, 4 * np.pi))
ax_left.invert_xaxis()

# Plot the time series on the top
ax_top = fig.add_subplot(gs[0, 1])
ax_top.plot(time_points, x)
ax_top.set_xticks(time_ticks)
ax_top.set_xticklabels(time_ticklabels)
ax_top.set_yticks(value_ticks)
ax_top.set_yticklabels(value_ticks)
ax_top.xaxis.tick_top()
ax_top.set_xlim((0, 4 * np.pi))
ax_top.set_yticklabels(value_ticks)

# Plot the Gramian angular fields on the bottom right
ax_mtf = fig.add_subplot(gs[1, 1])
im = ax_mtf.imshow(X_mtf[0], cmap='rainbow', origin='lower', vmin=0., vmax=1.,
                   extent=[0, 4 * np.pi, 0, 4 * np.pi])
ax_mtf.set_xticks([])
ax_mtf.set_yticks([])
ax_mtf.set_title('Markov Transition Field', y=-0.09)

# Add colorbar
ax_cbar = fig.add_subplot(gs[1, 2])
fig.colorbar(im, cax=ax_cbar)

plt.show()

I got error

---------------------------------------------------------------------------
ModuleNotFoundError                       Traceback (most recent call last)
Input In [10], in <module>
      4 import numpy as np
      5 import matplotlib.pyplot as plt
----> 6 from pyts.image import MarkovTransitionField
      9 # Create a toy time series using the sine function
     10 time_points = np.linspace(0, 4 * np.pi, 1000)

ModuleNotFoundError: No module named 'pyts.image'
johannfaouzi commented 2 years ago

Did you install the package? The error raised makes me think that pyts is not installed. You can install it using either pip install pyts or conda install -c conda-forge pyts.