Beispiel #1
0
def round(value, decimal=0, digits=None):
    """
    ROUND TO GIVEN NUMBER OF DIGITS, OR GIVEN NUMBER OF DECIMAL PLACES
    decimal - NUMBER OF DIGITS AFTER DECIMAL POINT (NEGATIVE IS VALID)
    digits - NUMBER OF SIGNIFICANT DIGITS (LESS THAN 1 IS INVALID)
    """
    if value == None:
        return None
    elif value == 0:
        return 0
    else:
        value = float(value)

    if digits != None:
        try:
            if digits <= 0:
                return sign(value) * pow(10, round(math_log10(abs(value)), 0))
            m = pow(10, math_ceil(math_log10(abs(value))))
            return _round(value / m, 0) * m
        except Exception as e:
            from mo_logs import Log

            Log.error("not expected", e)
    elif decimal <= 0:
        return int(_round(value, decimal))
    else:
        return _round(value, decimal)
Beispiel #2
0
def sigfig(number, uncert):
    '''
    returns number of significant figures of a quantity
    :param number: the value of the quantity
    :param uncert: the uncertainty of the quantity
    :return: integer giving significant figures (100 = exact number)
    >>> sigfig(4, 0.0)
    100
    >>> sigfig(12.56, 0.04)
    4
    >>> sigfig(5.6, 0.09999999999999999)
    2
    >>> sigfig(0.0, 0.14142135623730953)
    0
    '''
    if not uncert:
        return 100
    if not number:
        return 0
    most = int(floor(math_log10(abs(number))))
    sig = int(floor(math_log10(uncert * 1.05)))
    sigfig = most - sig + 1
    if sigfig > 0:
        return sigfig
    return 1
Beispiel #3
0
def sigfig(number, uncert):
    '''
    returns number of significant figures of a quantity
    :param number: the value of the quantity
    :param uncert: the uncertainty of the quantity
    :return: integer giving significant figures (100 = exact number)
    >>> sigfig(4, 0.0)
    100
    >>> sigfig(12.56, 0.04)
    4
    >>> sigfig(5.6, 0.09999999999999999)
    2
    >>> sigfig(0.0, 0.14142135623730953)
    0
    '''
    if not uncert:
        return 100
    if not number:
        return 0
    most = int(floor(math_log10(abs(number))))
    sig = int(floor(math_log10(uncert * 1.05)))
    sigfig = most - sig + 1
    if sigfig > 0:
        return sigfig
    return 1
Beispiel #4
0
def almost_equal(first, second, digits=None, places=None, delta=None):
    try:
        if first == second:
            return True

        if delta is not None:
            if abs(first - second) <= delta:
                return True
        else:
            places = coalesce(places, digits, 18)
            diff = math_log10(abs(first - second))
            if diff < ceiling(math_log10(first)) - places:
                return True

        return False
    except Exception as e:
        from mo_logs import Log
        Log.error("problem comparing", cause=e)
Beispiel #5
0
 def setDecimals(selforcls, newDecimals):
     if newDecimals is not None:
         testfor(
             isNumber(newDecimals) and newDecimals >= 0, DecimalsError,
             "Parameter decimals has to be a positive number!")
     else:
         start, end = selforcls._valueRange
         newDecimals = round(math_log10(math_fabs(end - start)))
     newDecimals = max(newDecimals, 0)
     newDecimals = min(newDecimals, sys.float_info.max_10_exp)
     selforcls._decimals = int(newDecimals)
Beispiel #6
0
def log10(x: float) -> float:
    """
    Version of :func:`math.log10` that treats log(0) as ``-inf``, rather than
    crashing with ``ValueError: math domain error``.

    Args:
        x: parameter

    Returns:
        float: log10(x), the logarithm to base 10 of x

    See
    https://stackoverflow.com/questions/42980201/logarithm-of-zero-in-python.
    """
    return math_log10(x) if x != 0 else MINUS_INFINITY
Beispiel #7
0
    def word_segmentation(self,
                          phrase,
                          max_edit_distance=None,
                          max_segmentation_word_length=None,
                          ignore_token=None):
        """`word_segmentation` divides a string into words by inserting
        missing spaces at the appropriate positions misspelled words
        are corrected and do not affect segmentation existing spaces
        are allowed and considered for optimum segmentation

        `word_segmentation` uses a novel approach *without* recursion.
        https://medium.com/@wolfgarbe/fast-word-segmentation-for-noisy-text-2c2c41f9e8da
        While each string of length n can be segmented in 2^n−1
        possible compositions
        https://en.wikipedia.org/wiki/Composition_(combinatorics)
        `word_segmentation` has a linear runtime O(n) to find the optimum
        composition

        Find suggested spellings for a multi-word input string
        (supports word splitting/merging).

        **Args**:

        * phrase (str): The string being spell checked.
        * max_segmentation_word_length (int): The maximum word length\
            that should be considered.
        * max_edit_distance (int): The maximum edit distance between\
            input and corrected words (0=no correction/segmentation\
            only).
        * ignore_token (regex pattern): A regex pattern describing\
            what words/phrases to ignore and leave unchanged

        **Returns**:
        The word segmented string, the word segmented and spelling\
            corrected string, the edit distance sum between input\
            string and corrected string, the sum of word occurence\
            probabilities in log scale (a measure of how common and\
            probable the corrected segmentation is).
        """
        # number of all words in the corpus used to generate the
        # frequency dictionary. This is used to calculate the word
        # occurrence probability p from word counts c : p=c/N. N equals
        # the sum of all counts c in the dictionary only if the
        # dictionary is complete, but not if the dictionary is
        # truncated or filtered
        N = 1024908267229
        if max_edit_distance is None:
            max_edit_distance = self._max_dictionary_edit_distance
        if max_segmentation_word_length is None:
            max_segmentation_word_length = self._max_length
        array_size = min(max_segmentation_word_length, len(phrase))
        compositions = [Composition()] * array_size
        circular_index = cycle(range(array_size))
        idx = -1

        # outer loop (column): all possible part start positions
        for j in range(len(phrase)):
            # inner loop (row): all possible part lengths (from start
            # position): part can't be bigger than longest word in
            # dictionary (other than long unknown word)
            imax = min(len(phrase) - j, max_segmentation_word_length)
            for i in range(1, imax + 1):
                # get top spelling correction/ed for part
                part = phrase[j:j + i]
                separator_len = 0
                top_ed = 0
                top_log_prob = 0.0
                top_result = ""

                if part[0].isspace():
                    # remove space for levensthein calculation
                    part = part[1:]
                else:
                    # add ed+1: space did not exist, had to be inserted
                    separator_len = 1

                # remove space from part1, add number of removed spaces
                # to top_ed
                top_ed += len(part)
                # remove space.
                # add number of removed spaces to ed
                part = part.replace(" ", "")
                top_ed -= len(part)

                results = self.lookup(part,
                                      Verbosity.TOP,
                                      max_edit_distance,
                                      ignore_token=ignore_token)
                if results:
                    top_result = results[0].term
                    top_ed += results[0].distance
                    # Naive Bayes Rule. We assume the word
                    # probabilities of two words to be independent.
                    # Therefore the resulting probability of the word
                    # combination is the product of the two word
                    # probabilities. Instead of computing the product
                    # of probabilities we are computing the sum of the
                    # logarithm of probabilities because the
                    # probabilities of words are about 10^-10, the
                    # product of many such small numbers could exceed
                    # (underflow) the floating number range and become
                    # zero. log(ab)=log(a)+log(b)
                    top_log_prob = math_log10(
                        float(results[0].count) / float(N))
                else:
                    top_result = part
                    # default, if word not found. otherwise long input
                    # text would win as long unknown word (with
                    # ed=edmax+1), although there there should many
                    # spaces inserted
                    top_ed += len(part)
                    top_log_prob = math_log10(10.0 / N /
                                              math_pow(10.0, len(part)))

                dest = (i + idx) % array_size
                # set values in first loop
                if j == 0:
                    compositions[dest] = Composition(part, top_result, top_ed,
                                                     top_log_prob)
                # pylint: disable=C0301,R0916
                elif (i == max_segmentation_word_length
                      # replace values if better log_prob_sum, if same
                      # edit distance OR one space difference
                      or ((compositions[idx].distance_sum + top_ed
                           == compositions[dest].distance_sum
                           or compositions[idx].distance_sum + separator_len +
                           top_ed == compositions[dest].distance_sum)
                          and compositions[dest].log_prob_sum <
                          compositions[idx].log_prob_sum + top_log_prob)
                      # replace values if smaller edit distance
                      or compositions[idx].distance_sum + separator_len +
                      top_ed < compositions[dest].distance_sum):
                    compositions[dest] = Composition(
                        compositions[idx].segmented_string + " " + part,
                        compositions[idx].corrected_string + " " + top_result,
                        compositions[idx].distance_sum + separator_len +
                        top_ed, compositions[idx].log_prob_sum + top_log_prob)
            idx = next(circular_index)
        return compositions[idx]
Beispiel #8
0
def qlog(a):
    return Q(math_log10(a.number), "log(%s)", a.units, abs(a.uncert / a.number), a.prefu, (a,))
Beispiel #9
0
def mostsig(number, ope=1.00000000001): return int(floor(math_log10(abs(number) * ope)))



def latex_number(ascii):
Beispiel #10
0
def unit_string(value, units, prefu={'M', 'L', 'J', 'C', 'V', 'N', 'W', 'Pa'}):
    """Determine the most compact set of units for a quantity given in SI units. Also, choose which units of equal dimensions to
    choose while keeping the number near 1.

    Returns: (the number(float) and, as two lists, the positive and negative units of the quantity

    >>> unit_string(8.314, (0, 1, 2, -2, -1, -1, 0, 0), {'K', 'J', 'mol'})
    (8.314, [('J', 1)], [('K', 1), ('mol', 1)])
    >>> unit_string(8.314, (0, 1, 2, -2, -1, -1, 0, 0), set())
    (8.314, [('kg', 1), ('m', 2)], [('K', 1), ('s', 2), ('mol', 1)])
    >>> unit_string(0.00513, (0, 0, 1, 0, 0, 0, 0, 0), {'mm'})
    (5.13, [('mm', 1)], [])
    >>> unit_string(0.04713, (0, 0, 1, 0, 0, 0, 0, 0), {'m', 'mm'})
    (47.13, [('mm', 1)], [])
    >>> unit_string(4.713, (0, 0, 1, 0, 0, 0, 0, 0), {'m', 'mm'})
    (4.713, [('m', 1)], [])
    """
    if units == unity or not value:
        if value and '%' in prefu:
            return value*100, [('%', 1)], []
        return value, "", ""
    SIunits = list(units)
    derived = dict((pu, 0) for pu in prefu if (pu in unitquant and sum(abs(t) for t in unitquant[pu].units) > 1))
    while derived:
        (improvement, d, sign) = try_all_derived(SIunits, derived)
        if improvement <= 0:
            break
        derived[d] += sign
        for i, used_in_derived in enumerate(unitquant[d].units):
            SIunits[i] -= used_in_derived * sign
        value /= unitquant[d].number ** sign
    allunits = dict([(x, derived[x]) for x in derived if derived[x]])
    DUS = [du for du in allunits if du in derived]
    for DU in DUS: # choose between atm and mmHg etc.
        choices = [pu for pu in derived if unitquant[pu].units == unitquant[DU].units]
        if not choices:
            break
        quality = [(abs(math_log10(abs(value) / (unitquant[c].number / unitquant[DU].number) ** allunits[DU]) - 1.0), c)
                   for c in choices]
        best = min(quality)[1]
        if best != DU:
            allunits[best] = allunits[DU]
            value /= (unitquant[best].number / unitquant[DU].number) ** allunits[best]
            allunits[DU] = 0
    for i, SIU in enumerate(SIunit_symbols): # choose between m, cm, mm etc.
        if not SIunits[i]:
            continue
        choices = [pu for pu in prefu if (pu not in derived and pu in unitquant) and unitquant[pu].units[i]]
        if not choices:
            break
        quality = [(abs(math_log10(abs(value) / unitquant[c].number ** (SIunits[i] / unitquant[c].units[i])) - 1.0), c)
                   for c in choices]
        best = min(quality)[1]
        allunits[best] = Fraction(SIunits[i], unitquant[best].units[i])
        if allunits[best].denominator == 1:
            allunits[best] = int(allunits[best])
        SIunits[i] = 0
        value /= unitquant[best].number ** allunits[best]
    for u, d in zip(SIunit_symbols, SIunits):
        if d:
            allunits[u] = d
    #print('unitstring', allunits, prefu)
    if '°ΔC' in prefu and 'K' in allunits:
        allunits['°ΔC'] = allunits['K']
        allunits['K'] = 0
        #rint('unitstring delC', allunits)
    elif '°aC' in prefu and allunits == {'K':1}:
        #unitstring {'K': 1} {'°aC', 'K'}
        allunits = {'°aC':1}
        value -= 273.15
        #rint('unitstring absC', allunits)
    poslist = [(u, exp) for u, exp in allunits.items() if exp > 0]
    neglist = [(u, -exp) for u, exp in allunits.items() if exp < 0]
    return value, poslist, neglist
Beispiel #11
0
def qlog(a):
    return Q(math_log10(a.number), "log(%s)", a.units, abs(a.uncert / a.number), a.prefu, (a,))
Beispiel #12
0
def mostsig(number, ope=1.00000000001): return int(floor(math_log10(abs(number) * ope)))



def latex_number(ascii):
Beispiel #13
0
def unit_string(value, units, prefu={'M', 'L', 'J', 'C', 'V', 'N', 'W', 'Pa'}):
    """Determine the most compact set of units for a quantity given in SI units. Also, choose which units of equal dimensions to
    choose while keeping the number near 1.

    Returns: (the number(float) and, as two lists, the positive and negative units of the quantity

    >>> unit_string(8.314, (0, 1, 2, -2, -1, -1, 0, 0), {'K', 'J', 'mol'})
    (8.314, [('J', 1)], [('K', 1), ('mol', 1)])
    >>> unit_string(8.314, (0, 1, 2, -2, -1, -1, 0, 0), set())
    (8.314, [('kg', 1), ('m', 2)], [('K', 1), ('s', 2), ('mol', 1)])
    >>> unit_string(0.00513, (0, 0, 1, 0, 0, 0, 0, 0), {'mm'})
    (5.13, [('mm', 1)], [])
    >>> unit_string(0.04713, (0, 0, 1, 0, 0, 0, 0, 0), {'m', 'mm'})
    (47.13, [('mm', 1)], [])
    >>> unit_string(4.713, (0, 0, 1, 0, 0, 0, 0, 0), {'m', 'mm'})
    (4.713, [('m', 1)], [])
    """
    if units == unity or not value:
        return value, "", ""
    SIunits = list(units)
    derived = dict((pu, 0) for pu in prefu if sum(abs(t) for t in unitquant[pu].units) > 1)
    while derived:
        (improvement, d, sign) = try_all_derived(SIunits, derived)
        if improvement <= 0:
            break
        derived[d] += sign
        for i, used_in_derived in enumerate(unitquant[d].units):
            SIunits[i] -= used_in_derived * sign
        value /= unitquant[d].number ** sign
    allunits = dict([(x, derived[x]) for x in derived if derived[x]])
    DUS = [du for du in allunits if du in derived]
    for DU in DUS: # choose between atm and mmHg etc.
        choices = [pu for pu in derived if unitquant[pu].units == unitquant[DU].units]
        if not choices:
            break
        quality = [(abs(math_log10(abs(value) / (unitquant[c].number / unitquant[DU].number) ** allunits[DU]) - 1.0), c)
                   for c in choices]
        best = min(quality)[1]
        if best != DU:
            allunits[best] = allunits[DU]
            value /= (unitquant[best].number / unitquant[DU].number) ** allunits[best]
            allunits[DU] = 0
    for i, SIU in enumerate(SIunit_symbols): # choose between m, cm, mm etc.
        if not SIunits[i]:
            continue
        choices = [pu for pu in prefu if (pu not in derived) and unitquant[pu].units[i]]
        if not choices:
            break
        quality = [(abs(math_log10(abs(value) / unitquant[c].number ** (SIunits[i] / unitquant[c].units[i])) - 1.0), c)
                   for c in choices]
        best = min(quality)[1]
        allunits[best] = Fraction(SIunits[i], unitquant[best].units[i])
        if allunits[best].denominator == 1:
            allunits[best] = int(allunits[best])
        SIunits[i] = 0
        value /= unitquant[best].number ** allunits[best]
    for u, d in zip(SIunit_symbols, SIunits):
        if d:
            allunits[u] = d
    poslist = [(u, exp) for u, exp in allunits.items() if exp > 0]
    neglist = [(u, -exp) for u, exp in allunits.items() if exp < 0]
    return value, poslist, neglist
def r6_dnn_image_display(target_dirname, dnn_image_obj=None, show_fig=False):
    LOGGER.info('{}: r6: Turning upsampled envelope into image...'.format(target_dirname))
    if dnn_image_obj is None:
        dnn_image_obj = loadmat(os_path_join(target_dirname, DNN_IMAGE_FNAME))

    beam_position_x_up = dnn_image_obj['beam_position_x_up']
    depth = dnn_image_obj['depth']
    envUp_dB = dnn_image_obj['envUp_dB']
    env_up = dnn_image_obj['envUp']

    LOGGER.debug('{}: r6: Finished loading vars'.format(target_dirname))
    x = np_squeeze(beam_position_x_up) # beam_position_x_up
    y = np_squeeze(depth) # depth

    LOGGER.debug('{}: r6: Finished squeezing x, y'.format(target_dirname))
    fig, ax = plt_subplots()
    LOGGER.debug('{}: r6: Finished plt.figure'.format(target_dirname))
    image = ax.imshow(envUp_dB, vmin=-60, vmax=0, cmap='gray', aspect='auto', extent=[x[0]*1000, x[-1]*1000, y[-1]*1000, y[0]*1000])
    ax.set_aspect('equal')
    LOGGER.debug('{}: r6: Finished plt.imshow'.format(target_dirname))
    fig.colorbar(image)
    LOGGER.debug('{}: r6: Finished plt.colorbar'.format(target_dirname))
    # plt_xlabel('lateral (mm)', fontsize=FONT_SIZE)
    ax.set_xlabel('lateral (mm)', fontsize=FONT_SIZE)
    # plt_ylabel('axial (mm)', fontsize=FONT_SIZE)
    ax.set_ylabel('axial (mm)', fontsize=FONT_SIZE)
    LOGGER.debug('{}: r6: Finished plt.xlabel/ylabel'.format(target_dirname))

    # if show_fig is True:
    # plt_show(block=False)

    # Save image to file
    dnn_image_path = os_path_join(target_dirname, DNN_IMAGE_SAVE_FNAME)
    fig.savefig(dnn_image_path)
    plt_close(fig)

    LOGGER.debug('{}: r6: Finished saving figure'.format(target_dirname))
    # scan_battery_dirname = os_path_dirname(target_dirname)
    # process_scripts_dirpath = os_path_join(scan_battery_dirname, PROCESS_SCRIPTS_DIRNAME)

    # circle_radius = load_single_value(process_scripts_dirpath, CIRCLE_RADIUS_FNAME)
    # circle_coords_x = load_single_value(process_scripts_dirpath, CIRCLE_COORDS_X_FNAME)
    # circle_coords_y = load_single_value(process_scripts_dirpath, CIRCLE_COORDS_Y_FNAME)

    # xx, yy = np_meshgrid(x, y)
    # mask_in = get_circular_mask(xx, yy, (circle_coords_x, circle_coords_y), circle_radius)
    #
    # # mask_in
    # # create rectangular region outside lesion
    # box_xmin_right = load_single_value(process_scripts_dirpath, BOX_XMIN_RIGHT_FNAME)
    # box_xmax_right = load_single_value(process_scripts_dirpath, BOX_XMAX_RIGHT_FNAME)
    #
    # box_xmin_left = load_single_value(process_scripts_dirpath, BOX_XMIN_LEFT_FNAME)
    # box_xmax_left = load_single_value(process_scripts_dirpath, BOX_XMAX_LEFT_FNAME)
    #
    # # Box shares y position and height with circle (diameter)
    # ymin = circle_coords_y - circle_radius
    # ymax = circle_coords_y + circle_radius
    # mask_out_left = (xx >= box_xmin_left) * (xx <= box_xmax_left) * (yy >= ymin) * (yy <= ymax)
    # mask_out_right = get_rectangle_mask(xx, yy, box_xmin_right, box_xmax_right, ymin, ymax)
    # mask_out = mask_out_left | mask_out_right

    # Display circle and boxes
    # with_circle = envUp_dB.copy()
    #
    #
    # with_circle[mask_out_left+mask_in+mask_out_right] = 0
    #
    # plt.figure(figsize=(12,16))
    # plt.imshow(with_circle, vmin=-60, vmax=0, cmap='gray', aspect='auto', extent = [beam_position_x_up[0]*1000,beam_position_x_up[-1]*1000,depth[-1]*1000, depth[0]*1000])
    # plt.colorbar()
    # FONT_SIZE = 20
    # plt.xlabel('lateral (mm)', fontsize=FONT_SIZE)
    # plt.ylabel('axial (mm)', fontsize=FONT_SIZE)

    # plt.show()

    # Calculate image statistics
    # print('r6: env_up.shape =', env_up.shape)
    mask_in, mask_out = get_masks(target_dirname)
    LOGGER.debug('{}: r6: Finished loading masks'.format(target_dirname))
    # print('r6: mask_in.shape={}, mask_out.shape={}'.format(mask_in.shape, mask_out.shape))
    env_up_inside_lesion = env_up[mask_in]
    mean_in = env_up_inside_lesion.mean()
    var_in = env_up_inside_lesion.var(ddof=1) # ddof is important cuz Matlab

    env_up_outside_lesion = env_up[mask_out]
    mean_out = env_up_outside_lesion.mean()
    var_out = env_up_outside_lesion.var(ddof=1) # ddof is important cuz Matlab

    LOGGER.debug('{}: r6: Finished mean and var calculations'.format(target_dirname))
    CR = -20 * math_log10(mean_in / mean_out)
    CNR = 20 * math_log10(abs(mean_in - mean_out)/math_sqrt(var_in + var_out))
    SNR = mean_out / math_sqrt(var_out)

    LOGGER.debug('{}: r6: Finished speckle stats calculations'.format(target_dirname))
    # Save image statistics to file
    speckle_stats = [CR, CNR, SNR, mean_in, mean_out, var_in, var_out]
    speckle_stats_path = os_path_join(target_dirname, SPECKLE_STATS_FNAME)

    with open(speckle_stats_path, 'w') as f:
        f.write("\n".join([str(item) for item in speckle_stats]))

    LOGGER.debug('{}: r6: Finished saving .txt'.format(target_dirname))
    # Also save image statistics json as a redundant (but more readable) method
    speckle_stats_dict = {
        'CR': CR,
        'CNR': CNR,
        'SNR': SNR,
        'mean_inside_lesion': mean_in,
        'variance_inside_lesion': var_in,
        'mean_outside_lesion': mean_out,
        'variance_outside_lesion': var_out,
    }

    speckle_stats_dict_path = os_path_join(target_dirname, SPECKLE_STATS_DICT_FNAME)

    with open(speckle_stats_dict_path, 'w') as f:
        json_dump(speckle_stats_dict, f, indent=4)
    LOGGER.debug('{}: r6: Finished saving .json'.format(target_dirname))

    LOGGER.info('{}: r6 Done'.format(target_dirname))