Skip to content

Commit

Permalink
Use cached_property
Browse files Browse the repository at this point in the history
  • Loading branch information
ovidner committed Jan 26, 2024
1 parent 13a8dc8 commit abe85f4
Showing 1 changed file with 3 additions and 10 deletions.
13 changes: 3 additions & 10 deletions src/autopack/data_model.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import os
import pathlib
from functools import cached_property
from typing import Any, List, Literal, Optional

import numpy as np
Expand Down Expand Up @@ -102,26 +103,18 @@ def check_costs(cls, v: np.ndarray):

return v

@property
@cached_property
def interpolator(self) -> sp.interpolate.NearestNDInterpolator:
"""
Interpolator for cost field
"""
if hasattr(self, "_interpolator"):
return self._interpolator

flat_coords = self.coordinates.reshape(-1, 3)
flat_costs = self.costs.reshape(-1)
feasible_mask = np.isfinite(flat_costs)
feasible_costs = flat_costs[feasible_mask]
feasible_coords = flat_coords[feasible_mask]

interpolator = sp.interpolate.NearestNDInterpolator(
feasible_coords, feasible_costs
)
self._interpolator = interpolator

return interpolator
return sp.interpolate.NearestNDInterpolator(feasible_coords, feasible_costs)

def interpolate(self, coords: np.ndarray):
"""
Expand Down

0 comments on commit abe85f4

Please sign in to comment.