Pylance and mypy cannot find editable local imports

Posted on Thu 01 June 2023 in Pylance, Mypy, Python • Tagged with Pylance, Mypy, Python

The other day, I wrote a post about how I fixed a problem with Pylance not finding a local import. It worked, but now mypy was complaining about the same import.

The solution was installing the package in compat mode with:

pip install -e . --config-settings editable_mode=compat

Reading units from a pickle file with Pint

Posted on Tue 30 May 2023 in Pylance, Python, Pint • Tagged with Pylance, Python, Pint

I have a Python package called cloudmodel that uses Pint to define units and adds some to the default registry. When I read a pickle file that use these units, I get an error:

int.errors.UndefinedUnitError: 'usd' is not defined in the unit registry

The way to fix it is to set the application registry to the one used by cloudmodel:

from cloudmodel.unified.units import ureg
from pint import set_application_registry

set_application_registry(ureg)

Pylance cannot find editable local import

Posted on Sat 27 May 2023 in Pylance, Python • Tagged with Pylance, Python

I had a problem with Pylance not finding a local import. I had installed the package in editable mode with pip install -e . and it was working fine in the terminal, but Pylance was not able to find it.

The solution was installing the package in strict mode with:

pip install -e . --config-settings editable_mode=strict

I got there from this troubleshooting page and this explanation in the Setuptools documentation.


Click removal in audio files with Python

Posted on Tue 23 May 2023 in Audio, Python • Tagged with Audio, Python

A friend of mine had an audio file with many clicks in it. He asked me if I could remove them. I tried with the Audacity click removal tool, but it didn't work: it removed the clicks, but created a lot of artifacts in the audio, so I thought it would be a nice challenge to try to do it with Python.

The waveform looked like this:

Waveform with clicks

I did it using the pydub package. The basic idea is detecting the clicks and replacing them with the previous sample. I used two different criteria to detect the clicks:

  • If the average of the previous samples is below a threshold and the current sample is above another threshold.

  • If the difference between the previous sample and the current one is above a threshold.

The code is not very clean, but it worked. I had to play with the thresholds and the width of the samples to get the best results. I also had to play with the width of the clicks to avoid removing too much audio …


Continue reading

Computing the MASE for historical_forecasts in darts

Posted on Thu 11 May 2023 in Time series prediction, darts • Tagged with Time series prediction, darts

I haven't found any example online on how to compute the MASE (Mean Absolute Scaled Error) for historical forecasts in darts, so I thought I'd share this example:

import numpy as np
from darts import TimeSeries
from darts.models import NaiveSeasonal
from darts.metrics import mase

my_data = [40, 42, 41, 45, 45, 47, 50]
my_np = np.array(my_data)
my_ts = TimeSeries.from_values(my_np)
my_model = NaiveSeasonal(K=1)
start = 3
my_forecast = my_model.historical_forecasts(series=my_ts, start=start, forecast_horizon=1)
my_mase = mase(actual_series=my_ts, pred_series=my_forecast, insample=my_ts[:start])
print(f"my_mase: {my_mase}")
my_ts.plot(label="original", marker="o")
my_forecast.plot(label="historical_forecasts", marker="x")

Notice that the insample parameter is set to the original time series up to the start of the historical forecast.