def find_spinning(mag,gyros):
		""" return the indicies in the magnetometer data when
		the gyro indicates it is spinning on the z axis """

		import numpy
		import scipy.signal
		from matplotlib.mlab import find

		threshold = 40
		spinning = scipy.signal.medfilt(abs(gyros['z'][:,0]),kernel_size=5) > threshold

		# make sure to always find end elements
		spinning = numpy.concatenate((numpy.array([False]),spinning,numpy.array([False])))
		start = find(spinning[1:] & ~spinning[0:-1])
		stop = find(~spinning[1:] & spinning[0:-1])-1

		tstart = gyros['time'][start]
		tstop = gyros['time'][stop]

		idx = numpy.zeros((0),dtype=int)
		for i in arange(tstart.size):

			i1 = abs(mag['time']-tstart[i]).argmin()
			i2 = abs(mag['time']-tstop[i]).argmin()
			
			idx = numpy.concatenate((idx,arange(i1,i2,dtype=int)))

		return idx
Exemple #2
0
    def find_spinning(mag, gyros):
        """ return the indicies in the magnetometer data when
		the gyro indicates it is spinning on the z axis """

        import numpy
        import scipy.signal
        from matplotlib.mlab import find

        threshold = 40
        spinning = scipy.signal.medfilt(abs(gyros['z'][:, 0]),
                                        kernel_size=5) > threshold

        # make sure to always find end elements
        spinning = numpy.concatenate(
            (numpy.array([False]), spinning, numpy.array([False])))
        start = find(spinning[1:] & ~spinning[0:-1])
        stop = find(~spinning[1:] & spinning[0:-1]) - 1

        tstart = gyros['time'][start]
        tstop = gyros['time'][stop]

        idx = numpy.zeros((0), dtype=int)
        for i in arange(tstart.size):

            i1 = abs(mag['time'] - tstart[i]).argmin()
            i2 = abs(mag['time'] - tstop[i]).argmin()

            idx = numpy.concatenate((idx, arange(i1, i2, dtype=int)))

        return idx
Exemple #3
0
def sigmoid(feature_map):
    shape = feature_map.shape
    output = zeros(shape)

    for channel_number in range(shape[-1]):
        for row in arange(0, shape[0]):
            for column in arange(0, shape[1]):
                output[row, column, channel_number] = 1 / (
                    1 + math.exp(feature_map[row, column, channel_number]))
Exemple #4
0
def indicatorMapFromAxis(value, limits, squareSize):
    '''axis = [xmin, xmax, ymin, ymax] '''
    from numpy.core.multiarray import arange
    from numpy import floor
    indicatorMap = {}
    for x in arange(limits[0], limits[1], squareSize):
        for y in arange(limits[2], limits[3], squareSize):
            indicatorMap[(floor(x/squareSize), floor(y/squareSize))] = value
    return indicatorMap
Exemple #5
0
def relu(
    feature_map
):  # Activation function, normalize of what is passed from the convolution stage
    shape = feature_map.shape
    output = zeros(shape)

    for channel_number in range(shape[-1]):
        for row in arange(0, shape[0]):
            for column in arange(0, shape[1]):
                output[row, column, channel_number] = numpy.max(
                    [0, feature_map[row, column, channel_number]])

    return output
Exemple #6
0
def constroi_onda_amostras(request, t_start, t_stop, dc, ampl, freq, desl, fs,
                           s_npf):
    # Constrói o sinal e suas amostras

    # converte o recebido do simulador para float e cria os arange, para o sinal e para as amostras
    t_start_c = float(t_start)  # forma para conversão para float
    t_stop_c = t_stop.__float__()  # outra forma para conversão para float

    t_step = 1 / (100 * float(freq))
    fs_c = float(fs)
    t = arange(t_start_c, t_stop_c, t_step)
    t2 = arange(t_start_c, t_stop_c, 1 / fs_c)

    # verifica o tipo de onda e constrói o sinal e as amostras
    if str(s_npf) == 'cos':
        s = float(dc) + float(ampl) * cos(2 * pi * float(freq) *
                                          (t + float(desl)))
        s2 = float(dc) + float(ampl) * cos(2 * pi * float(freq) *
                                           (t2 + float(desl)))
    elif str(s_npf) == 'sin':
        s = float(dc) + float(ampl) * sin(2 * pi * float(freq) *
                                          (t + float(desl)))
        s2 = float(dc) + float(ampl) * sin(2 * pi * float(freq) *
                                           (t2 + float(desl)))
    elif str(s_npf) == 'sinc':
        s = float(dc) + float(ampl) * sinc(2 * float(freq) * (t + float(desl)))
        s2 = float(dc) + float(ampl) * sinc(2 * float(freq) *
                                            (t2 + float(desl)))
    elif str(s_npf) == 'sinc2':
        s = float(dc) + float(ampl) * sinc(float(freq) * (t + float(desl)))**2
        s2 = float(dc) + float(ampl) * sinc(float(freq) *
                                            (t2 + float(desl)))**2
    elif str(s_npf) == 'sawtooth':
        s = float(dc) + float(ampl) * sawtooth(
            2 * pi * float(freq) * (t + float(desl)), width=1)
        s2 = float(dc) + float(ampl) * sawtooth(
            2 * pi * float(freq) * (t2 + float(desl)), width=1)
    elif str(s_npf) == 'square':
        s = float(dc) + float(ampl) * signal.square(2 * pi * float(freq) *
                                                    (t + float(desl)))
        s2 = float(dc) + float(ampl) * signal.square(2 * pi * float(freq) *
                                                     (t2 + float(desl)))
    elif str(s_npf) == 'triangle':
        # sawtooth por default vai de +1 para -1, por isso coloquei a ampl negativa para "corrigir" a onda
        s = float(dc) - float(ampl) * sawtooth(
            2 * pi * float(freq) * (t + float(desl)), width=0.5)
        s2 = float(dc) - float(ampl) * sawtooth(
            2 * pi * float(freq) * (t2 + float(desl)), width=0.5)

    return s, t, s2, t2
Exemple #7
0
def indicatorMapFromPolygon(value, polygon, squareSize):
    '''Fills an indicator map with the value within the polygon
    (array of Nx2 coordinates of the polygon vertices)'''
    import matplotlib.nxutils as nx
    from numpy.core.multiarray import array, arange
    from numpy import floor

    points = []
    for x in arange(min(polygon[:,0])+squareSize/2, max(polygon[:,0]), squareSize):
        for y in arange(min(polygon[:,1])+squareSize/2, max(polygon[:,1]), squareSize):
            points.append([x,y])
    inside = nx.points_inside_poly(array(points), polygon)
    indicatorMap = {}
    for i in xrange(len(inside)):
        if inside[i]:
            indicatorMap[(floor(points[i][0]/squareSize), floor(points[i][1]/squareSize))] = 0
    return indicatorMap
def fit(function, parameters, y, x = None):
    def f(params):
        i = 0
        for p in parameters:
            p.set(params[i])
            i += 1
        return y - function(x)

    if x is None: x = arange(y.shape[0])
    p = [param() for param in parameters]
    p, success = optimize.leastsq(f, p)
    return p, success
Exemple #9
0
def fit(function, parameters, y, x=None):
    def f(params):
        i = 0
        for p in parameters:
            p.set(params[i])
            i += 1
        return y - function(x)

    if x is None: x = arange(y.shape[0])
    p = [param() for param in parameters]
    p, success = optimize.leastsq(f, p)
    return p, success
Exemple #10
0
class PlotSearchLines_GEVec(PlotEnvelopeSearchLines):
    filename = "GEVec"
    num_delays = arange(40)
    legend_outside = False
    reference_color = red
    plot_IQR = True
    convolvers = tuple(
        SpatiotemporalConvolution(num_delays=num) for num in num_delays)
    labels = ["GEVec, all channels"]
    colors = [blue]
    envelope_maker_lists = [convolvers]
    with_reference = True
Exemple #11
0
    def _get_optimized_learning_step(self, partial_semantics):
        """Calculates optimized learning step."""
        """ bootstrap samples; compute OLS for each; use desired criterion to select the final LS """
        if self.bootstrap_ols:

            weights = []
            size = self.target_vector.shape[0]

            for sample in range(self.bootstrap_ols_samples):

                idx = np_choice(arange(size), size, replace=True)

                bootstrap_delta_target = copy(
                    self.target_vector[idx]).astype(float)
                if self.champion:
                    full_predictions = self.champion.neural_network.get_predictions(
                    )
                    bootstrap_delta_target -= full_predictions[idx]

                bootstrap_partial_semantics = partial_semantics[idx]
                inverse = array(
                    pinv(
                        resize(bootstrap_partial_semantics,
                               (1, bootstrap_partial_semantics.size))))
                ols = dot(inverse.transpose(), bootstrap_delta_target)[0]

                weights += [ols]

            ols_median = median(weights)
            ols_mean = mean(weights)
            ols = self._compute_ols(partial_semantics)
            abs_dif = abs(ols_median - ols_mean)

            if abs_dif >= self.high_absolute_ls_difference:
                self.high_absolute_differences_history.append(
                    [abs_dif, ols_median, ols_mean, ols])
                #===============================================================
                # print('Absolute difference: %.3f, median vs. mean: %.3f vs. %.3f' % (abs_dif, ols_median, ols_mean))
                #===============================================================
                #===============================================================
                # print('Absolute difference: %.3f, median vs. mean vs. OLS: %.3f vs. %.3f vs. %.3f' % (abs_dif, ols_median, ols_mean, ols))
                # print()
                #===============================================================

            if self.bootstrap_ols_criterion == 'median':
                return median(weights)
            else:
                return mean(weights)

        else:
            return self._compute_ols(partial_semantics)
Exemple #12
0
def digits_with_repetition_labels() -> Tuple[array, array]:
    """
    Return a random list of 10 digits from 0 to 9. Two of the digits will be repeated. The rest will be unique.
    Along with this list, return a list of 10 labels, where the label is 0 if the corresponding digit is unique and 1
    if it is repeated.

    :return: digits and labels
    """
    n = 10
    xs = arange(n)
    ys = zeros(n, int)
    shuffle(xs)
    i, j = sample(range(n), 2)
    xs[i] = xs[j]
    ys[i] = ys[j] = 1
    return xs, ys
Exemple #13
0
def main() -> None:
    scenario, planning_problem = load_scenario(
        'scenarios/DEU_B471-1_1_T-1_mod_2.xml')

    num_time_steps: int = 15
    fixed_drawables: List[drawable_types] = []
    for obj in [scenario.lanelet_network, planning_problem.initial_state]:
        fixed_drawables.append(DrawHelp.convert_to_drawable(obj))
    for time_step in range(1, num_time_steps + 1):
        for obs in scenario.static_obstacles:
            fixed_drawables.append(
                DrawHelp.convert_to_drawable(
                    [obs.occupancy_at_time(time_step)]))
        for obs in scenario.dynamic_obstacles:
            fixed_drawables.append(
                DrawHelp.convert_to_drawable(
                    [obs.occupancy_at_time(time_step)]))

    min_velocity: int = 40
    max_velocity: int = 59
    velocity_step_size: float = 0.1
    drivable_areas: Dict[int, float] = {}

    config_file: TextIO = open("config.conf", "w")
    config_file.write("Velocities from " + str(min_velocity) + " to " +
                      str(max_velocity))
    config_file.write("num_time_steps: " + str(num_time_steps))
    config_file.write("time_step_size: " + str(scenario.dt))
    config_file.write("GenerationConfig.max_yaw: " +
                      str(GenerationConfig.max_yaw))
    config_file.write("GenerationConfig.yaw_steps: " +
                      str(GenerationConfig.yaw_steps))
    config_file.write("GenerationConfig.num_threads: " +
                      str(GenerationConfig.num_threads))
    config_file.write("GenerationConfig.position_threshold: " +
                      str(GenerationConfig.position_threshold))
    config_file.write("GenerationConfig.angle_threshold: " +
                      str(GenerationConfig.angle_threshold))
    config_file.write("DrawConfig.car_length: " + str(DrawConfig.car_length))
    config_file.write("DrawConfig.car_width: " + str(DrawConfig.car_width))
    config_file.close()

    overall_time: datetime = datetime.now()
    for velocity in arange(min_velocity, max_velocity + velocity_step_size,
                           velocity_step_size):
        print("Velocity: " + str(velocity))

        # Prepare figure and subplots
        main_fig, (scenario_fig, area_fig) = plt.subplots(nrows=1,
                                                          ncols=2,
                                                          figsize=(19.20,
                                                                   10.80),
                                                          dpi=100)
        main_fig.suptitle("Influence of velocity to drivable area",
                          fontsize=32)
        plt.sca(scenario_fig)

        # Draw artists to be shown always
        for fixed in fixed_drawables:
            DrawHelp.draw(copy(fixed))

        # Generate and draw states resulting from the current velocity of the ego vehicle
        planning_problem.initial_state.velocity = velocity
        start_time: datetime = datetime.now()
        valid_converted, num_states_processed \
            = GenerationHelp.generate_states(scenario, MyState(planning_problem.initial_state), num_time_steps)
        print("Processed " + str(num_states_processed) + " states in " +
              str(datetime.now() - start_time))

        all_states: List[VehicleInfo] = flatten_dict_values(valid_converted)

        for vehicle in all_states:
            DrawHelp.draw(vehicle.drawable)

        union: MultiPolygon = DrawHelp.union_to_polygon(
            list(map(lambda v: v.drawable, all_states)))
        DrawHelp.draw(union)
        drivable_areas[velocity] = union.area

        scenario_fig.set_aspect('equal')
        scenario_fig.set_xlim([100, 200])
        scenario_fig.set_ylim([30, 100])
        scenario_fig.set_xlabel("position [m]")
        scenario_fig.set_ylabel("position [m]")

        # Draw changes of drivable area
        area_fig.set_xlim([min_velocity, max_velocity])
        area_fig.set_ylim([0, max(270.0, max(drivable_areas.values()))])
        area_fig.plot(drivable_areas.keys(), drivable_areas.values(), 'x',
                      drivable_areas.keys(), drivable_areas.values(), '-')
        area_fig.set_xlabel("velocity [m/s]")
        area_fig.set_ylabel("area [m²]")

        # Save the figure
        plt.savefig("out_velocity_" + str(velocity) + ".png")
        # plt.close()  # FIXME Causes EOFErrors

    print("Overall generation took: " + str(datetime.now() - overall_time))
Exemple #14
0
c = -65

d = 8

iapp = 10
tr = [200 / dt, 700 / dt]
T = ceil(tmax / dt)
print(T)
v = zeros(int(T))
print(v)
u = zeros(int(T))
print(u)
v[0] = -70
u[0] = -14

for t in arange(T - 1):
    if t > tr[0] and t < tr[1]:
        i = iapp
    else:
        i = 0

    if v[int(t)] < 35:
        dv = (0.04 * v[int(t)] + 5) * v[int(t)] + 140 - u[int(t)]
        v[int(t) + 1] = v[int(t)] + (dv + i) * dt
        du = a * (b * v[int(t)] - u[int(t)])
        u[int(t) + 1] = u[int(t)] + dt * du
    else:
        v[int(t)] = 35
        v[int(t) + 1] = c
        u[int(t) + 1] = u[int(t)] + d
Exemple #15
0
 def delays(self):
     """ Includes the current time sample, as delay = 0. """
     return arange(self.num_delays + 1)
Exemple #16
0
def mag_calibration(mag, gyros=None, LH=200, LV=500):
    """ Calibrates the magnetometer data by fitting it to a sphere,
	ideally when constantly turning to spread the data around that
	sphere somewhat evenly (or at least in a horizontal plane)"""

    import numpy
    from scipy.optimize import minimize
    from numpy.core.multiarray import arange

    def find_spinning(mag, gyros):
        """ return the indicies in the magnetometer data when
		the gyro indicates it is spinning on the z axis """

        import numpy
        import scipy.signal
        from matplotlib.mlab import find

        threshold = 40
        spinning = scipy.signal.medfilt(abs(gyros['z'][:, 0]),
                                        kernel_size=5) > threshold

        # make sure to always find end elements
        spinning = numpy.concatenate(
            (numpy.array([False]), spinning, numpy.array([False])))
        start = find(spinning[1:] & ~spinning[0:-1])
        stop = find(~spinning[1:] & spinning[0:-1]) - 1

        tstart = gyros['time'][start]
        tstop = gyros['time'][stop]

        idx = numpy.zeros((0), dtype=int)
        for i in arange(tstart.size):

            i1 = abs(mag['time'] - tstart[i]).argmin()
            i2 = abs(mag['time'] - tstop[i]).argmin()

            idx = numpy.concatenate((idx, arange(i1, i2, dtype=int)))

        return idx

    if gyros is not None:
        idx = find_spinning(mag, gyros)
    else:
        idx = arange(mag['time'].size)

    mag_x = mag['x'][idx, 0]
    mag_y = mag['y'][idx, 0]
    mag_z = mag['z'][idx, 0]

    rx = max(mag_x) - min(mag_x)
    ry = max(mag_y) - min(mag_y)

    mx = rx / 2 + min(mag_x)
    my = ry / 2 + min(mag_y)

    def distortion(x, mag_x=mag_x, mag_y=mag_y, mag_z=mag_z, LH=LH, LV=LV):
        """ loss function for distortion from spherical data """
        from numpy import sqrt, mean
        cor_x = mag_x * x[0] - x[3]
        cor_y = mag_y * x[1] - x[4]
        cor_z = mag_z * x[2] - x[5]
        l = sqrt(cor_x**2 + cor_y**2 + cor_z**2)
        L0 = sqrt(LH**2 + LV**2)
        spherical_error = mean((l - L0)**2)

        # note that ideally the horizontal error would be calculated
        # after correcting for attitude but that requires high temporal
        # accuracy from attitude which we don't want to requires. this
        # works well in practice.
        lh = sqrt(cor_x**2 + cor_y**2)
        horizontal_error = mean((lh - LH)**2)

        # weight both the spherical error and the horizontal error
        # components equally
        return spherical_error + horizontal_error

    cons = ({
        'type': 'ineq',
        'fun': lambda x: numpy.array([x[0] - 0.01])
    }, {
        'type': 'ineq',
        'fun': lambda x: numpy.array([x[1] - 0.01])
    }, {
        'type': 'ineq',
        'fun': lambda x: numpy.array([x[2] - 0.01])
    })
    opts = {'xtol': 1e-8, 'disp': False, 'maxiter': 10000}

    # method of COBYLA also works well
    x0 = numpy.array([1, 1, 1, 0, 0, 0])
    res = minimize(distortion,
                   x0,
                   method='Nelder-Mead',
                   options=opts,
                   constraints=cons)

    x = res.x
    cor_x = mag_x * x[0] - x[3]
    cor_y = mag_y * x[1] - x[4]
    cor_z = mag_z * x[2] - x[5]

    import matplotlib
    from numpy import sqrt
    matplotlib.pyplot.subplot(1, 2, 1)
    matplotlib.pyplot.plot(cor_x, cor_y, '.', cor_x, cor_z, '.', cor_z, cor_y,
                           '.')
    #matplotlib.pyplot.xlim(-1,1)
    #matplotlib.pyplot.ylim(-1,1)
    matplotlib.pyplot.subplot(1, 2, 2)
    matplotlib.pyplot.plot(sqrt(cor_x**2 + cor_y**2 + cor_z**2))

    return res, cor_x, cor_y, cor_z
 def getZones(zoneCount=25):
     step = (totalZone[1] - totalZone[0])/zoneCount
     temp = arange(totalZone[0],totalZone[1]+1,step)
     return [(temp[i],temp[i+1]) for i in range(len(temp)-1)]
Exemple #18
0
p = data[:, odorants]
# p = np.transpose(centers)
pl.suptitle('K-Means with ' + str(features) + " features")
pl.subplot(131)
pl.xlabel("Euclidean Distance between \nglomeruli with " + str(features) +
          " features")
pl.ylabel("Glomerulus")
link = linkage(distance.pdist(p, 'euclidean'), method="single")
dend = dendrogram(link, labels=hallem.or_list, orientation="right")
sorted_list = p[dend["leaves"]]

pl.subplot(132)
pl.pcolor(sorted_list)
pl.ylim((0, 23))
pl.xticks(arange(.5, .5 + features),
          hallem.odorant_list[odorants],
          rotation="vertical")
pl.title("Heat Map of \nGlomeruli Activity")
pl.colorbar()

pl.subplot(133)
pl.title("Fingerprint")
for odor in range(0, len(sorted_list)):
    x = range(0, len(sorted_list[odor]))
    y = [odor] * len(sorted_list[odor])
    values = sorted_list[odor]
    pl.scatter(x, y, s=values, c="green", alpha=.75)
    pl.scatter(x, y, s=values * -1, c="red", alpha=1)

    pl.ylim((-.5, 22.5))
Exemple #19
0
fig.autofmt_xdate()

p = data[:, odorants]
# p = np.transpose(centers)
pl.suptitle('K-Means with ' + str(features) + " features")
pl.subplot(131)
pl.xlabel("Euclidean Distance between \nglomeruli with " + str(features) + " features")
pl.ylabel("Glomerulus")
link = linkage(distance.pdist(p, 'euclidean'), method="single")
dend = dendrogram(link, labels=hallem.or_list, orientation="right")
sorted_list = p[dend["leaves"]]

pl.subplot(132)
pl.pcolor(sorted_list)
pl.ylim((0, 23))
pl.xticks(arange(.5, .5 + features), hallem.odorant_list[odorants], rotation="vertical")
pl.title("Heat Map of \nGlomeruli Activity")
pl.colorbar()

pl.subplot(133)
pl.title("Fingerprint")
for odor in range(0, len(sorted_list)):
    x = range(0, len(sorted_list[odor]))
    y = [odor] * len(sorted_list[odor])
    values = sorted_list[odor]
    pl.scatter(x, y, s=values, c="green", alpha=.75)
    pl.scatter(x, y, s=values * -1, c="red", alpha=1)

    pl.ylim((-.5, 22.5))
    pl.xlim((-.5, features))
    pl.xticks(arange(features), hallem.odorant_list[odorants], rotation="vertical")
Exemple #20
0
p = data[:, odorants]
# p = np.transpose(centers)
pl.suptitle('K-Means with ' + str(features) + " features")
pl.subplot(131)
pl.xlabel("Euclidean Distance between \nglomeruli with " + str(
    features) + " features")
pl.ylabel("Glomerulus")
link = linkage(distance.pdist(p, 'euclidean'), method="single")
dend = dendrogram(link, labels=hallem.or_list, orientation="right")
sorted_list = p[dend["leaves"]]

pl.subplot(132)
pl.pcolor(sorted_list)
pl.ylim((0, 23))
pl.xticks(arange(.5, .5 + features), hallem.odorant_list[odorants],
          rotation="vertical")
pl.title("Heat Map of \nGlomeruli Activity")
pl.colorbar()

pl.subplot(133)
pl.title("Fingerprint")
for odor in range(0, len(sorted_list)):
    x = range(0, len(sorted_list[odor]))
    y = [odor] * len(sorted_list[odor])
    values = sorted_list[odor]
    pl.scatter(x, y, s=values, c="green", alpha=.75)
    pl.scatter(x, y, s=values * -1, c="red", alpha=1)

    pl.ylim((-.5, 22.5))
    pl.xlim((-.5, features))
    am =  dict((regType, dict((tw, sum(sites[tw][chrNo][pos] for chrNo, pos in anno[regType])/len(anno[regType]))
                                        for tw in twins))
                                            for regType in anno)



    elapsed('Average methylation calculation')

    print 'Average Methylation per Region'

    fig = plt.figure()
    ax = plt.subplot(111)
    plt.ylim(0,1)
    plt.xlim(0.5, 10.5)

    x = arange(1,len(twins)+1)
    for rt in sorted(am):
        print rt,'sites:%d' % len((anno[rt]))  ,'\t', ' '.join(t+':'+str(am[rt][t]) for t in twins)
        if not rt.startswith('repeat') or rt == 'repeat|All':
            ax.plot(x, [am[rt][t] for t in twins], 'o-', label = rt)

    plt.xticks( x,  tuple(twins) )
    box = ax.get_position()
    ax.set_position([box.x0, box.y0, box.width * 0.8, box.height])

    ax.legend(loc='center left', bbox_to_anchor=(1, 0.5))

    fig.savefig('sites_methylation_per_region1.png')

    # plot repeats
    fig = plt.figure()
Exemple #22
0
#img       = grid.plot()
g = check_and_filter(grid, b)
g.plot()
plot_points(coords[g.indexes], b[g.indexes])
plt.savefig('tests/%s/grid.png' % prefix)
plt.figure()
#exit()

#spline    = LinearInterpolation(grid, b)
#img       = spline.plot((200,200))
#img.figure()
#img       = spline.plot_y_cut(-0.6, 200)
#img.figure()
#
spline    = CubicHermiteSpline(grid, b)
for y in arange(-1.0, 1.0, 0.2):
    spline.plot_y_cut(y, 200, f)
    plt.savefig('tests/%s/nf%.2f.png' % (prefix, y) )
    plt.figure()
spline.plot((200,200))
plt.savefig('tests/%s/nf-field.png' % prefix)
plt.figure()

spline    = CubicHermiteSpline(g, b[g.indexes])
for y in arange(-1.0, 1.0, 0.2):
    spline.plot_y_cut(y, 200, f)
    plt.savefig('tests/%s/f%.2f.png' % (prefix, y) )
    plt.figure()
spline.plot((200,200))
plt.savefig('tests/%s/f-field.png' % prefix)
Exemple #23
0
######################
### Main body
######################

# base name of files
base = "N16_dr10_fr10_br1_bd5"

# plot web page delay
x, y = io.loadtxt(base + ".dly", usecols=[0,1], unpack=True)
plt.close()
plt.plot(x, y, 'o-', linewidth=3)
plt.axis([0, 100, 0, 40])
plt.xlabel("Number of Sessions per ONU ($n$)")
plt.ylabel("$D_W$ [s]")
plt.yticks(ma.arange(0, 50, 10))
plt.grid(linestyle='-')
plt.minorticks_on()
plt.show()
plt.savefig(base + ".dly.png", format='png')

raw_input("Press ENTER to continue ...")

# plot ECR
x, y = io.loadtxt(base + ".ecr", usecols=[0,1], unpack=True)
plt.clf()
plt.plot(x, y, 'o-', linewidth=3)
plt.axis([0, 100, 0, 10])
plt.xlabel("Number of Sessions per ONU ($n$)")
plt.ylabel("ECR [Mbps]")
plt.grid(linestyle='-')
databaseFile = '/home/jeff/work/rotNSruns/macroRun.db'
connection = sqlite3.connect(databaseFile)
c = connection.cursor()
#c.execute("SELECT DISTINCT T FROM MODELS")
#Ts=c.fetchall()
#print "TS: ", Ts
#c.execute("SELECT DISTINCT edMax FROM MODELS ORDER BY edMax")
#edMaxes=c.fetchall()
#print edMaxes
#c.execute("SELECT DISTINCT a FROM MODELS ORDER BY a")
#aes=c.fetchall()
#print aes
# Here we try and do Ansorg plots SHED vs RPOE  with a curves

aes = arange(0.0, 1.1, 0.1)
print aes
filtersT = nearValueFilter("T", 0.5)
filtersEd = nearValueFilter("edMax", 528787878700000.0)
filters = filtersT + filtersEd
for a in aes:
    thisFilter = filters + nearValueFilter("a", a)
    sequencePlot(["rpoe", "shed"],
                 c,
                 thisFilter,
                 suppressShow=True,
                 c=(a, a / 2.0, 0.5))

sequencePlot(["rpoe", "shed"], c, thisFilter, c=(a, a / 2.0, 0.5))
exit()
Exemple #25
0
import zipfile

from numpy import *
from numpy.core.defchararray import array
from numpy.core.multiarray import arange
import os
a = arange(9)
a.shape = (3, 3, 1)

CannedImages = zipfile.ZipFile(
    "C:\\archive\\gerrit\\BurtonInstrument\\src\\focus_images\\focus_images.zip"
)
inputImage = CannedImages.open("image0500.png").read()

a = bytearray(memoryview(inputImage))
b = a[0:6]

if 'PNG' in b:
    print "success"
else:
    print "failure"
Exemple #26
0

import os.path
import numpy.lib.io as io
import numpy.core.multiarray as ma
import matplotlib.pyplot as plt


######################
### Main body
######################

# base name of files
base = "N1_br1_bd5"

# plot web page delay
x, y = io.loadtxt(base + "_n5.dly.org", usecols=[0,1], unpack=True)

plt.close()
plt.plot(x, y, linewidth=3)
plt.axis([0, 10, 0, 4])
plt.xlabel("Access Rate ($R_D$=$R_F$) [Mbps]")
plt.ylabel("$D_W$ [s]")
plt.xticks(ma.arange(0, 11))
#plt.yticks(ma.arange(0, 50, 10))
plt.grid(linestyle='-')
plt.minorticks_on()
plt.show()
plt.savefig(base + "_n5.dly.png", format='png')

Exemple #27
0
def mag_calibration(mag,gyros=None,LH=200,LV=500):
	""" Calibrates the magnetometer data by fitting it to a sphere,
	ideally when constantly turning to spread the data around that
	sphere somewhat evenly (or at least in a horizontal plane)"""

	import numpy
	from scipy.optimize import minimize
	from numpy.core.multiarray import arange

	def find_spinning(mag,gyros):
		""" return the indicies in the magnetometer data when
		the gyro indicates it is spinning on the z axis """

		import numpy
		import scipy.signal
		from matplotlib.mlab import find

		threshold = 40
		spinning = scipy.signal.medfilt(abs(gyros['z'][:,0]),kernel_size=5) > threshold

		# make sure to always find end elements
		spinning = numpy.concatenate((numpy.array([False]),spinning,numpy.array([False])))
		start = find(spinning[1:] & ~spinning[0:-1])
		stop = find(~spinning[1:] & spinning[0:-1])-1

		tstart = gyros['time'][start]
		tstop = gyros['time'][stop]

		idx = numpy.zeros((0),dtype=int)
		for i in arange(tstart.size):

			i1 = abs(mag['time']-tstart[i]).argmin()
			i2 = abs(mag['time']-tstop[i]).argmin()
			
			idx = numpy.concatenate((idx,arange(i1,i2,dtype=int)))

		return idx

	if gyros is not None:
		idx = find_spinning(mag,gyros)
	else:
		idx = arange(mag['time'].size)

	mag_x = mag['x'][idx,0]
	mag_y = mag['y'][idx,0]
	mag_z = mag['z'][idx,0]

	rx = max(mag_x) - min(mag_x)
	ry = max(mag_y) - min(mag_y)

	mx = rx / 2 + min(mag_x)
	my = ry / 2 + min(mag_y)

	def distortion(x,mag_x=mag_x,mag_y=mag_y,mag_z=mag_z,LH=LH,LV=LV):
		""" loss function for distortion from spherical data """
		from numpy import sqrt, mean
		cor_x = mag_x * x[0] - x[3]
		cor_y = mag_y * x[1] - x[4]
		cor_z = mag_z * x[2] - x[5]
		l = sqrt(cor_x**2 + cor_y**2 + cor_z**2)
		L0 = sqrt(LH**2 + LV**2)
		spherical_error = mean((l - L0)**2)

		# note that ideally the horizontal error would be calculated
		# after correcting for attitude but that requires high temporal
		# accuracy from attitude which we don't want to requires. this
		# works well in practice.
		lh = sqrt(cor_x**2 + cor_y**2)
		horizontal_error = mean((lh - LH)**2)

		# weight both the spherical error and the horizontal error
		# components equally
		return spherical_error + horizontal_error

	cons = ({'type': 'ineq', 'fun' : lambda x: numpy.array([x[0] - 0.01])},
	        {'type': 'ineq', 'fun' : lambda x: numpy.array([x[1] - 0.01])},
	        {'type': 'ineq', 'fun' : lambda x: numpy.array([x[2] - 0.01])})
	opts = {'xtol': 1e-8, 'disp': False, 'maxiter': 10000}

	# method of COBYLA also works well
	x0 = numpy.array([1, 1, 1, 0, 0, 0])
	res = minimize(distortion, x0, method='Nelder-Mead', options=opts, constraints=cons)

	x = res.x
	cor_x = mag_x * x[0] - x[3]
	cor_y = mag_y * x[1] - x[4]
	cor_z = mag_z * x[2] - x[5]

	import matplotlib
	from numpy import sqrt
	matplotlib.pyplot.subplot(1,2,1)
	matplotlib.pyplot.plot(cor_x,cor_y,'.',cor_x,cor_z,'.',cor_z,cor_y,'.')
	#matplotlib.pyplot.xlim(-1,1)
	#matplotlib.pyplot.ylim(-1,1)
	matplotlib.pyplot.subplot(1,2,2)
	matplotlib.pyplot.plot(sqrt(cor_x**2+cor_y**2+cor_z**2))

	return res, cor_x, cor_y, cor_z
Exemple #28
0
def arange(start, stop=None, step=1, typecode=None, dtype=None):
    dtype = convtypecode2(typecode, dtype)
    return mu.arange(start, stop, step, dtype)
Exemple #29
0
i1_new, i3_new, Uc1_new = 0, 0, 0


def func1(i1, i3, Uc1):
    return (Umax * math.sin(2 * math.pi * f * t)-i1*R1-Uc1-(i1-i3)*R2)/L1


def func2(i1, i3, Uc1):
    return ((i1-i3)*R2+Uc1-i3*(R3+R4)) / L2

def func3(i1, i3):
    return ((i1-i3)/C1)


if __name__ == '__main__':
    for t in arange(0, (T - h / 2), h):
        U1 = Umax * math.sin(2 * math.pi * f * t)


        i1_new = i1 + h * func1(i1, i3, Uc1)
        i3_new = i3 + h * func2(i1, i3, Uc1)
        Uc1_new = Uc1 + h * func3(i1,i3)

        i1, i3, Uc1 = i1_new, i3_new, Uc1_new
        U2 = i3 * R4
        print(str(U2))
        plt.plot(t + h, U2, 'ro')

    plt.xlabel(r'$t$')
    plt.ylabel(r'$U2$')
    plt.grid(True)
Exemple #30
0
import numpy.core.multiarray as ma
import matplotlib.pyplot as plt


######################
### Main body
######################

# base name of files
base = "N1_br1_bd5"

# plot web page delay
x1, y1 = io.loadtxt(base + "_n1.dly", usecols=[0,1], unpack=True)
x2, y2 = io.loadtxt(base + "_n10.dly", usecols=[0,1], unpack=True)
x3, y3 = io.loadtxt(base + "_n100.dly", usecols=[0,1], unpack=True)
x4, y4 = io.loadtxt(base + "_n400.dly", usecols=[0,1], unpack=True)

plt.close()
l1, l2, l3, l4 = plt.plot(x1, y1, 'bo-', x2, y2, 'r*-', x3, y3, 'gD-', x4, y4, 'c^-', linewidth=3)
plt.legend((l1, l2, l3, l4), ('$n$=1', '$n$=10', '$n$=100', '$n$=400'), 'center right')
plt.axis([0, 10, 0, 100])
plt.xlabel("Access Rate ($R_D$=$R_F$) [Mbps]")
plt.ylabel("$D_W$ [s]")
plt.xticks(ma.arange(0, 11))
#plt.yticks(ma.arange(0, 50, 10))
plt.grid(linestyle='-')
plt.minorticks_on()
plt.show()
plt.savefig(base + ".dly.png", format='png')

Exemple #31
0
def compare_varying_outdegree_traces(settings,
                                     time,
                                     random_seed,
                                     log_path=None,
                                     plot_path=None):
    """
    Args:
        settings (list of Setting): Description s of a setting
        time (int): Total time steps to run
        random_seed (int): Seed for randomness
        log_path (string / None): Desired path for log. Defaults to data/logs/TITLE.csv, where TITLE includes parameters
        plot_path (string / None): Desired path for graphics file. PDF extension is supported, other file formats may
                                  also work depending on matplotlib. Defaults to data/plots/TITLE.pdf
    """

    title = f"var_T{time}_sd{random_seed}"
    for s in settings:
        title += (
            f"_({''.join(m.PLOT_ABBREVIATION for m in s.mechanisms)}_g{round(s.gamma * 100)}_kdist"
            f"{'_'.join(str(round(p * 100)) for p in s.outdegree_distribution)}_d{round(s.d * 100)}_"
            f"sz{s.step_size}_s{s.smoothing})")
    if log_path is None:
        log_path = f"data/logs/{title}.csv"
    if plot_path is None:
        plot_path = f"data/plots/{title}.pdf"

    plt.figure(figsize=(6.4, 3.2))

    fonts = {'family': 'serif', 'serif': ['Libertine']}
    rc('font', **fonts)
    rc('text', usetex=True)

    with open(log_path, 'w') as file:
        file.write(
            f"Smoothened traces: settings={settings}, T={time}, random_seed={random_seed}\n"
        )
        for s in settings:
            print(s)

            # Different iterations should be different for smoothing, but different settings might as well be as
            # reproducible as possible.
            seed(random_seed)

            max_weight_history_sum = [[
                0 for _ in range(ceil(time / s.step_size))
            ] for _ in s.mechanisms]
            num_timeouts = [0 for _ in s.mechanisms]

            for iteration in range(s.smoothing):
                print(f"Iteration {iteration + 1} out of {s.smoothing}")

                elapsed_time = [0. for _ in s.mechanisms]
                time_out = [False for _ in s.mechanisms]
                graph = Graph(s.gamma, s.d, None)

                max_weight_history_for_iteration = [[] for _ in s.mechanisms]

                protocolist = ProtocollingObserver(graph)

                mechanisms = [
                    observer_class(graph) for observer_class in s.mechanisms
                ]
                assert len(mechanisms) > 0

                # pull that into the loop
                tick = 0
                file.write("1\t" + protocolist.protocol[-1])
                for i, mechanism in enumerate(mechanisms):
                    delegations = mechanism.get_delegations()
                    max_weight = mechanism.max_weight_from_delegations(
                        delegations)
                    assert max_weight == 1
                    max_weight_history_for_iteration[i].append(max_weight)
                    file.write("\t" + str(max_weight))
                file.write("\n")

                for t in range(2, time + 1):
                    graph.add_node(
                        choice(arange(1,
                                      len(s.outdegree_distribution) + 1),
                               p=s.outdegree_distribution))
                    file.write(f"{t}\t{protocolist.protocol[-1]}")
                    if (t - 1) % s.step_size == 0:
                        tick += 1
                        for i, mechanism in enumerate(mechanisms):
                            if time_out[i]:
                                continue

                            if elapsed_time[i] >= MECHANISM_TIMEOUT:
                                time_out[i] = True
                            else:
                                begin = perf_counter()
                                try:
                                    delegations = mechanism.get_delegations(
                                        time_out=MECHANISM_TIMEOUT -
                                        elapsed_time[i])
                                except TimeoutError:
                                    time_out[i] = True
                                elapsed_time[i] += perf_counter() - begin

                            if time_out[i]:
                                num_timeouts[i] += 1
                                print(
                                    f"Mechanism {mechanism.PLOT_LABEL} timed out for {num_timeouts[i]}th time in "
                                    f"iteration {iteration} after {elapsed_time[i]} s."
                                )
                                assert len(max_weight_history_for_iteration[i]
                                           ) == tick
                                n = [x * step_size + 1 for x in range(tick)]
                                plt.plot(
                                    n,
                                    max_weight_history_for_iteration[i],
                                    color=mechanism.PLOT_COLOR,
                                    label=
                                    f"{mechanism.PLOT_LABEL} (run {iteration + 1})",
                                    linestyle=mechanism.PLOT_PATTERN,
                                    alpha=1 / (num_timeouts[i] + 1))

                            max_weight = mechanism.max_weight_from_delegations(
                                delegations)
                            max_weight_history_for_iteration[i].append(
                                max_weight)
                            file.write("\t" + str(max_weight))
                    file.write("\n")

                for i, mechanism in enumerate(mechanisms):
                    if not time_out[i]:
                        for t in range(len(
                                max_weight_history_for_iteration[i])):
                            max_weight_history_sum[i][
                                t] += max_weight_history_for_iteration[i][t]

                file.flush()

            n = [x * s.step_size + 1 for x in range(ceil(time / s.step_size))]
            for i, mechanism in enumerate(s.mechanisms):
                if num_timeouts[i] == s.smoothing:
                    print(
                        f"Nothing to plot for {mechanism.PLOT_LABEL}, all iterations timed out."
                    )
                    continue
                max_weight_average = [
                    weight_sum / (s.smoothing - num_timeouts[i])
                    for weight_sum in max_weight_history_sum[i]
                ]
                assert len(n) == len(max_weight_average)
                plt.plot(n,
                         max_weight_average,
                         color=mechanism.PLOT_COLOR,
                         label=mechanism.PLOT_LABEL,
                         linestyle=mechanism.PLOT_PATTERN)

    plt.legend(loc=2)
    plt.ylabel('average maximum weight')
    plt.xlabel('time')
    plt.ylim(ymin=0)
    plt.xlim(xmin=0)
    plt.savefig(plot_path, bbox_inches='tight')