Example #1
0
    def __init__(self,
                 n_components,
                 opu=None,
                 ndims=1,
                 n_2d_features=None,
                 packed=False,
                 simulated=False,
                 max_n_features=None,
                 verbose_level=-1,
                 linear=False):
        if verbose_level >= 0:
            lightonml.set_verbose_level(verbose_level)
        self.verbose_level = lightonml.get_verbose_level()
        super(OPUMap, self).__init__()
        if opu is None:
            if simulated:
                simulated_opu = SimulatedOpuDevice()
                if max_n_features is None:
                    raise ValueError(
                        "When using simulated=True, you need to provide max_n_features."
                    )
                self.opu = OPU(opu_device=simulated_opu,
                               max_n_features=max_n_features,
                               n_components=n_components)
            else:
                self.opu = OPU(n_components=n_components)
        else:
            self.opu = opu
            self.opu.n_components = n_components
            if simulated and not isinstance(opu.device, SimulatedOpuDevice):
                warnings.warn(
                    "You provided a real OPU object but set simulated=True."
                    " Will use the real OPU.")
            if isinstance(opu.device, SimulatedOpuDevice) and not simulated:
                warnings.warn(
                    "You provided a simulated OPU object but set simulated=False. "
                    "Will use simulated OPU.")
        self.n_components = self.opu.n_components
        if ndims not in [1, 2]:
            raise ValueError("Number of input dimensions must be 1 or 2")
        self.ndims = ndims
        self.n_2d_features = n_2d_features
        self.packed = packed
        self.simulated = simulated
        self.linear = linear
        self.max_n_features = max_n_features

        self.fitted = False
        self.online = False
        if lightonml.get_verbose_level() >= 1:
            print("OPU output is detached from the computational graph.")
Example #2
0
    def __init__(self,
                 n_components,
                 opu=None,
                 ndims=1,
                 n_2d_features=None,
                 packed=False,
                 simulated=False,
                 max_n_features=None,
                 verbose_level=-1,
                 linear=False):
        # verbose_level shouldn't be used anymore, but put it as attributes
        # in order to comply with sklearn estimator
        if verbose_level >= 0:
            lightonml.set_verbose_level(verbose_level)
        self.verbose_level = lightonml.get_verbose_level()

        if opu is None:
            if simulated:
                simulated_opu_device = SimulatedOpuDevice()
                if max_n_features is None:
                    raise ValueError(
                        "When using simulated=True, you need to provide max_n_features."
                    )
                self.opu = OPU(opu_device=simulated_opu_device,
                               max_n_features=max_n_features,
                               n_components=n_components)
            else:
                self.opu = OPU(n_components=n_components)
        else:
            self.opu = opu
            self.opu.n_components = n_components
            if simulated and not isinstance(opu.device, SimulatedOpuDevice):
                warnings.warn(
                    "You provided a real OPU object but set simulated=True."
                    " Will use the real OPU.")
            if isinstance(opu.device, SimulatedOpuDevice) and not simulated:
                warnings.warn(
                    "You provided a simulated OPU object but set simulated=False."
                    " Will use simulated OPU.")

        if ndims not in [1, 2]:
            raise ValueError("Number of input dimensions must be 1 or 2")
        self.ndims = ndims
        self.n_2d_features = n_2d_features
        self.packed = packed
        self.simulated = simulated
        self.linear = linear
        self.max_n_features = max_n_features
        self.fitted = False
Example #3
0
    def __init__(self,
                 n_components: int = 200000,
                 opu_device: Optional[Union["OpuDevice",
                                            SimulatedOpuDevice]] = None,
                 max_n_features: int = 1000,
                 config_file: str = "",
                 config_override: dict = None,
                 verbose_level: int = -1,
                 input_roi_strategy: types.InputRoiStrategy = types.
                 InputRoiStrategy.full,
                 open_at_init: bool = None,
                 disable_pbar=False,
                 simulated=False,
                 rescale: Union[OutputRescaling,
                                str] = OutputRescaling.variance):

        self.__opu_config = None
        self.__config_file = config_file
        self.__config_override = config_override
        self._max_n_features = max_n_features
        self.disable_pbar = disable_pbar
        self.rescale = rescale

        # Get trace and print functions
        if verbose_level != -1:
            warnings.warn(
                "Verbose level arg will removed in 1.3, "
                "Use lightonml.set_verbose_level instead", DeprecationWarning)
            lightonml.set_verbose_level(verbose_level)
        else:
            verbose_level = lightonml.get_verbose_level()
        self._debug = lightonml.get_debug_fn()
        self._trace = lightonml.get_trace_fn()
        self._print = lightonml.get_print_fn()
        no_config_msg = "No configuration files for the OPU was found on this machine.\n" \
                        "You may want to run the OPU in a simulated manner, by passing the " \
                        "simulated argument to True at init.\n" \
                        "See https://docs.lighton.ai/notes/get_started.html#Simulating-an-OPU " \
                        "for more details.\n" \
                        "See also https://lighton.ai/products for getting access to our technology."

        if simulated and opu_device is not None:
            raise ValueError(
                "simulated and opu_device arguments are conflicting")

        # Device init, or take the one passed as input
        if opu_device:
            if type(opu_device).__name__ not in [
                    "SimulatedOpuDevice", "OpuDevice"
            ]:
                raise TypeError(
                    "opu_device must be of type SimulatedOpuDevice or OpuDevice"
                )
            self.device = opu_device
        elif simulated:
            self.device = SimulatedOpuDevice()
        else:
            # Instantiate device directly
            from lightonopu.internal.device import OpuDevice
            if not self.__config_file and not config.host_has_opu_config():
                # Looks like there's no OPU on this host as we didn't find configuration files
                raise RuntimeError(no_config_msg)
            opu_type = self.config["type"]
            frametime_us = self.config["input"]["frametime_us"]
            exposure_us = self.config["output"]["exposure_us"]
            seq_nb_prelim = self.config.get("sequence_nb_prelim", 0)
            name = self.config["name"]
            self.device = OpuDevice(opu_type, frametime_us, exposure_us,
                                    seq_nb_prelim, None, verbose_level, name)

        self._base_frametime_us = self.device.frametime_us
        self._base_exposure_us = self.device.exposure_us

        if self._s.simulated:
            # build the random matrix if not done already
            self._resize_rnd_matrix(max_n_features, n_components)
        else:
            # Make sure lightonopu is at 1.4.1 or later, needed for linear_reconstruction
            pkg_resources.require("lightonopu>=1.4.1")
            # initialize linear_reconstruction library
            from lightonopu import linear_reconstruction
            linear_reconstruction.init(np.prod(self.device.input_shape))

            self._output_roi = output_roi.OutputRoi(
                self.device.output_shape_max, self.device.output_roi_strategy,
                self._s.allowed_roi, self._s.min_n_components)
        # This also sets the output ROI
        self.n_components = n_components
        self.input_roi_strategy = input_roi_strategy
        # Runner initialized when entering fit
        self._runner = None  # type: Optional[TransformRunner]
        # ExitStack for device acquisition, initialized when entering fit
        self._acq_stack = ExitStack()
        self._trace("OPU initialized")

        # Open at init, unless relevant host.json option is False
        if open_at_init is None:
            open_at_init = get_host_option("lightonml_open_at_init", True)
        if open_at_init:
            self.open()
Example #4
0
 def __try_or_giveup(self, func, *args, **kwargs):
     # in verbose mode, print the exception
     print_exc = get_verbose_level() >= 2
     return utils.try_or_giveup(func, self.s.n_tries, print_exc, *args,
                                **kwargs)
Example #5
0
 def __init__(self, total, description, disable):
     self.total = total
     self.disable_pbar = get_verbose_level() < 1 or disable
     self.pbar = None
     self.description = description
Example #6
0
    def __init__(self, n_components: int = 200000,
                 opu_device: Optional[Union[OpuDevice, SimulatedOpuDevice]] = None,
                 max_n_features: int = 1000, config_file: str = "",
                 config_override: dict = None, verbose_level: int = -1,
                 input_roi_strategy: types.InputRoiStrategy = types.InputRoiStrategy.auto,
                 open_at_init: bool = None, disable_pbar=False):

        self.__opu_config = None
        self.__config_file = config_file
        self.__config_override = config_override
        self._max_n_features = max_n_features
        self.disable_pbar = disable_pbar

        # Get trace and print functions
        if verbose_level != -1:
            warnings.warn("Verbose level arg will removed in 1.3, "
                          "Use lightonml.set_verbose_level instead",
                          DeprecationWarning)
            lightonml.set_verbose_level(verbose_level)
        else:
            verbose_level = lightonml.get_verbose_level()
        self._debug = lightonml.get_debug_fn()
        self._trace = lightonml.get_trace_fn()
        self._print = lightonml.get_print_fn()

        # Device init, or take the one passed as input
        if not opu_device:
            opu_type = self.config["type"]
            self._base_frametime_us = self.config["input"]["frametime_us"]
            self._base_exposure_us = self.config["output"]["exposure_us"]
            seq_nb_prelim = self.config.get("sequence_nb_prelim", 0)
            name = self.config["name"]
            self.device = OpuDevice(opu_type, self._base_frametime_us,
                                    self._base_exposure_us, seq_nb_prelim,
                                    None, verbose_level, name)
        else:
            if not isinstance(opu_device, (SimulatedOpuDevice, OpuDevice)):
                raise TypeError("opu_device must be of type {} or {}"
                                .format(SimulatedOpuDevice.__qualname__,
                                        OpuDevice.__qualname__))
            self.device = opu_device
            self._base_frametime_us = self.device.frametime_us
            self._base_exposure_us = self.device.exposure_us

        if self._s.simulated:
            # build the random matrix if not done already
            self._resize_rnd_matrix(max_n_features, n_components)

        self._output_roi = output_roi.OutputRoi(self.device.output_shape_max,
                                                self.device.output_roi_strategy,
                                                self._s.allowed_roi, self._s.min_n_components)
        # This also sets the output ROI
        self.n_components = n_components
        self.input_roi_strategy = input_roi_strategy
        # Runner initialized when entering fit
        self._runner = None  # type: Optional[TransformRunner]
        # ExitStack for device acquisition, initialized when entering fit
        self._acq_stack = ExitStack()
        self._trace("OPU initialized")

        # Open at init, unless relevant host.json option is False
        if open_at_init is None:
            open_at_init = get_host_option("lightonml_open_at_init", True)
        if open_at_init:
            self.open()