Example #1
0
 def test_toReproduceAndFixIssue57(self):
     HashMap = jpy.get_type('java.util.HashMap')
     Map = jpy.get_type('java.util.Map')
     m = HashMap()
     c = m.getClass()
     self.assertEqual(c.getName(), 'java.util.HashMap')
     m = jpy.cast(m, Map)
     # without the fix, we get "AttributeError: 'java.util.Map' object has no attribute 'getClass'"
     c = m.getClass()
     self.assertEqual(c.getName(), 'java.util.HashMap')
Example #2
0
 def test_toReproduceAndFixIssue57(self):
     HashMap = jpy.get_type('java.util.HashMap')
     Map = jpy.get_type('java.util.Map')
     m = HashMap()
     c = m.getClass()
     self.assertEqual(c.getName(), 'java.util.HashMap')
     m = jpy.cast(m, Map)
     # without the fix, we get "AttributeError: 'java.util.Map' object has no attribute 'getClass'"
     c = m.getClass()
     self.assertEqual(c.getName(), 'java.util.HashMap')
def PythonShiftAwareListenerAdapter(dynamicTable,
                                    implementation,
                                    description=None,
                                    retain=True):
    """
    Constructs the InstrumentedShiftAwareListenerAdapter, implemented in Python, and plugs it into the table's
    listenForUpdates method.

    :param dynamicTable: table to which to listen - NOTE: it will be cast to DynamicTable.
    :param implementation: the body of the implementation for the InstrumentedShiftAwareListenerAdapter.onUpdate method, and
      must either be a class with onUpdate method or a callable.
    :param description: A description for the UpdatePerformanceTracker to append to its entry description.
    :param retain: Whether a hard reference to this listener should be maintained to prevent it from being collected.
    """

    jtype = jpy.get_type(
        'io.deephaven.integrations.python.PythonShiftAwareListenerAdapter')
    dt = jpy.cast(dynamicTable,
                  jpy.get_type('io.deephaven.db.v2.DynamicTable'))
    ListenerInstance = jtype(description, dt, retain, implementation)
    dt.listenForUpdates(ListenerInstance)
def PythonListenerAdapter(dynamicTable,
                          implementation,
                          description=None,
                          retain=True,
                          replayInitialImage=False):
    """
    Constructs the InstrumentedListenerAdapter, implemented in Python, and plugs it into the table's
    listenForUpdates method.

    :param dynamicTable: table to which to listen - NOTE: it will be cast to DynamicTable.
    :param implementation: the body of the implementation for the InstrumentedListenerAdapter.onUpdate method, and
      must either be a class with onUpdate method or a callable.
    :param description: A description for the UpdatePerformanceTracker to append to its entry description.
    :param retain: Whether a hard reference to this listener should be maintained to prevent it from being collected.
    :param replayInitialImage: False to only process new rows, ignoring any previously existing rows in the Table;
      True to process updates for all initial rows in the table PLUS all new row changes.
    """

    dt = jpy.cast(dynamicTable,
                  jpy.get_type('io.deephaven.db.v2.DynamicTable'))
    jtype = jpy.get_type(
        'io.deephaven.integrations.python.PythonListenerAdapter')
    ListenerInstance = jtype(description, dynamicTable, retain, implementation)
    dt.listenForUpdates(ListenerInstance, replayInitialImage)
def listen(t,
           listener,
           description=None,
           retain=True,
           ltype="auto",
           start_listening=True,
           replay_initial=False,
           lock_type="shared"):
    """
    Listen to table changes.

    Table change events are processed by listener, which can be either (1) a callable (e.g. function) or
    (2) an object which provides an "onUpdate" method.
    In either case, the method must have one of the following signatures.

    * (added, removed, modified): legacy
    * (isReplay, added, removed, modified): legacy + replay
    * (update): shift-aware
    * (isReplay, update): shift-aware + replay

    For legacy listeners, added, removed, and modified are the indices of the rows which changed.
    For shift-aware listeners, update is an object which describes the table update.
    Listeners which support replaying the initial table snapshot have an additional parameter, inReplay, which is
    true when replaying the initial snapshot and false during normal updates.

    See the Deephaven listener documentation for details on processing update events.  This documentation covers the
    details of the added, removed, and modified indices; index shift information; as well as the details of how to apply
    the update object.  It also has examples on how to access current and previous-tick table values.

    :param t: dynamic table to listen to.
    :param listener: listener to process changes.
    :param description: description for the UpdatePerformanceTracker to append to the listener's entry description.
    :param retain: whether a hard reference to this listener should be maintained to prevent it from being collected.
    :param ltype: listener type.  Valid values are "auto", "legacy", and "shift_aware".  "auto" (default) uses inspection to automatically determine the type of input listener.  "legacy" is for a legacy listener, which takes three (added, removed, modified) or four (isReplay, added, removed, modified) arguments.  "shift_aware" is for a shift-aware listener, which takes one (update) or two (isReplay, update) arguments.
    :param start_listening: True to create the listener and register the listener with the table.  The listener will see updates.  False to create the listener, but do not register the listener with the table.  The listener will not see updates.
    :param replay_initial: True to replay the initial table contents to the listener.  False to only listen to new table changes.  To replay the initial image, the listener must support replay.
    :param lock_type: LTM lock type.  Used when replay_initial=True.  See :func:`doLocked` for valid values.
    :return: table listener handle.
    """

    if replay_initial and not start_listening:
        raise ValueError(
            "Unable to create listener.  Inconsistent arguments.  If the initial snapshot is replayed (replay_initial=True), then the listener must be registered to start listening (start_listening=True)."
        )

    _java_type_DynamicTable = jpy.get_type("io.deephaven.db.v2.DynamicTable")
    dt = jpy.cast(t, _java_type_DynamicTable)

    nargs = _nargsListener(listener)

    if ltype == None or ltype == "auto":
        if nargs == 1 or nargs == 2:
            ltype = "shift_aware"
        elif nargs == 3 or nargs == 4:
            ltype = "legacy"
        else:
            raise ValueError(
                "Unable to autodetect listener type.  Listener does not take an expected number of arguments.  args={}"
                .format(nargs))

    if ltype == "legacy":
        if nargs == 3:
            if replay_initial:
                raise ValueError(
                    "Listener does not support replay: ltype={} nargs={}".
                    format(ltype, nargs))

            ListenerAdapter = jpy.get_type(
                "io.deephaven.integrations.python.PythonListenerAdapter")
            listener_adapter = ListenerAdapter(description, dt, retain,
                                               listener)
        elif nargs == 4:
            ListenerAdapter = jpy.get_type(
                "io.deephaven.integrations.python.PythonReplayListenerAdapter")
            listener_adapter = ListenerAdapter(description, dt, retain,
                                               listener)
        else:
            raise ValueError(
                "Legacy listener must take 3 (added, removed, modified) or 4 (isReplay, added, removed, modified) arguments."
            )

    elif ltype == "shift_aware":
        if nargs == 1:
            if replay_initial:
                raise ValueError(
                    "Listener does not support replay: ltype={} nargs={}".
                    format(ltype, nargs))

            ListenerAdapter = jpy.get_type(
                "io.deephaven.integrations.python.PythonShiftAwareListenerAdapter"
            )
            listener_adapter = ListenerAdapter(description, dt, retain,
                                               listener)
        elif nargs == 2:
            ListenerAdapter = jpy.get_type(
                "io.deephaven.integrations.python.PythonShiftAwareReplayListenerAdapter"
            )
            listener_adapter = ListenerAdapter(description, dt, retain,
                                               listener)
        else:
            raise ValueError(
                "Shift-aware listener must take 1 (update) or 2 (isReplay, update) arguments."
            )

    else:
        raise ValueError("Unsupported listener type: ltype={}".format(ltype))

    handle = TableListenerHandle(dt, listener_adapter)

    def start():
        if replay_initial:
            listener_adapter.replay()

        if start_listening:
            handle.register()

    if replay_initial:
        doLocked(start, lock_type=lock_type)
    else:
        start()

    return handle
Example #6
0
    def getPixelValues(self, product, variables, attributes, variable, mask=None):
        """
        Returns pixel values of variable of in-memory products

        :type product: *snappy.Product*
        :param product: In memory representation of data product.

        :type variables: list
        :param variables: list of product variables as ``Variable`` or ``SpectralVariable`` class objects.

        :type attributes: dict
        :param attributes: Dictionary of product attributes

        :type variable: str
        :param variable: Name of variable to return data for.

        :return:
            :pixel_values: *numpy.ndarray*

            specified variable pixels values
        """

        # 1. Pre-processing

        # Check requested variable in variables dictionary
        # if variable not in [v.name for v in variables]:
        #     raise RuntimeError("%s variable not available in opened %s product" % (variable, attributes['product_string']))

        # * special case for time_stamp as values in metadata
        if variable == "time_stamp":
            return self.getTimeStampValues(product, variables, attributes, variable)

        # 2. Open data

        # a. Initialise data array
        w = product.getSceneRasterWidth()
        h = product.getSceneRasterHeight()
        pixel_values = zeros(w * h, float32)

        # b. Get data object, depending on variable type
        band_names = product.getBandNames()
        tie_point_grid_names = product.getTiePointGridNames()
        mask_names = product.getMaskGroup().getNodeNames()
        if variable in band_names:
            obj = product.getBand(variable)
            valid_mask = zeros(h * w, bool_)
            product.getBand(variable).readValidMask(0, 0, w, h, valid_mask)
            valid_mask.shape = h, w

        elif variable in tie_point_grid_names:
            obj = product.getTiePointGrid(variable)
        elif variable in mask_names:
            mask = product.getMaskGroup().get(variable)
            obj = jpy.cast(mask, snappy.Mask)
            pixel_values = zeros(w * h, uint32)

        # c. Populate pixel data array with data from data object
        obj.readPixels(0, 0, w, h, pixel_values)
        pixel_values.shape = h, w

        # d. apply valid data mask to band data
        if variable in band_names:
            if mask is not None:
                pixel_values = ma.masked_array(pixel_values, mask=mask-valid_mask)
            else:
                pixel_values = ma.masked_array(pixel_values, mask=-valid_mask)
            return pixel_values

        if mask is not None:
            pixel_values = ma.masked_array(pixel_values, mask=mask)

        return pixel_values