Example #1
0
    def plotImage(self, z_dates, z_data, save_file):
        logging.info('Plotting image')

        ymin = 0 - float(self.ybuffer_space)
        ymax = len(self.z_labels)  # + float(self.ybuffer_space)

        logging.debug('Set ymax to {y}'.format(y=ymax))

        plt.tick_params(axis='both', which='major', labelsize=self.font_size)
        plt.xlabel('Delivery Dates', fontsize=self.font_size)   # Dates
        plt.ylabel('')   # Categories
        plt.gcf().autofmt_xdate()

        for index in range(len(self.z_labels)):
            logging.debug('Plotting {i}: {l}'.format(i=index, l=self.z_labels[index]))
            ax = self.fig.add_subplot(111)
            ax.plot(
                numpy_array(z_dates[index]),
                numpy_array([index] * len(z_dates[index])),
                marker='^',
                markersize=15,
                # color='r',  # let it auto pick for colors
                linestyle='-',
            )
            for subindex in range(len(z_dates[index])):
                this_date = z_dates[index][subindex]
                txt = z_data[index][subindex]
                txt = textwrap.fill(txt, width=28)
                ax.annotate(
                    s=txt,
                    xy=(this_date, float(index) + 0.05),
                    xycoords='data',
                    rotation=self.annotate_rotation,
                    rotation_mode='anchor',
                    size=self.font_size,
                    va='bottom',
                    ha='left',
                )

        ax.set_ylim([ymin, ymax])

        locator = matplotlib.dates.MonthLocator(bymonth=[1, 5, 9])
        self.fig.gca().yaxis.set_major_formatter(
            matplotlib.ticker.FuncFormatter(self.labelFormatter)
        )
        self.fig.gca().yaxis.set_major_locator(
            matplotlib.ticker.MultipleLocator(1)
        )
        self.fig.gca().xaxis.set_major_formatter(
            matplotlib.dates.DateFormatter('%m/%d/%Y')
        )
        self.fig.gca().xaxis.set_major_locator(locator)
        self.fig.gca().xaxis.grid(True)

        today = datetime.today()
        ax.axvline(today, color='r', linestyle='--')

        self.canvas.draw()
	def _pose_to_pose_dist(self, pose1, pose2) :
		'''
			Calculate the distance between two given poses
			Does not take into account orientation
		'''
		return norm(
				numpy_array([pose1.pose.position.x, pose1.pose.position.y, pose1.pose.position.z]) - 
				numpy_array([pose2.pose.position.x, pose2.pose.position.y, pose2.pose.position.z])
			)
Example #3
0
    def plotImage(self, z_dates, z_data, save_file):
        logging.info('Plotting image')

        ymin = 0 - float(self.ybuffer_space)
        ymax = len(self.z_labels)  # + float(self.ybuffer_space)

        logging.debug('Set ymax to {y}'.format(y=ymax))

        plt.tick_params(axis='both', which='major', labelsize=self.font_size)
        plt.xlabel('Delivery Dates', fontsize=self.font_size)  # Dates
        plt.ylabel('')  # Categories
        plt.gcf().autofmt_xdate()

        for index in range(len(self.z_labels)):
            logging.debug('Plotting {i}: {l}'.format(i=index,
                                                     l=self.z_labels[index]))
            ax = self.fig.add_subplot(111)
            ax.plot(
                numpy_array(z_dates[index]),
                numpy_array([index] * len(z_dates[index])),
                marker='^',
                markersize=15,
                # color='r',  # let it auto pick for colors
                linestyle='-',
            )
            for subindex in range(len(z_dates[index])):
                this_date = z_dates[index][subindex]
                txt = z_data[index][subindex]
                txt = textwrap.fill(txt, width=28)
                ax.annotate(
                    s=txt,
                    xy=(this_date, float(index) + 0.05),
                    xycoords='data',
                    rotation=self.annotate_rotation,
                    rotation_mode='anchor',
                    size=self.font_size,
                    va='bottom',
                    ha='left',
                )

        ax.set_ylim([ymin, ymax])

        locator = matplotlib.dates.MonthLocator(bymonth=[1, 5, 9])
        self.fig.gca().yaxis.set_major_formatter(
            matplotlib.ticker.FuncFormatter(self.labelFormatter))
        self.fig.gca().yaxis.set_major_locator(
            matplotlib.ticker.MultipleLocator(1))
        self.fig.gca().xaxis.set_major_formatter(
            matplotlib.dates.DateFormatter('%m/%d/%Y'))
        self.fig.gca().xaxis.set_major_locator(locator)
        self.fig.gca().xaxis.grid(True)

        today = datetime.today()
        ax.axvline(today, color='r', linestyle='--')

        self.canvas.draw()
Example #4
0
 def boundingBox(self):
     if len(self.points) == 0:
         #no curves to draw
         #defaults to (-1,-1) and (1,1) but axis can be set in Draw
         minXY= numpy_array([-1,-1])
         maxXY= numpy_array([ 1, 1])
     else:
         minXY= numpy.minimum.reduce(self.points)
         maxXY= numpy.maximum.reduce(self.points)
     return minXY, maxXY
Example #5
0
 def _setSize(self, width=None, height=None):
     """DC width and height."""
     if width == None:
         (self.width,self.height) = self.GetClientSizeTuple()
     else:
         self.width, self.height= width,height
     self.plotbox_size = (1-self.border)*numpy_array([self.width, self.height])
     xo = 0.5*(self.width-self.plotbox_size[0])
     yo = self.height-0.5*(self.height-self.plotbox_size[1])
     self.plotbox_origin = numpy_array([xo, yo])
Example #6
0
 def boundingBox(self):
     if len(self.points) == 0:
         #no curves to draw
         #defaults to (-1,-1) and (1,1) but axis can be set in Draw
         minXY = numpy_array([-1, -1])
         maxXY = numpy_array([1, 1])
     else:
         minXY = numpy.minimum.reduce(self.points)
         maxXY = numpy.maximum.reduce(self.points)
     return minXY, maxXY
Example #7
0
 def photo_engine(self):
     print(">>Photo Engine in")
     url_text = mirtools.get_face_cam_url()
     print("Using IP {}".format(url_text))
     i = 0
     while True:
         try:
             response = requests_get(url_text)
             img = Image.open(BytesIO(response.content))
             img = numpy_array(img)
             if i % 20 == 0:
                 print("++++++putting img[{}]".format(i))
             # time.sleep(1)
             self.img_for_processing_q.put((img, i), timeout=0.1)
             i += 1
         except queue.Full:
             try:
                 self.img_for_processing_q.get(timeout=0.1)
             except queue.Empty:
                 print("Story time : A recogniser swiped a photo under my nose. F$$k!")
             # traceback.print_exc()
             # print("-------img_Q_FULL")
             if i > 1000:
                 i = 0
         except:
             print("$$$$$ is cam on? 'll wait for 3 secs. btw ip is [{}]".format(url_text))
             time.sleep(3)
Example #8
0
def sample_size_f(a, axis=None, masked=False):
    """Return the sample size.

    :Parameters:

        axis: `int`, optional
            Axis along which to operate. By default, flattened input
            is used.

    :Returns:

        `numpy.ndarray`

    """
    if masked:
        N = numpy_sum(~a.mask, axis=axis, dtype=float)
        if not numpy_ndim(N):
            N = numpy_asanyarray(N)
    else:
        if axis is None:
            N = numpy_array(a.size, dtype=float)
        else:
            shape = a.shape
            N = numpy_empty(shape[:axis] + shape[axis + 1 :], dtype=float)
            N[...] = shape[axis]
    # --- End: if

    return asanyarray(N)
Example #9
0
def st2rt(array, units_in, units_out, dummy1=None):
    '''The returned array is always independent.

    :Parameters:

        array: numpy array-like of ISO 8601 date-time strings

        units_in: `Units` or `None`

        units_out: `Units`

        dummy1:
            Ignored.

    :Returns:

        `numpy.ndarray`
            An array of floats with the same shape as *array*.

    '''
    array = st2dt(array, units_in)

    ndim = numpy_ndim(array)

    if not ndim and isinstance(array, numpy_ndarray):
        # This necessary because date2num gets upset if you pass
        # it a scalar numpy array
        array = array.item()

    array = units_out._utime.date2num(array)

    if not ndim:
        array = numpy_array(array)

    return array
Example #10
0
def take_profile_photo(id_photo, reload_face_sig_q):
    url = get_face_cam_url()
    dropbox = dir_path + "/faces/"
    unique_name = str(id_photo) + ".jpg"
    photo_path = dropbox + unique_name
    print(photo_path + url)

    if os.path.isfile(photo_path):
        return

    while True:
        response = requests_get(url)
        img = Image.open(BytesIO(response.content))
        img = numpy_array(img)
        face_encodings = face_recognition.face_encodings(img)
        print(face_encodings)
        if len(face_encodings) > 0:
            data = img
            # Rescale to 0-255 and convert to uint8
            rescaled = (255.0 / data.max() * (data - data.min())).astype(np.uint8)

            im = Image.fromarray(rescaled)
            im.save(photo_path)
            break
    print("Took a photo with a face " + photo_path + "Reloading...faces..")
    reload_face_sig_q.put(1)
    reload_face_sig_q.put(1)
    return photo_path
Example #11
0
def rt2dt(array, units_in, units_out=None, dummy1=None):
    '''Convert reference times  to date-time objects

    The returned array is always independent.

    :Parameters:

        array: numpy array-like

        units_in: `Units`

        units_out: *optional*
            Ignored.

        dummy1:
            Ignored.

    :Returns:

        `numpy.ndarray`
            An array of `cf.Datetime` objects with the same shape as
            *array*.

    '''
    mask = None
    if numpy_ma_isMA(array):
        # num2date has issues if the mask is nomask
        mask = array.mask
        if mask is numpy_ma_nomask or not numpy_ma_is_masked(array):
            array = array.view(numpy_ndarray)

    units = units_in.units
    calendar = getattr(units_in, 'calendar', 'standard')

    array = cftime.num2date(array, units, calendar,
                            only_use_cftime_datetimes=True)
#    array = units_in._utime.num2date(array)

    if mask is not None:
        array = numpy_ma_masked_where(mask, array)

    ndim = numpy_ndim(array)

    if mask is None:
        # There is no missing data
        return numpy_array(array, dtype=object)
        # return numpy_vectorize(
        #     partial(dt2Dt, calendar=units_in._calendar),
        #     otypes=[object])(array)
    else:
        # There is missing data
        if not ndim:
            return numpy_ma_masked_all((), dtype=object)
        else:
            # array = numpy_array(array)
            # array = numpy_vectorize(
            #     partial(dt2Dt, calendar=units_in._calendar),
            #     otypes=[object])(array)
            return numpy_ma_masked_where(mask, array)
Example #12
0
def pmin(x, y):
    '''TODO

    :Parameters:

        x: `numpy.ndarray`
           May be updated in place and should not be used again.

        y: `numpy.ndarray`
           Will not be updated in place.

    :Returns:

        out: `numpy.ndarray`

    '''
    if numpy_ma_isMA(x):
        if numpy_ma_isMA(y):
            # x and y are both masked
            z = numpy_minimum(x, y)
            z = numpy_ma_where(x.mask & ~y.mask, y, z)
            x = numpy_ma_where(y.mask & ~x.mask, x, z)
            if x.mask is numpy_ma_nomask:
                x = numpy_array(x)
        else:
            # Only x is masked
            z = numpy_minimum(x, y)
            x = numpy_ma_where(x.mask, y, z)
            if x.mask is numpy_ma_nomask:
                x = numpy_array(x)
    elif numpy_ma_isMA(y):
        # Only y is masked
        z = numpy_minimum(x, y)
        x = numpy_ma_where(y.mask, x, z)
        if x.mask is numpy_ma_nomask:
            x = numpy_array(x)
    else:
        # x and y are both unmasked
        if not numpy_ndim(x):
            # Make sure that we have a numpy array (as opposed to,
            # e.g. a numpy.float64)
            x = numpy_asanyarray(x)

        numpy_minimum(x, y, out=x)

    return x
Example #13
0
def pmax(x, y):
    """The element-wise maximum of two arrays.

    :Parameters:

        x: array-like
           May be updated in place and should not be used again.

        y: array-like
           Will not be updated in place.

    :Returns:

        `numpy.ndarray`

    """
    if numpy_ma_isMA(x):
        if numpy_ma_isMA(y):
            # x and y are both masked
            z = numpy_maximum(x, y)
            z = numpy_ma_where(x.mask & ~y.mask, y, z)
            x = numpy_ma_where(y.mask & ~x.mask, x, z)
            if x.mask is numpy_ma_nomask:  # not numpy_any(x.mask):
                x = numpy_array(x)
        else:
            # Only x is masked
            z = numpy_maximum(x, y)
            x = numpy_ma_where(x.mask, y, z)
            if x.mask is numpy_ma_nomask:  # not numpy_any(x.mask):
                x = numpy_array(x)
    elif numpy_ma_isMA(y):
        # Only y is masked
        z = numpy_maximum(x, y)
        x = numpy_ma_where(y.mask, x, z)
        if x.mask is numpy_ma_nomask:  # not numpy_any(x.mask):
            x = numpy_array(x)
    else:
        # x and y are both unmasked
        if not numpy_ndim(x):
            # Make sure that we have a numpy array (as opposed to,
            # e.g. a numpy.float64)
            x = numpy_asanyarray(x)

        numpy_maximum(x, y, out=x)

    return x
Example #14
0
    def Draw(self, graphics, xAxis=None, yAxis=None, dc=None):
        """Draw objects in graphics with specified x and y axis.
        graphics- instance of PlotGraphics with list of PolyXXX objects
        xAxis - tuple with (min, max) axis range to view
        yAxis - same as xAxis
        dc - drawing context - doesn't have to be specified.
        If it's not, the offscreen buffer is used
        """

        if dc == None:
            # allows using floats for certain functions
            dc = FloatDCWrapper(wx.BufferedDC(wx.ClientDC(self), self._Buffer))
            dc.Clear()
        dc.DrawBitmap(self.bmp, 0, 0, False)
        dc.BeginDrawing()

        #sizes axis to axis type, create lower left and upper right corners of plot
        if xAxis == None or yAxis == None:
            #One or both axis not specified in Draw
            p1, p2 = graphics.boundingBox()  #min, max points of graphics
            if xAxis == None:
                xAxis = self._axisInterval(self._xSpec, p1[0],
                                           p2[0])  #in user units
            if yAxis == None:
                yAxis = self._axisInterval(self._ySpec, p1[1], p2[1])
            #Adjust bounding box for axis spec
            p1[0], p1[1] = xAxis[0], yAxis[
                0]  #lower left corner user scale (xmin,ymin)
            p2[0], p2[1] = xAxis[1], yAxis[
                1]  #upper right corner user scale (xmax,ymax)
        else:
            #Both axis specified in Draw
            p1 = numpy_array([xAxis[0], yAxis[0]
                              ])  #lower left corner user scale (xmin,ymin)
            p2 = numpy_array([xAxis[1], yAxis[1]
                              ])  #upper right corner user scale (xmax,ymax)

        #allow for scaling and shifting plotted points
        scale = (self.plotbox_size) / (p2 - p1) * numpy_array((1, -1))
        shift = -p1 * scale + self.plotbox_origin

        graphics.scaleAndShift(scale, shift)
        graphics.draw(dc)
        dc.EndDrawing()
Example #15
0
 def __init__(self, points, attr):
     self.points = numpy_array(points)
     self.currentScale= (1,1)
     #self.currentShift= (0,0)
     self.scaled = self.points
     self.attributes = {}
     self.attributes.update(self._attributes)
     for name, value in attr.items():
         if name not in self._attributes.keys():
             raise KeyError, "Style attribute incorrect. Should be one of %s" %self._attributes.keys()
         self.attributes[name] = value
Example #16
0
    def add_array(self, name, values, array):
        """Add a new array to the DAF file.

        The summary will be initialized with the `name` and `values`,
        and will have its start word and end word fields set to point to
        where the `array` of floats has been appended to the file.

        """
        f = self.file
        scs = self.summary_control_struct

        record_number = self.bward
        data = bytearray(self.read_record(record_number))
        next_record, previous_record, n_summaries = scs.unpack(data[:24])

        if n_summaries < self.summaries_per_record:
            summary_record = record_number
            name_record = summary_record + 1
            data[:24] = scs.pack(next_record, previous_record, n_summaries + 1)
            self.write_record(summary_record, data)
        else:
            summary_record = ((self.free - 1) * 8 + 1023) // 1024 + 1
            name_record = summary_record + 1
            free_record = summary_record + 2

            n_summaries = 0
            data[:24] = scs.pack(summary_record, previous_record, n_summaries)
            self.write_record(record_number, data)

            summaries = scs.pack(0, record_number, 1).ljust(1024, b'\0')
            names = b'\0' * 1024
            self.write_record(summary_record, summaries)
            self.write_record(name_record, names)

            self.bward = summary_record
            self.free = (free_record - 1) * 1024 // 8 + 1

        start_word = self.free
        f.seek((start_word - 1) * 8)
        array = numpy_array(array)  # TODO: force correct endian
        f.write(array.view())
        end_word = f.tell() // 8

        self.free = end_word + 1
        self.write_file_record()

        values = values[:self.nd + self.ni - 2] + (start_word, end_word)

        base = 1024 * (summary_record - 1)
        offset = int(n_summaries) * self.summary_step
        f.seek(base + scs.size + offset)
        f.write(self.summary_struct.pack(*values))
        f.seek(base + 1024 + offset)
        f.write(name[:self.summary_length].ljust(self.summary_step, b' '))
Example #17
0
    def add_array(self, name, values, array):
        """Add a new array to the DAF file.

        The summary will be initialized with the `name` and `values`,
        and will have its start word and end word fields set to point to
        where the `array` of floats has been appended to the file.

        """
        f = self.file
        scs = self.summary_control_struct

        record_number = self.bward
        data = bytearray(self.read_record(record_number))
        next_record, previous_record, n_summaries = scs.unpack(data[:24])

        if n_summaries < self.summaries_per_record:
            summary_record = record_number
            name_record = summary_record + 1
            data[:24] = scs.pack(next_record, previous_record, n_summaries + 1)
            self.write_record(summary_record, data)
        else:
            summary_record = ((self.free - 1) * 8 + 1023) // 1024 + 1
            name_record = summary_record + 1
            free_record = summary_record + 2

            n_summaries = 0
            data[:24] = scs.pack(summary_record, previous_record, n_summaries)
            self.write_record(record_number, data)

            summaries = scs.pack(0, record_number, 1).ljust(1024, b'\0')
            names = b'\0' * 1024
            self.write_record(summary_record, summaries)
            self.write_record(name_record, names)

            self.bward = summary_record
            self.free = (free_record - 1) * 1024 // 8 + 1

        start_word = self.free
        f.seek((start_word - 1) * 8)
        array = numpy_array(array)  # TODO: force correct endian
        f.write(array.view())
        end_word = f.tell() // 8

        self.free = end_word + 1
        self.write_file_record()

        values = values[:self.nd + self.ni - 2] + (start_word, end_word)

        base = 1024 * (summary_record - 1)
        offset = int(n_summaries) * self.summary_step
        f.seek(base + scs.size + offset)
        f.write(self.summary_struct.pack(*values))
        f.seek(base + 1024 + offset)
        f.write(name[:self.summary_length].ljust(self.summary_step, b' '))
Example #18
0
    def read_array(self, name=None, rtype=str, dim=None, shape=None):
        """
        An array is represented as a specification on one line and the array contents on subsequent
        lines.  The specification consists of the name of the array, the token 'Array', the
        dimension of the array, and the sizes of the dimensions.  Each subsequent line must contain
        as many elements as the size of the last dimension, and there must be enough lines to
        complete the array.  Note: this layout is consistent with how Numpy prints arrays.  vtype is
        used to construct and array of the correct type.  If name is not none, it will be checked
        against the name of the array.  If dim is not None, it will be checked against the specified
        dimension of the array.  If shape is not None, it must be a tuple of ints and will be
        checked against the specified shape of the array.
        
        >>> src = (OnyxTextReader._make_header_tokens('test', '0'), ('name0', 'Array', 2, 3, 4),(0,1,4,9),(1,2,3,4),(10,20,30,40))
        >>> ctr0 = OnyxTextReader(src)
        >>> ctr0.read_array('name0', int, 2, (3,4))
        ('name0', array([[ 0,  1,  4,  9],
               [ 1,  2,  3,  4],
               [10, 20, 30, 40]]))
        """
        tokens = self._next_line()
        self._verify_token_count_min(4, tokens, "array specification line")
        rname = tokens[0]
        array_token = tokens[1]
        spec_dim = int(tokens[2])
        spec_shape = tuple([int(t) for t in tokens[3:]])
        self._verify_thing(self._ARRAY_TOKEN, array_token, 'token')
        self._verify_thing(name, rname, 'name')
        self._verify_thing(dim, spec_dim, 'array dimension')
        self._verify_thing(shape, spec_shape, 'array shape')

        # Even if we didn't get expected values, we can verify a few things in the spec.
        if spec_dim < 1:
            self.raise_parsing_error("found array dimension %d - it must be at least 1" % (d,))
        if len(spec_shape) != spec_dim:
            self.raise_parsing_error("array dimension is %d but shape is %s" % (spec_dim, spec_shape))
        n_items_per_line = spec_shape[-1]
        if spec_dim == 1:
            n_lines = 1
        else:
            n_lines = reduce(lambda x,y: x*y, shape[:-1])
        values = []

        if rtype == float:
            unformatter = readable_string_to_float
        else:
            unformatter = lambda x: x
        for i in xrange(n_lines):
            tokens = self._next_line()
            self._verify_token_count(n_items_per_line, tokens, "array values line")
            values += [rtype(unformatter(t)) for t in tokens]
        result = numpy_array(values, dtype=rtype).reshape(shape)
        return rname, result
Example #19
0
    def Draw(self, graphics, xAxis = None, yAxis = None, dc = None):
        """Draw objects in graphics with specified x and y axis.
        graphics- instance of PlotGraphics with list of PolyXXX objects
        xAxis - tuple with (min, max) axis range to view
        yAxis - same as xAxis
        dc - drawing context - doesn't have to be specified.
        If it's not, the offscreen buffer is used
        """

        if dc == None:
            # allows using floats for certain functions
            dc = FloatDCWrapper(wx.BufferedDC(wx.ClientDC(self), self._Buffer))
            dc.Clear()            
        dc.DrawBitmap(self.bmp, 0, 0, False) 
        dc.BeginDrawing()

        #sizes axis to axis type, create lower left and upper right corners of plot
        if xAxis == None or yAxis == None:
            #One or both axis not specified in Draw
            p1, p2 = graphics.boundingBox()     #min, max points of graphics
            if xAxis == None:
                xAxis = self._axisInterval(self._xSpec, p1[0], p2[0]) #in user units
            if yAxis == None:
                yAxis = self._axisInterval(self._ySpec, p1[1], p2[1])
            #Adjust bounding box for axis spec
            p1[0],p1[1] = xAxis[0], yAxis[0]     #lower left corner user scale (xmin,ymin)
            p2[0],p2[1] = xAxis[1], yAxis[1]     #upper right corner user scale (xmax,ymax)
        else:
            #Both axis specified in Draw
            p1= numpy_array([xAxis[0], yAxis[0]])    #lower left corner user scale (xmin,ymin)
            p2= numpy_array([xAxis[1], yAxis[1]])     #upper right corner user scale (xmax,ymax)

        #allow for scaling and shifting plotted points
        scale = (self.plotbox_size) / (p2-p1)* numpy_array((1,-1))
        shift = -p1*scale + self.plotbox_origin 

        graphics.scaleAndShift(scale, shift)
        graphics.draw(dc)
        dc.EndDrawing()
def pixels3d(surface):
    """pygame.numpyarray.pixels3d(Surface): return array

    reference pixels into a 3d array

    Create a new 3D array that directly references the pixel values in a
    Surface. Any changes to the array will affect the pixels in the
    Surface. This is a fast operation since no data is copied.

    This will only work on Surfaces that have 24-bit or 32-bit
    formats. Lower pixel formats cannot be referenced.

    The Surface this references will remain locked for the lifetime of
    the array (see the Surface.lock - lock the Surface memory for pixel
    access method).
    """
    return numpy_array(surface.get_view("3"), copy=False)
Example #21
0
def pixels3d(surface):
    """pygame.numpyarray.pixels3d(Surface): return array

    reference pixels into a 3d array

    Create a new 3D array that directly references the pixel values in a
    Surface. Any changes to the array will affect the pixels in the
    Surface. This is a fast operation since no data is copied.

    This will only work on Surfaces that have 24-bit or 32-bit
    formats. Lower pixel formats cannot be referenced.

    The Surface this references will remain locked for the lifetime of
    the array (see the Surface.lock - lock the Surface memory for pixel
    access method).
    """
    return numpy_array(surface.get_view("3"), copy=False)
Example #22
0
def pixels2d(surface):
    """pygame.numpyarray.pixels2d(Surface): return array

    reference pixels into a 2d array
    
    Create a new 2D array that directly references the pixel values in a
    Surface. Any changes to the array will affect the pixels in the
    Surface. This is a fast operation since no data is copied.

    Pixels from a 24-bit Surface cannot be referenced, but all other
    Surface bit depths can.

    The Surface this references will remain locked for the lifetime of
    the array (see the Surface.lock - lock the Surface memory for pixel
    access method).
    """
    try:
        return numpy_array(surface.get_view('2'), copy=False)
    except (ValueError, TypeError):
        raise ValueError("bit depth %i unsupported for 2D reference array" %
                         (surface.get_bitsize(),))
    def test_eigenvalues_eigenvectors(self):
        ls_entries = [[16, 14, 11, 18, 11], [15, 14, 8, 15, 10],
                      [15, 10, 4, 7, 15], [7, 13, 9, 19, 9], [12, 4, 19, 8, 9]]

        from numpy.linalg import eig as numpy_eig
        from numpy import array as numpy_array, allclose

        A = SquareMatrix(ls_entries)
        A_numpy = numpy_array(ls_entries)

        eigs, eig_vects = A.eigenvalues_eigenvectors()
        np_eigs, np_eig_vects = numpy_eig(A_numpy)

        # compare the transposes so that can use numpy allclose method
        eig_vects = sorted(eig_vects.transpose().ls_entries,
                           key=lambda x: x[0])
        np_eig_vects = sorted(np_eig_vects.T, key=lambda x: x[0])

        # Just need to test for the eigenvectors, if those match then the eigenvalues would've also been the same
        # Tests to the default tolerances of rtol=1e-05, atol=1e-08
        self.assertTrue(allclose(eig_vects, np_eig_vects))
Example #24
0
def pixels2d(surface):
    """pygame.numpyarray.pixels2d(Surface): return array

    reference pixels into a 2d array
    
    Create a new 2D array that directly references the pixel values in a
    Surface. Any changes to the array will affect the pixels in the
    Surface. This is a fast operation since no data is copied.

    Pixels from a 24-bit Surface cannot be referenced, but all other
    Surface bit depths can.

    The Surface this references will remain locked for the lifetime of
    the array (see the Surface.lock - lock the Surface memory for pixel
    access method).
    """
    try:
        return numpy_array(surface.get_view('2'), copy=False)
    except ValueError:
        raise ValueError("bit depth %i unsupported for 2D reference array" %
                         (surface.get_bitsize(), ))
Example #25
0
def sample_size_f(a, axis=None, masked=False):
    '''TODO

    :Parameters:

        axis: `int`, optional
            non-negative


    '''
    if masked:
        N = numpy_sum(~a.mask, axis=axis, dtype=float)
        if not numpy_ndim(N):
            N = numpy_asanyarray(N)
    else:
        if axis is None:
            N = numpy_array(a.size, dtype=float)
        else:
            shape = a.shape
            N = numpy_empty(shape[:axis] + shape[axis + 1:], dtype=float)
            N[...] = shape[axis]
    # --- End: if

    return asanyarray(N)
Example #26
0
def _get_numpy_as_image(array):
    array = array.copy()  # prevent original array from modifying

    data_range_warnings = []
    array_min = array.min()
    array_max = array.max()
    if array_min < 0:
        data_range_warnings.append(
            f"the smallest value in the array is {array_min}")
    if array_max > 1:
        data_range_warnings.append(
            f"the largest value in the array is {array_max}")
    if data_range_warnings:
        data_range_warning_message = (" and ".join(data_range_warnings) +
                                      ". ").capitalize()
        click.echo(
            f"{data_range_warning_message}"
            f"To be interpreted as colors correctly values in the array need to be in the [0, 1] range.",
            err=True,
        )
    array *= 255
    shape = array.shape
    if len(shape) == 2:
        return _get_pil_image_data(
            pilimage_fromarray(array.astype(numpy_uint8)))
    if len(shape) == 3:
        if shape[2] == 1:
            array2d = numpy_array([[col[0] for col in row] for row in array])
            return _get_pil_image_data(
                pilimage_fromarray(array2d.astype(numpy_uint8)))
        if shape[2] in (3, 4):
            return _get_pil_image_data(
                pilimage_fromarray(array.astype(numpy_uint8)))
    raise ValueError(
        "Incorrect size of numpy.ndarray. Should be 2-dimensional or"
        "3-dimensional with 3rd dimension of size 1, 3 or 4.")
Example #27
0
def img2np(img='data.bmp'):
    bmp = Image.open(img)
    return numpy_array(bmp)
Example #28
0
def test_units(filename):
    """
    Tests that these fields have the same units within SWIFTsimIO as they
    do in the SWIFT code itself.
    """

    data = load(filename)

    shared = ["coordinates", "masses", "particle_ids", "velocities"]

    baryon = [
        "maximal_temperatures",
        "maximal_temperature_scale_factors",
        "iron_mass_fractions_from_snia",
        "metal_mass_fractions_from_agb",
        "metal_mass_fractions_from_snii",
        "metal_mass_fractions_from_snia",
        "smoothed_element_mass_fractions",
        "smoothed_iron_mass_fractions_from_snia",
        "smoothed_metal_mass_fractions",
    ]

    to_test = {
        "gas": [
            "densities",
            "entropies",
            "internal_energies",
            "smoothing_lengths",
            "pressures",
            "temperatures",
        ] + baryon + shared,
        "dark_matter":
        shared,
    }

    for ptype, properties in to_test.items():
        field = getattr(data, ptype)

        # Now need to extract the particle paths in the original hdf5 file
        # for comparison...
        paths = numpy_array(field.particle_metadata.field_paths)
        names = numpy_array(field.particle_metadata.field_names)

        for property in properties:
            # Read the 0th element, and compare in CGS units.
            # We need to use doubles here as sometimes we can overflow!
            our_units = getattr(field, property).astype(float64)[0]

            our_units.convert_to_cgs()

            # Find the path in the HDF5 for our linked dataset
            path = paths[names == property][0]

            with h5py.File(filename, "r") as handle:
                swift_units = handle[path].attrs[
                    "Conversion factor to CGS (not including cosmological corrections)"][
                        0]
                swift_value = swift_units * handle[path][0]

            assert isclose(swift_value, our_units.value, 5e-5).all()

    # If we didn't crash out, we gucci.
    return
Example #29
0
    def __init__(
        self,
        srcfield,
        dstfield,
        srcfracfield,
        dstfracfield,
        method="conservative_1st",
        ignore_degenerate=False,
    ):
        """Creates a handle for regridding fields from a source grid to
        a destination grid that can then be used by the run_regridding
        method.

        :Parameters:

            srcfield: ESMF.Field
                The source field with an associated grid to be used for
                regridding.

            dstfield: ESMF.Field
                The destination field with an associated grid to be used
                for regridding.

            srcfracfield: ESMF.Field
                A field to hold the fraction of the source field that
                contributes to conservative regridding.

            dstfracfield: ESMF.Field
                A field to hold the fraction of the source field that
                contributes to conservative regridding.

            method: `str`, optional
                By default the regridding method is set to
                'conservative_1st'. In this case or if it is set to
                'conservative' first-order conservative regridding is
                used. If it is set to 'conservative_2nd' second order
                conservative regridding is used. If it is set to
                'linear' then (multi)linear interpolation is used.  If
                it is set to 'patch' then higher-order patch recovery is
                used.  If it is set to 'nearest_stod' then nearest source
                to destination interpolation is used. If it is set to
                'nearest_dtos' then nearest destination to source
                interpolation is used.

            ignore_degenerate: `bool`, optional
                Whether to check for degenerate points.

        """
        # create a handle to the regridding method
        regrid_method_map = {
            "linear": ESMF.RegridMethod.BILINEAR,  # see comment below...
            "bilinear": ESMF.RegridMethod.BILINEAR,  # (for back compat)
            "conservative": ESMF.RegridMethod.CONSERVE,
            "conservative_1st": ESMF.RegridMethod.CONSERVE,
            "conservative_2nd": ESMF.RegridMethod.CONSERVE_2ND,
            "nearest_dtos": ESMF.RegridMethod.NEAREST_DTOS,
            "nearest_stod": ESMF.RegridMethod.NEAREST_STOD,
            "patch": ESMF.RegridMethod.PATCH,
        }
        # ... diverge from ESMF with respect to name for bilinear method by
        # using 'linear' because 'bi' implies 2D linear interpolation, which
        # could mislead or confuse for Cartesian regridding in 1D or 3D.
        regrid_method = regrid_method_map.get(
            method, ValueError("Regrid method not recognised.")
        )
        # Initialise the regridder. This also creates the
        # weights needed for the regridding.
        self.regridSrc2Dst = ESMF.Regrid(
            srcfield,
            dstfield,
            regrid_method=regrid_method,
            src_mask_values=numpy_array([0], dtype="int32"),
            dst_mask_values=numpy_array([0], dtype="int32"),
            src_frac_field=srcfracfield,
            dst_frac_field=dstfracfield,
            unmapped_action=ESMF.UnmappedAction.IGNORE,
            ignore_degenerate=ignore_degenerate,
        )
Example #30
0
    def create_grid(
        coords,
        use_bounds,
        mask=None,
        cartesian=False,
        cyclic=False,
        coords_2D=False,
        coord_order=None,
    ):
        """Create an ESMPy grid given a sequence of coordinates for use
        as a source or destination grid in regridding. Optionally the
        grid may have an associated mask.

        :Parameters:

            coords: sequence
                The coordinates if not Cartesian it is assume that the
                first is longitude and the second is latitude.

            use_bounds: `bool`
                Whether to populate the grid corners with information from
                the bounds or not.

            mask: `numpy.ndarray`, optional

                An optional numpy array of booleans containing the grid
                points to mask.  Where the elements of mask are True the
                output grid is masked.

            cartesian: `bool`, optional
                Whether to create a Cartesian grid or a spherical one,
                False by default.

            cyclic: `bool`, optional
                Whether or not the longitude (if present) is cyclic. If
                None the a check for cyclicity is made from the
                bounds. None by default.

            coords_2D: `bool`, optional
                Whether the coordinates are 2D or not. Presently only
                works for spherical coordinates. False by default.

            coord_order: sequence, optional
                Two tuples one indicating the order of the x and y axes
                for 2D longitude, one for 2D latitude.

        :Returns:

            `ESMF.Grid`
                The resulting ESMPy grid for use as a source or destination
                grid in regridding.

        """

        if not cartesian:
            lon = coords[0]
            lat = coords[1]
            if not coords_2D:
                # Get the shape of the grid
                shape = [lon.size, lat.size]
            else:
                x_order = coord_order[0]
                y_order = coord_order[1]
                # Get the shape of the grid
                shape = lon.transpose(x_order).shape
                if lat.transpose(y_order).shape != shape:
                    raise ValueError(
                        "The longitude and latitude coordinates"
                        " must have the same shape."
                    )
            # --- End: if

            if use_bounds:
                if not coords_2D:
                    # Get the bounds
                    x_bounds = lon.get_bounds()
                    y_bounds = lat.get_bounds().clip(-90, 90, "degrees").array

                    # If cyclic not set already, check for cyclicity
                    if cyclic is None:
                        cyclic = abs(
                            x_bounds.datum(-1) - x_bounds.datum(0)
                        ) == Data(360, "degrees")

                    x_bounds = x_bounds.array
                else:
                    # Get the bounds
                    x_bounds = lon.get_bounds()
                    y_bounds = lat.get_bounds().clip(-90, 90, "degrees")
                    n = x_bounds.shape[0]
                    m = x_bounds.shape[1]
                    x_bounds = x_bounds.array
                    y_bounds = y_bounds.array

                    tmp_x = numpy_empty((n + 1, m + 1))
                    tmp_x[:n, :m] = x_bounds[:, :, 0]
                    tmp_x[:n, m] = x_bounds[:, -1, 1]
                    tmp_x[n, :m] = x_bounds[-1, :, 3]
                    tmp_x[n, m] = x_bounds[-1, -1, 2]

                    tmp_y = numpy_empty((n + 1, m + 1))
                    tmp_y[:n, :m] = y_bounds[:, :, 0]
                    tmp_y[:n, m] = y_bounds[:, -1, 1]
                    tmp_y[n, :m] = y_bounds[-1, :, 3]
                    tmp_y[n, m] = y_bounds[-1, -1, 2]

                    x_bounds = tmp_x
                    y_bounds = tmp_y

            else:
                if not coords_2D:
                    # If cyclicity not set already, check for cyclicity
                    if cyclic is None:
                        try:
                            x_bounds = lon.get_bounds()
                            cyclic = abs(
                                x_bounds.datum(-1) - x_bounds.datum(0)
                            ) == Data(360, "degrees")
                        except ValueError:
                            pass
            # --- End: if

            # Create empty grid
            max_index = numpy_array(shape, dtype="int32")
            if use_bounds:
                staggerLocs = [ESMF.StaggerLoc.CORNER, ESMF.StaggerLoc.CENTER]
            else:
                staggerLocs = [ESMF.StaggerLoc.CENTER]

            if cyclic:
                grid = ESMF.Grid(
                    max_index, num_peri_dims=1, staggerloc=staggerLocs
                )
            else:
                grid = ESMF.Grid(max_index, staggerloc=staggerLocs)

            # Populate grid centres
            x, y = 0, 1
            gridXCentre = grid.get_coords(x, staggerloc=ESMF.StaggerLoc.CENTER)
            gridYCentre = grid.get_coords(y, staggerloc=ESMF.StaggerLoc.CENTER)
            if not coords_2D:
                gridXCentre[...] = lon.array.reshape((lon.size, 1))
                gridYCentre[...] = lat.array.reshape((1, lat.size))
            else:
                gridXCentre[...] = lon.transpose(x_order).array
                gridYCentre[...] = lat.transpose(y_order).array

            # Populate grid corners if there are bounds
            if use_bounds:
                gridCorner = grid.coords[ESMF.StaggerLoc.CORNER]
                if not coords_2D:
                    if cyclic:
                        gridCorner[x][...] = x_bounds[:, 0].reshape(
                            lon.size, 1
                        )
                    else:
                        n = x_bounds.shape[0]
                        tmp_x = numpy_empty(n + 1)
                        tmp_x[:n] = x_bounds[:, 0]
                        tmp_x[n] = x_bounds[-1, 1]
                        gridCorner[x][...] = tmp_x.reshape(lon.size + 1, 1)

                    n = y_bounds.shape[0]
                    tmp_y = numpy_empty(n + 1)
                    tmp_y[:n] = y_bounds[:, 0]
                    tmp_y[n] = y_bounds[-1, 1]
                    gridCorner[y][...] = tmp_y.reshape(1, lat.size + 1)
                else:
                    gridCorner = grid.coords[ESMF.StaggerLoc.CORNER]
                    x_bounds = x_bounds.transpose(x_order)
                    y_bounds = y_bounds.transpose(y_order)
                    if cyclic:
                        x_bounds = x_bounds[:-1, :]
                        y_bounds = y_bounds[:-1, :]
                    gridCorner[x][...] = x_bounds
                    gridCorner[y][...] = y_bounds
            # --- End: if
        else:
            # Test the dimensionality of the list of coordinates
            ndim = len(coords)
            if ndim < 1 or ndim > 3:
                raise ValueError(
                    "Cartesian grid must have between 1 and 3 dimensions."
                )

            # For 1D conservative regridding add an extra dimension of size 1
            if ndim == 1:
                if not use_bounds:
                    # For 1D nonconservative regridding the extra dimension
                    # should already have been added in cf.Field.regridc.
                    raise ValueError(
                        "Cannot create a Cartesian grid from "
                        "one dimension coordinate with no bounds."
                    )
                coords = [
                    DimensionCoordinate(
                        data=Data(0),
                        bounds=Data(
                            [
                                numpy_finfo("float32").epsneg,
                                numpy_finfo("float32").eps,
                            ]
                        ),
                    )
                ] + coords
                if mask is not None:
                    mask = mask[None, :]
                ndim = 2

            shape = list()
            for coord in coords:
                shape.append(coord.size)

            # Initialise the grid
            max_index = numpy_array(shape, dtype="int32")
            if use_bounds:
                if ndim < 3:
                    staggerLocs = [
                        ESMF.StaggerLoc.CORNER,
                        ESMF.StaggerLoc.CENTER,
                    ]
                else:
                    staggerLocs = [
                        ESMF.StaggerLoc.CENTER_VCENTER,
                        ESMF.StaggerLoc.CORNER_VFACE,
                    ]
            else:
                if ndim < 3:
                    staggerLocs = [ESMF.StaggerLoc.CENTER]
                else:
                    staggerLocs = [ESMF.StaggerLoc.CENTER_VCENTER]
            # --- End: if
            grid = ESMF.Grid(
                max_index, coord_sys=ESMF.CoordSys.CART, staggerloc=staggerLocs
            )

            # Populate the grid centres
            for d in range(0, ndim):
                if ndim < 3:
                    gridCentre = grid.get_coords(
                        d, staggerloc=ESMF.StaggerLoc.CENTER
                    )
                else:
                    gridCentre = grid.get_coords(
                        d, staggerloc=ESMF.StaggerLoc.CENTER_VCENTER
                    )
                gridCentre[...] = coords[d].array.reshape(
                    [shape[d] if x == d else 1 for x in range(0, ndim)]
                )
            # --- End: for

            # Populate grid corners
            if use_bounds:
                if ndim < 3:
                    gridCorner = grid.coords[ESMF.StaggerLoc.CORNER]
                else:
                    gridCorner = grid.coords[ESMF.StaggerLoc.CORNER_VFACE]

                for d in range(0, ndim):
                    # boundsD = coords[d].get_bounds(create=True).array
                    boundsD = coords[d].get_bounds(None)
                    if boundsD is None:
                        boundsD = coords[d].create_bounds()

                    boundsD = boundsD.array

                    if shape[d] > 1:
                        tmp = numpy_empty(shape[d] + 1)
                        tmp[0:-1] = boundsD[:, 0]
                        tmp[-1] = boundsD[-1, 1]
                        boundsD = tmp

                    gridCorner[d][...] = boundsD.reshape(
                        [shape[d] + 1 if x == d else 1 for x in range(0, ndim)]
                    )
            # --- End: if
        # --- End: if

        # Add the mask if appropriate
        if mask is not None:
            gmask = grid.add_item(ESMF.GridItem.MASK)
            gmask[...] = 1
            gmask[mask] = 0

        return grid
Example #31
0
def numpy_sample_standard_deviation(X):
    from numpy import std as numpy_std
    from numpy import array as numpy_array
    return numpy_std(numpy_array([X]), ddof=1)
Example #32
0
def dt2rt(array, units_in, units_out, dummy1=None):
    '''Round to the nearest millisecond. This is only necessary whilst
    netCDF4 time functions have an accuracy of no better than 1
    millisecond (which is still the case at version 1.2.2).

    The returned array is always independent.

    :Parameters:

        array: numpy array-like of date-time objects

        units_in:
            Ignored.

        units_out: `Units`

        dummy1:
            Ignored.

    :Returns:

        `numpy.ndarray`
            An array of numbers with the same shape as *array*.

    '''
    ndim = numpy_ndim(array)

    if not ndim and isinstance(array, numpy_ndarray):
        # This necessary because date2num gets upset if you pass
        # it a scalar numpy array
        array = array.item()

#   calendar = getattr(units_out, 'calendar', '')
#   if calendar

#   array = cftime.date2num(
#       array, units=units_out.units,
#       calendar=getattr(units_out, 'calendar', 'standard')
#   )

    array = units_out._utime.date2num(array)

    if not ndim:
        array = numpy_array(array)

    # Round to the nearest millisecond. This is only necessary whilst
    # netCDF4 time functions have an accuracy of no better than 1
    # millisecond (which is still the case at version 1.2.2).
    units = units_out._utime.units
    decimals = 3
    month = False
    year = False

    day = units in cftime.day_units
    if day:
        array *= 86400.0
    else:
        sec = units in cftime.sec_units
        if not sec:
            hr = units in cftime.hr_units
            if hr:
                array *= 3600.0
            else:
                m = units in cftime.min_units
                if m:
                    array *= 60.0
                else:
                    millisec = units in cftime.millisec_units
                    if millisec:
                        decimals = 0
                    else:
                        microsec = units in cftime.microsec_units
                        if microsec:
                            decimals = -3
                        else:
                            month = units in ('month', 'months')
                            if month:
                                array *= (365.242198781 / 12.0) * 86400.0
                            else:
                                year = units in ('year', 'years', 'yr')
                                if year:
                                    array *= 365.242198781 * 86400.0
    # --- End: if
    array = numpy_around(array, decimals, array)

    if day:
        array /= 86400.0
    elif sec:
        pass
    elif hr:
        array /= 3600.0
    elif m:
        array /= 60.0
    elif month:
        array /= (365.242198781 / 12.0) * 86400.0
    elif year:
        array /= 365.242198781 * 86400.0

    if not ndim:
        array = numpy_asanyarray(array)

    return array
Example #33
0
 def GetXY(self,event):
     """Takes a mouse event and returns the XY user axis values."""
     screenPos= numpy_array( event.GetPosition())
     x,y= (screenPos-self._pointShift)/self._pointScale
     return x,y
Example #34
0
 def GetXY(self, event):
     """Takes a mouse event and returns the XY user axis values."""
     screenPos = numpy_array(event.GetPosition())
     x, y = (screenPos - self._pointShift) / self._pointScale
     return x, y
Example #35
0
def img2np(img='data.bmp'):
	bmp = Image.open(img)
	return numpy_array(bmp)