Example #1
0
def round_to_half_integer(input_value):
    """Rounds numbers to nearest half-integer that is not also an integer.

    :param input_value: Either numpy array of real numbers or scalar real
        number.
    :return: output_value: Same as input_value, except rounded.
    """

    if isinstance(input_value, collections.Iterable):
        error_checking.assert_is_real_numpy_array(input_value)
    else:
        error_checking.assert_is_real_number(input_value)

    return numpy.round(input_value + 0.5) - 0.5
Example #2
0
def round_to_nearest(input_value, rounding_base):
    """Rounds numbers to nearest x, where x is a positive real number.

    :param input_value: Either numpy array of real numbers or scalar real
        number.
    :param rounding_base: Numbers will be rounded to this base.
    :return: output_value: Same as input_value, except rounded.
    """

    if isinstance(input_value, collections.Iterable):
        error_checking.assert_is_real_numpy_array(input_value)
    else:
        error_checking.assert_is_real_number(input_value)

    error_checking.assert_is_greater(rounding_base, 0)
    return rounding_base * numpy.round(input_value / rounding_base)
Example #3
0
    def test_assert_is_real_number_integer(self):
        """Checks assert_is_real_number when input is integer."""

        error_checking.assert_is_real_number(SINGLE_INTEGER)
Example #4
0
    def test_assert_is_real_number_nan(self):
        """Checks assert_is_real_number when input is NaN."""

        error_checking.assert_is_real_number(numpy.nan)
Example #5
0
    def test_assert_is_real_number_none(self):
        """Checks assert_is_real_number when input is None."""

        with self.assertRaises(TypeError):
            error_checking.assert_is_real_number(None)
Example #6
0
    def test_assert_is_real_number_complex(self):
        """Checks assert_is_real_number when input is complex."""

        with self.assertRaises(TypeError):
            error_checking.assert_is_real_number(SINGLE_COMPLEX_NUMBER)
Example #7
0
    def test_assert_is_real_number_boolean(self):
        """Checks assert_is_real_number when input is Boolean."""

        with self.assertRaises(TypeError):
            error_checking.assert_is_real_number(SINGLE_BOOLEAN)
Example #8
0
    def test_assert_is_real_number_float(self):
        """Checks assert_is_real_number when input is float."""

        error_checking.assert_is_real_number(SINGLE_FLOAT)
Example #9
0
    def test_assert_is_real_number_too_many_inputs(self):
        """Checks assert_is_real_number when input is array of real numbers."""

        with self.assertRaises(TypeError):
            error_checking.assert_is_real_number(FLOAT_NUMPY_ARRAY)
def plot_colour_bar(axes_object_or_matrix,
                    data_matrix,
                    colour_map_object,
                    colour_norm_object,
                    orientation_string=DEFAULT_CBAR_ORIENTATION_STRING,
                    padding=None,
                    extend_min=True,
                    extend_max=True,
                    fraction_of_axis_length=1.,
                    font_size=FONT_SIZE):
    """Plots colour bar.

    :param axes_object_or_matrix: Either one axis handle (instance of
        `matplotlib.axes._subplots.AxesSubplot`) or a numpy array thereof.
    :param data_matrix: numpy array of values to which the colour map applies.
    :param colour_map_object: Colour map (instance of `matplotlib.pyplot.cm` or
        similar).
    :param colour_norm_object: Colour normalization (maps from data space to
        colour-bar space, which goes from 0...1).  This should be an instance of
        `matplotlib.colors.Normalize`.
    :param orientation_string: Orientation ("vertical" or "horizontal").
    :param padding: Padding between colour bar and main plot (in range 0...1).
        To use the default (there are different defaults for vertical and horiz
        colour bars), leave this alone.
    :param extend_min: Boolean flag.  If True, values below the minimum
        specified by `colour_norm_object` are possible, so the colour bar will
        be plotted with an arrow at the bottom.
    :param extend_max: Boolean flag.  If True, values above the max specified by
        `colour_norm_object` are possible, so the colour bar will be plotted
        with an arrow at the top.
    :param fraction_of_axis_length: The colour bar will take up this fraction of
        the axis length (x-axis if orientation_string = "horizontal", y-axis if
        orientation_string = "vertical").
    :param font_size: Font size for tick marks on colour bar.
    :return: colour_bar_object: Colour-bar handle (instance of
        `matplotlib.pyplot.colorbar`).
    """

    error_checking.assert_is_real_numpy_array(data_matrix)
    error_checking.assert_is_boolean(extend_min)
    error_checking.assert_is_boolean(extend_max)
    error_checking.assert_is_greater(fraction_of_axis_length, 0.)
    # error_checking.assert_is_leq(fraction_of_axis_length, 1.)

    scalar_mappable_object = pyplot.cm.ScalarMappable(cmap=colour_map_object,
                                                      norm=colour_norm_object)
    scalar_mappable_object.set_array(data_matrix)

    if extend_min and extend_max:
        extend_arg = 'both'
    elif extend_min:
        extend_arg = 'min'
    elif extend_max:
        extend_arg = 'max'
    else:
        extend_arg = 'neither'

    if padding is None:
        if orientation_string == 'horizontal':
            padding = HORIZONTAL_CBAR_PADDING
        else:
            padding = VERTICAL_CBAR_PADDING

    # error_checking.assert_is_geq(padding, 0.)
    # error_checking.assert_is_leq(padding, 1.)
    error_checking.assert_is_real_number(padding)

    if isinstance(axes_object_or_matrix, numpy.ndarray):
        axes_arg = axes_object_or_matrix.ravel().tolist()
    else:
        axes_arg = axes_object_or_matrix

    colour_bar_object = pyplot.colorbar(ax=axes_arg,
                                        mappable=scalar_mappable_object,
                                        orientation=orientation_string,
                                        pad=padding,
                                        extend=extend_arg,
                                        shrink=fraction_of_axis_length)

    colour_bar_object.ax.tick_params(labelsize=font_size)

    if orientation_string == 'horizontal':
        colour_bar_object.ax.set_xticklabels(
            colour_bar_object.ax.get_xticklabels(), rotation=90)

    return colour_bar_object