Skip to content

Commit

Permalink
BUG: make simple micro moments multiprocessing-compatible
Browse files Browse the repository at this point in the history
Use module-level functions instead of lambda functions, which aren't supported by default multiprocessing due to pickling constraints.
  • Loading branch information
jeffgortmaker committed Jun 26, 2023
1 parent 092e468 commit 0a592fd
Showing 1 changed file with 14 additions and 2 deletions.
16 changes: 14 additions & 2 deletions pyblp/micro.py
Original file line number Diff line number Diff line change
Expand Up @@ -324,9 +324,9 @@ def __init__(
if isinstance(parts, MicroPart):
parts = [parts]
if compute_value is None:
compute_value = lambda v: float(v)
compute_value = default_compute_value
if compute_gradient is None:
compute_gradient = lambda v: np.ones_like(v)
compute_gradient = default_compute_gradient
else:
if not isinstance(parts, collections.abc.Sequence) or len(parts) < 1:
raise TypeError("parts must be a MicroPart instance or a sequence of instances.")
Expand Down Expand Up @@ -364,6 +364,18 @@ def __str__(self) -> str:
return f"{self.name}: {format_number(self.value)} ({parts_string})"


def default_compute_value(part_values: Array) -> float:
"""Define the default micro value computation function for a single micro part. This needs to be at the module
level to allow for standard multiprocessing to work.
"""
return float(part_values)


def default_compute_gradient(part_values: Array) -> Array:
"""Define the same but for the gradient."""
return np.ones_like(part_values)


class Moments(object):
"""Information about a sequence of micro moments."""

Expand Down

0 comments on commit 0a592fd

Please sign in to comment.