Пример #1
0
def sequence_to_Hdw_at_specific_ws(sequence,
                                   ws,
                                   return_magnitude=True,
                                   verbose=False):
    """
    To turn a radians/sample frequency
    into the DTFT value on the unit circle
    
    """
    if not nu.is_array_like(ws):
        ws = [ws]
        singular_flag = True
    else:
        singular_flag = False

    P1 = pu.Polynomial1D(sequence)
    hdw = [P1.evaluate(exp(1j * w)) for w in ws]

    if return_magnitude:
        hdw = np.abs(hdw)

    if verbose:
        for hd, w in zip(hdw, ws):
            print(f"For {w}: Hd(ejw) = {hd}")

    if singular_flag:
        return hdw[0]
    else:
        return hdw
Пример #2
0
def extract_module_algorithm(module, return_parameters=False, verbose=False):
    """
    Will extract which model
    """
    if type(module) == dict:
        if verbose:
            print(f"\nalgorithms sent in dict")
        algorithms_local = module.get("algorithms", None)
        module = module["module"]
        algorithms_only = True

    elif nu.is_array_like(module, include_tuple=True):
        if verbose:
            print(f"\nalgorithms sent in array")
        algorithms_local = module[1]
        module = module[0]
        algorithms_only = True
    else:
        if verbose:
            print(f"\nNo algorithms per module set")
        algorithms_local = None
        algorithms_only = None

    if return_parameters:
        return module, algorithms_local, algorithms_only
    else:
        return module, algorithms_local
Пример #3
0
def ripple_magnitude_at_specific_ws(
    sequence,
    ws,
    band_freq=None,
    band_weights=None,
    passband_freq=None,
    stopband_freq=None,
    verbose=False,
    return_magnitude=True,
):
    """
    To measure the frequency response at certain points
    """

    if passband_freq is not None and stopband_freq is not None:
        band_freq = [0, passband_freq, stopband_freq, pi]

    if band_weights is None:
        band_weights = np.array([1, -1, 0])
    band_weights = np.array(band_weights)

    if not nu.is_array_like(ws):
        singular_flag = True
        ws = np.array([ws])
    else:
        ws = np.array(ws)
        singular_flag = False

    band_idx = np.digitize(ws, band_freq) - 1
    band_idx[band_idx == len(band_freq) - 1] = len(band_freq) - 2

    final_band_weights = band_weights[band_idx]

    band_idx[final_band_weights == -1] = band_idx[final_band_weights == -1] - 1
    final_band_weights = band_weights[band_idx]

    if verbose:
        print(f"final_band_weights = {final_band_weights}")

    if np.any(final_band_weights < 0):
        raise Exception("Negative band weights")

    hdw = sequence_to_Hdw_at_specific_ws(sequence, ws)

    ripple_values = hdw - final_band_weights

    if return_magnitude:
        ripple_values = np.abs(ripple_values)

    if verbose:
        for w, r in zip(ws, ripple_values):
            print(f"at w = {np.round(w,3)}: ripple = {r}")

    if singular_flag:
        return ripple_values[0]
    else:
        return ripple_values
Пример #4
0
def find_mesh_width_array_border(
        curr_limb,
        node_1,
        node_2,
        width_name="no_spine_median_mesh_center",
        segment_start=1,
        segment_end=4,
        skeleton_segment_size=None,
        width_segment_size=None,
        recalculate_width_array=False,  #will automatically recalculate the width array
        default_segment_size=1000,
        no_spines=True,
        summary_measure="mean",
        print_flag=True,
        **kwargs):
    """
    Purpose: To send back an array that 
    represents the widths of curent branches
    at their boundary
    - the widths may be calculated differently than currently
      stored if specified so

    Applications: 
    1) Will help with filtering out false positives
    with the axon detection
    2) For merge detections to help detect
    large width change

    Process: 
    0) make sure the two nodes are connected in the concept network
    1) if the skeleton_segment_size and width_semgent is None then recalculate the width array
    - send the 
    2) calculate the endpoints from the skeletons (to ensure they are in the right order)
    3) find the connectivity of the endpoints
    4) Get the subarrays of the width_arrays according to the start and end specified
    5) return the subarrays

    Example of Use: 
    find_mesh_width_array_border(curr_limb=curr_limb_obj,
                             #node_1=56,
                             #node_2=71,
                             node_1 = 8,
                             node_2 = 5,
                            width_name = "no_spine_average_mesh_center",
                            segment_start = 1,
                            segment_end = 4,
                            skeleton_segment_size = 50,
                            width_segment_size = None,
                            recalculate_width_array = True, #will automatically recalculate the width array
                            default_segment_size = 1000,
                            print_flag=True
                            )

    """

    # 0) make sure the two nodes are connected in the concept network
    if node_2 not in xu.get_neighbors(curr_limb.concept_network, node_1):
        raise Exception(
            f"Node_1 ({node_1}) and Node_2 ({node_2}) are not connected in the concept network"
        )

    # 0) extract the branch objects
    branch_obj_1 = curr_limb.concept_network.nodes[node_1]["data"]
    branch_obj_2 = curr_limb.concept_network.nodes[node_2]["data"]
    # 1) if the skeleton_segment_size and width_semgent is then recalculate the width array
    if not skeleton_segment_size is None or recalculate_width_array:

        if "mesh_center" in width_name:
            distance_by_mesh_center = True
        else:
            distance_by_mesh_center = False

        if ("no_spine" in width_name) or (no_spines):
            no_spines = True
        else:
            if print_flag:
                print("Using no spines")

        if print_flag:
            print(f"distance_by_mesh_center = {distance_by_mesh_center}")

        if skeleton_segment_size is None:
            skeleton_segment_size = default_segment_size

        if not nu.is_array_like(skeleton_segment_size):
            skeleton_segment_size = [skeleton_segment_size]

        if width_segment_size is None:
            width_segment_size = skeleton_segment_size

        if not nu.is_array_like(width_segment_size):
            width_segment_size = [width_segment_size]

        current_width_array_1, current_width_1 = calculate_new_width(
            branch_obj_1,
            skeleton_segment_size=skeleton_segment_size[0],
            width_segment_size=width_segment_size[0],
            distance_by_mesh_center=distance_by_mesh_center,
            return_average=True,
            print_flag=False,
            no_spines=no_spines,
            summary_measure=summary_measure)

        current_width_array_2, current_width_2 = calculate_new_width(
            branch_obj_2,
            skeleton_segment_size=skeleton_segment_size[-1],
            width_segment_size=width_segment_size[-1],
            distance_by_mesh_center=distance_by_mesh_center,
            no_spines=no_spines,
            return_average=True,
            print_flag=False,
            summary_measure=summary_measure)
    else:
        if print_flag:
            print("**Using the default width arrays already stored**")
        current_width_array_1 = branch_obj_1.width_array[width_name]
        current_width_array_2 = branch_obj_2.width_array[width_name]

    if print_flag:
        print(f"skeleton_segment_size = {skeleton_segment_size}")
        print(f"width_segment_size = {width_segment_size}")
        print(f"current_width_array_1 = {current_width_array_1}")
        print(f"current_width_array_2 = {current_width_array_2}")

    #2) calculate the endpoints from the skeletons (to ensure they are in the right order)
    end_1 = sk.find_branch_endpoints(branch_obj_1.skeleton)
    end_2 = sk.find_branch_endpoints(branch_obj_2.skeleton)

    if print_flag:
        print(f"end_1 = {end_1}")
        print(f"end_2 = {end_2}")

    #3) find the connectivity of the endpoints
    node_connectivity = xu.endpoint_connectivity(end_1, end_2)

    #4) Get the subarrays of the width_arrays according to the start and end specified
    """
    Pseudocode: 

    What to do if too small? Take whole thing

    """
    if print_flag:
        print(f"node_connectivity = {node_connectivity}")

    return_arrays = []
    width_arrays = [current_width_array_1, current_width_array_2]

    for j, current_width_array in enumerate(width_arrays):

        if len(current_width_array) < segment_end:
            if print_flag:
                print(
                    f"The number of segments for current_width_array_{j+1} ({len(current_width_array)}) "
                    " was smaller than the number requested, so just returning the whole width array"
                )

            return_arrays.append(current_width_array)
        else:
            if node_connectivity[j] == 0:
                return_arrays.append(
                    current_width_array[segment_start:segment_end])
            elif node_connectivity[j] == 1:
                return_arrays.append(
                    current_width_array[-segment_end:-segment_start])
            else:
                raise Exception("Node connectivity was not 0 or 1")

    return return_arrays