Skip to content

fix typeError

fix typeError #1243

GitHub Actions / Core / Unit Test Results (3.11) failed Feb 18, 2025 in 0s

1 fail, 716 pass in 7m 7s

717 tests  ±0   716 ✅  - 1   7m 7s ⏱️ -10s
  1 suites ±0     0 💤 ±0 
  1 files   ±0     1 ❌ +1 

Results for commit 0b6a81e. ± Comparison against earlier commit 9919d11.

Annotations

Check warning on line 0 in climada.hazard.test.test_tc_tracks.TestFuncs

See this annotation in the file changed.

@github-actions github-actions / Core / Unit Test Results (3.11)

test_compute_density_tracks (climada.hazard.test.test_tc_tracks.TestFuncs) failed

tests_xml/tests.xml [took 0s]
Raw output
numpy.core._exceptions._UFuncNoLoopError: ufunc 'greater' did not contain a loop with signature matching types (<class 'numpy.dtypes.TimeDelta64DType'>, <class 'numpy.dtypes.Float64DType'>) -> None
numpy.exceptions.DTypePromotionError: The DType <class 'numpy.dtypes.TimeDelta64DType'> could not be promoted by <class 'numpy.dtypes.Float64DType'>. This means that no common DType exists for the given inputs. For example they cannot be stored in a single array unless the dtype is `object`. The full list of DTypes is: (<class 'numpy.dtypes.TimeDelta64DType'>, <class 'numpy.dtypes.Float64DType'>)

The above exception was the direct cause of the following exception:

self = <climada.hazard.test.test_tc_tracks.TestFuncs testMethod=test_compute_density_tracks>

    def test_compute_density_tracks(self):
        """Test `compute_track_density` to ensure proper density count."""
        # create track
        track = xr.Dataset(
            {
                "time_step": ("time", np.timedelta64(1, "h") * np.arange(4)),
                "max_sustained_wind": ("time", [10, 20, 30, 20]),
                "central_pressure": ("time", [1, 1, 1, 1]),
                "radius_max_wind": ("time", [1, 1, 1, 1]),
                "environnmental_pressure": ("time", [1, 1, 1, 1]),
                "basin": ("time", ["NA", "NA", "NA", "NA"]),
            },
            coords={
                "time": ("time", pd.date_range("2025-01-01", periods=4, freq="12H")),
                "lat": ("time", [-90, -90, -90, -90]),
                "lon": ("time", [-179, -169, -159, -149]),
            },
            attrs={
                "max_sustained_wind_unit": "m/s",
                "central_pressure_unit": "hPa",
                "name": "storm_0",
                "sid": "0",
                "orig_event_flag": True,
                "data_provider": "FAST",
                "id_no": "0",
                "category": "1",
            },
        )
    
        tc_tracks = tc.TCTracks([track])
    
>       hist_abs, *_ = tc.compute_track_density(
            tc_tracks,
            res=10,
            density=False,
        )

climada/hazard/test/test_tc_tracks.py:1282: 
_ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ 

tc_track = <climada.hazard.tc_tracks.TCTracks object at 0x7f1ad93cdf50>
res = 10, density = False, filter_tracks = True, wind_min = None
wind_max = None

    def compute_track_density(
        tc_track: TCTracks,
        res: int = 5,
        density: bool = False,
        filter_tracks: bool = True,
        wind_min: float = None,
        wind_max: float = None,
    ) -> tuple[np.ndarray, tuple]:
        """Compute absolute and normalized tropical cyclone track density. Before using this function,
        apply the same temporal resolution to all tracks by calling :py:meth:`equal_timestep` on the
        TCTrack object. Due to the computational cost of the this function, it is not recommended to
        use a grid resolution higher tha 0.1°. This function it creates 2D bins of the specified
        resolution (e.g. 1° x 1°). Second, since tracks are not lines but a series of points, it counts
        the number of points per bin. Lastly, it returns the absolute or normalized count per bin.
        To plot the output of this function use :py:meth:`plot_track_density`.
    
        Parameters:
        ----------
        res: int (optional) Default: 5°
            resolution in degrees of the grid bins in which the density will be computed
        density: bool (optional) default: False
            If False it returns the number of samples in each bin. If True, returns the
            probability density function at each bin computed as count_bin / grid_area.
        filter_tracks: bool (optional) default: True
            If True the track density is computed as the number of different tracks crossing a grid
            cell. If False, the track density takes into account how long the track stayed in each
            grid cell. Hence slower tracks increase the density if the parameter is set to False.
        wind_min: float (optional) default: None
            Minimum wind speed above which to select tracks.
        wind_max: float (optional) default: None
            Maximal wind speed below which to select tracks.
        Returns:
        -------
        hist: 2D np.ndwind_speeday
            2D matrix containing the track density
    
        Example:
        --------
        >>> tc_tracks = TCTrack.from_ibtracs_netcdf("path_to_file")
        >>> tc_tracks.equal_timestep(time_steph_h = 1)
        >>> hist_count = compute_track_density(res = 1)
        >>> plot_track_density(hist_count)
    
        """
    
        limit_ratio = 1.12 * 1.1  # record tc speed 112km/h -> 1.12°/h + 10% margin
    
>       if tc_track.data[0].time_step[0].values > res / limit_ratio:
E       numpy.core._exceptions._UFuncNoLoopError: ufunc 'greater' did not contain a loop with signature matching types (<class 'numpy.dtypes.TimeDelta64DType'>, <class 'numpy.dtypes.Float64DType'>) -> None

climada/hazard/tc_tracks.py:3032: UFuncTypeError