Exemplo n.º 1
0
 def _pvoc(self, X_hat, Phi_hat=None, R=None):
     """
     ::
       a phase vocoder - time-stretch
       inputs:
         X_hat - estimate of signal magnitude
         [Phi_hat] - estimate of signal phase
         [R] - resynthesis hop ratio
       output:
         updates self.X_hat with modified complex spectrum
     """
     N = self.nfft
     W = self.wfft
     H = self.nhop
     R = 1.0 if R is None else R
     dphi = (2 * P.pi * H * P.arange(N / 2 + 1)) / N
     print("Phase Vocoder Resynthesis...", N, W, H, R)
     A = P.angle(self.STFT) if Phi_hat is None else Phi_hat
     phs = A[:, 0]
     self.X_hat = []
     n_cols = X_hat.shape[1]
     t = 0
     while P.floor(t) < n_cols:
         tf = t - P.floor(t)
         idx = P.arange(2) + int(P.floor(t))
         idx[1] = n_cols - 1 if t >= n_cols - 1 else idx[1]
         Xh = X_hat[:, idx]
         Xh = (1 - tf) * Xh[:, 0] + tf * Xh[:, 1]
         self.X_hat.append(Xh * P.exp(1j * phs))
         U = A[:, idx[1]] - A[:, idx[0]] - dphi
         U = U - P.np.round(U / (2 * P.pi)) * 2 * P.pi
         phs += (U + dphi)
         t += P.randn() * P.sqrt(PVOC_VAR * R) + R  # 10% variance
     self.X_hat = P.np.array(self.X_hat).T
Exemplo n.º 2
0
    def _make_log_freq_map(self):
        """
        ::

            For the given ncoef (bands-per-octave) and nfft, calculate the center frequencies
            and bandwidths of linear and log-scaled frequency axes for a constant-Q transform.
        """
        fp = self.feature_params
        bpo = float(self.nbpo)  # Bands per octave
        self._fftN = float(self.nfft)
        hi_edge = float(self.hi)
        lo_edge = float(self.lo)
        f_ratio = 2.0**(1.0 / bpo)  # Constant-Q bandwidth
        self._cqtN = float(P.floor(P.log(hi_edge / lo_edge) / P.log(f_ratio)))
        self._dctN = self._cqtN
        self._outN = float(self.nfft / 2 + 1)
        if self._cqtN < 1: print("warning: cqtN not positive definite")
        mxnorm = P.empty(self._cqtN)  # Normalization coefficients
        fftfrqs = self._fftfrqs  #P.array([i * self.sample_rate / float(self._fftN) for i in P.arange(self._outN)])
        logfrqs = P.array([
            lo_edge * P.exp(P.log(2.0) * i / bpo) for i in P.arange(self._cqtN)
        ])
        logfbws = P.array([
            max(logfrqs[i] * (f_ratio - 1.0),
                self.sample_rate / float(self._fftN))
            for i in P.arange(self._cqtN)
        ])
        #self._fftfrqs = fftfrqs
        self._logfrqs = logfrqs
        self._logfbws = logfbws
        self._make_cqt()
Exemplo n.º 3
0
 def _pvoc2(self, X_hat, Phi_hat=None, R=None):
     """
     ::
       alternate (batch) implementation of phase vocoder - time-stretch
       inputs:
         X_hat - estimate of signal magnitude
         [Phi_hat] - estimate of signal phase
         [R] - resynthesis hop ratio
       output:
         updates self.X_hat with modified complex spectrum
     """
     N, W, H = self.nfft, self.wfft, self.nhop
     R = 1.0 if R is None else R
     dphi = P.atleast_2d((2 * P.pi * H * P.arange(N / 2 + 1)) / N).T
     print("Phase Vocoder Resynthesis...", N, W, H, R)
     A = P.angle(self.STFT) if Phi_hat is None else Phi_hat
     U = P.diff(A, 1) - dphi
     U = U - P.np.round(U / (2 * P.pi)) * 2 * P.pi
     t = P.arange(0, n_cols, R)
     tf = t - P.floor(t)
     phs = P.c_[A[:, 0], U]
     phs += U[:, idx[1]] + dphi  # Problem, what is idx ?
     Xh = (1 - tf) * Xh[:-1] + tf * Xh[1:]
     Xh *= P.exp(1j * phs)
     self.X_hat = Xh
def main(argv=None):
    if argv is None:
        argv = sys.argv
    if len(argv) != 4:
        print "Usage: " + argv[0] + " <report name> <report dir> <graph dir>"
        return 1

    report = argv[1]
    report_dir = argv[2]
    graph_dir = argv[3]

    file_list = glob(os.path.join(report_dir, 'part*'))
    #Copy the raw data file to the graph_dir
    raw_file = os.path.join(graph_dir, report + '.tsv')
    shutil.copyfile(file_list[0], raw_file)

    #Process the file into a graph, ideally I would combine the two into one but for now I'll stick with two
    data_file = csv.DictReader(open(raw_file, 'rb'), fieldnames = ['hour', 'Requests', 'Bytes'], delimiter="\t")
    
    #Make an empty set will all the hours in it os if an hour is not in the data it will be 0
    length = 24
    requests_dict = {}
    for num in range(length):
        requests_dict['%0*d' % (2, num)] = (0, 0)

    #add the values we have to the dictionaries
    for row in data_file:
        requests_dict[row['hour']] = (int(row['Requests']), int(row['Bytes']))

    #Now get the lists for graphing in the right order
    requests = []
    num_bytes = []
    requests_lists = requests_dict.items()
    requests_lists.sort(key=lambda req: req[0])
    for req in requests_lists:
        requests.append(req[1][0])
        num_bytes.append(req[1][1])

    fig = pylab.figure(1)
    pos = pylab.arange(length) + .5
    pylab.bar(pos, requests[:length], aa=True, ecolor='r')
    pylab.ylabel('Requests')
    pylab.xlabel('Hour')
    pylab.title('Request per hour')
    pylab.grid(True)

    #Save the figure
    pylab.savefig(os.path.join(graph_dir, report + '_requests.pdf'), bbox_inches='tight', pad_inches=1)

    #bytes listed 
    fig = pylab.figure(2)
    pos = pylab.arange(length) + .5
    pylab.bar(pos, num_bytes[:length], aa=True, ecolor='r')
    pylab.ylabel('Bytes')
    pylab.xlabel('Hour')
    pylab.title('Bytes per hour')
    pylab.grid(True)

    #Save the figure
    pylab.savefig(os.path.join(graph_dir, report + '_bytes.pdf'), bbox_inches='tight', pad_inches=1)
def custom_modulation(fc, bits, add_first_bit):
    # this custom ASK modulation (amplitude-shift modulation) is for support
    # for CDMA, in this case: 0, -2, 2. Each number will have certain amplitude
    lenghtBits = len(bits)
    final_time = lenghtBits / 2
    global ts
    ts = pyl.arange(0, final_time, sampling_period)
    global lenghtChunk
    lenghtChunk = int(len(ts) / lenghtBits)
    global A
    A = []
    if (add_first_bit):
        # When add_first_bit is activated, a "2" bit is added
        # at the beginning of the data
        ts = pyl.arange(0, final_time + 0.5, sampling_period)
        for j in range(lenghtChunk):
            A.append(5)
    for i in range(lenghtBits):
        for j in range(lenghtChunk):
            A_for_Bit_i = 0
            if (bits[i] == 0):
                A_for_Bit_i = 0
            elif (bits[i] == 2):
                A_for_Bit_i = 5
            elif (bits[i] == -2):
                A_for_Bit_i = 1
            A.append(A_for_Bit_i)
    return A * pyl.sin(2.0 * pyl.pi * fc * ts)
Exemplo n.º 6
0
def main():
    pl.ion()  # interactive mode
    fig1 = pl.figure()
    ax1 = fig1.add_axes([0.1, 0.1, 0.8, 0.8])
    pl.draw()
    ax1.plot(pl.arange(5), [30, 40, 55, 80, 100])
    pl.draw()
    ax1.set_xlabel('x')
    ax1.set_ylabel('y')
    pl.draw()

    for label in ax1.get_xticklabels():
        label.set_color('red')
        pl.draw()

    for label in ax1.get_yticklabels():
        label.set_color('green')
    pl.draw()
    ax1.grid(True)
    pl.draw()
    ax1.patch.set_facecolor('yellow')
    pl.draw()

    fig2 = pl.figure()
    pl.draw()
    ax2 = fig2.add_axes([0.1, 0.1, 0.8, 0.8])
    pl.draw()
    t = pl.arange(0.0, 1.0, 0.01)
    s = pl.sin(2 * pl.pi * t)
    ax2.plot(t, s, color='blue', lw=2)
    pl.draw()
    print "done"
Exemplo n.º 7
0
  def drawimage(self):
    #Find the longest text on the x axis
    maxtextlen = 0
    for text in self.xdata:
      if len(text) > maxtextlen:
        maxtextlen = len(text)

    #Convert it to a proportion of the image
    bottomProportion = .1 + maxtextlen*.013
    heightProportion = .99 - bottomProportion

    #Set the size of the subplot big enough to handle times
    #[left, bottom, width, height] out of 1
    self.figure.add_axes([.125,bottomProportion,.825,heightProportion],'w')

    #Set the min/max of each axis
    ymin = sys.maxint
    ymax = -sys.maxint+1
    for value in self.ydata[0]:
      if value < ymin:
        ymin = value
      if value > ymax:
        ymax = value
          
    self.figure.get_current_axis().set_xlim([0,len(self.xdata)+1])
    self.figure.get_current_axis().set_ylim([math.floor(ymin),math.ceil(ymax)])
    self.figure.get_current_axis().set_xticks(matlab.arange(len(self.xdata))+0.25)
    self.figure.get_current_axis().set_xticklabels(self.xdata,rotation='vertical')

    originY = None
    if ymin < 0 and ymax > 0:
      originY = 0
    self.figure.get_current_axis().bar(matlab.arange(len(self.xdata)),self.ydata[0],0.5,color=self.colours,originY=originY)
Exemplo n.º 8
0
    def prec_rec(ranks):
        """
        ::

            Return precision and recall arrays for ranks array data    
        """
        P = (1.0 + pylab.arange(pylab.size(ranks))) / (1.0 + pylab.sort(ranks))
        R = (1.0 + pylab.arange(pylab.size(ranks))) / pylab.size(ranks)
        return P, R
Exemplo n.º 9
0
    def insert_feature_files(featureList,
                             powerList,
                             keyList,
                             dbName,
                             delta_time=None,
                             undo_log10=False):
        """
        ::

            Walk the list of features, powers, keys, and, optionally, times, and insert into database
        """

        if delta_time == None:
            delta_time = 0.0
        db = adb.get(dbName, "w")

        if not db:
            print("Could not open database: %s" % dbName)
            return False
        # FIXME: need to test if KEY (%i) already exists in db
        # Support for removing keys via include/exclude keys
        for feat, pwr, key in zip(featureList, powerList, keyList):
            print("Processing features: %s" % key)
            F = adb.read(feat)
            P = adb.read(pwr)
            a, b = F.shape

            if (len(P.shape) == 2):
                P = P.reshape(P.shape[0])

            if (len(P.shape) == 1):
                c = P.shape[0]
            else:
                print("Error: powers have incorrect shape={0}".format(P.shape))
                return None

            if a != c:
                F = F.T
                a, b = F.shape
                if a != c:
                    print(
                        "Error: powers and features different lengths powers={0}*, features={1},{2}*"
                        .format(c, a, b))
                    return None
            # raw features, power in Bels, and key
            if undo_log10:
                F = 10**F
            if delta_time != 0.0:
                T = pylab.c_[pylab.arange(0, a) * delta_time,
                             (pylab.arange(0, a) + 1) * delta_time].reshape(
                                 1, 2 * a).squeeze()
            else:
                T = None
            db.insert(featData=F, powerData=P, timesData=T, key=key)
        return db
Exemplo n.º 10
0
 def find_gt_ranks(self, out_ranks, ground_truth_keys=None):
     """
     ::
     
         Return ranks matrix for ground-truth columns only
     """
     r = out_ranks.argsort()
     lzt_keys, lzt_len = self.get_adb_lists()
     gt_idx = [lzt_keys.index(s) for s in ground_truth_keys]
     ranks = pylab.zeros((len(gt_idx), len(gt_idx)))
     for i in pylab.arange(len(gt_idx)):
         for j in pylab.arange(len(gt_idx)):
             ranks[i][j] = pylab.nonzero(r[i] == gt_idx[j])[0][0]
     return ranks
Exemplo n.º 11
0
 def _make_dct(self):
     """
     ::
         Construct the discrete cosine transform coefficients for the
         current size of constant-Q transform
     """
     DCT_OFFSET = self.lcoef
     nm = 1 / P.sqrt(self._cqtN / 2.0)
     self.DCT = P.empty((self._dctN, self._cqtN))
     for i in P.arange(self._dctN):
         for j in P.arange(self._cqtN):
             self.DCT[i, j] = nm * P.cos(i * (2 * j + 1) *
                                         (P.pi / 2.0) / self._cqtN)
     for j in P.arange(self._cqtN):
         self.DCT[0, j] *= P.sqrt(2.0) / 2.0
def plot_prob_effector(sens, fpr, xmax=1, baserate=0.1):
    """Plots a line graph of P(effector|positive test) against
    the baserate of effectors in the input set to the classifier.
        
    The baserate argument draws an annotation arrow
    indicating P(pos|+ve) at that baserate
    """
    assert 0.1 <= xmax <= 1, "Max x axis value must be in range [0,1]"
    assert 0.01 <= baserate <= 1, "Baserate annotation must be in range [0,1]"
    baserates = pylab.arange(0, 1.05, xmax * 0.005)
    probs = [p_correct_given_pos(sens, fpr, b) for b in baserates]
    pylab.plot(baserates, probs, 'r')
    pylab.title("P(eff|pos) vs baserate; sens: %.2f, fpr: %.2f" % (sens, fpr))
    pylab.ylabel("P(effector|positive)")
    pylab.xlabel("effector baserate")
    pylab.xlim(0, xmax)
    pylab.ylim(0, 1)
    # Add annotation arrow
    xpos, ypos = (baserate, p_correct_given_pos(sens, fpr, baserate))
    if baserate < xmax:
        if xpos > 0.7 * xmax:
            xtextpos = 0.05 * xmax
        else:
            xtextpos = xpos + (xmax - xpos) / 5.
        if ypos > 0.5:
            ytextpos = ypos - 0.05
        else:
            ytextpos = ypos + 0.05
        pylab.annotate('baserate: %.2f, P(pos|+ve): %.3f' % (xpos, ypos),
                       xy=(xpos, ypos),
                       xytext=(xtextpos, ytextpos),
                       arrowprops=dict(facecolor='black', shrink=0.05))
    else:
        pylab.text(0.05 * xmax, 0.95,
                   'baserate: %.2f, P(pos|+ve): %.3f' % (xpos, ypos))
Exemplo n.º 13
0
def noise(noise_fun=pylab.rand, **params):
    """
    ::

        Generate noise according to params dict
            params - parameter dict containing sr, and num_harmonics elements [None=default_noise_params()]
            noise_fun - the noise generating function [pylab.rand]
    """
    params = _check_noise_params(**params)
    noise_dB = params['noise_dB']
    num_harmonics = params['num_harmonics']
    num_points = params['num_points']
    cf = params['cf']
    bw = params['bw']
    sr = params['sr']
    g = 10**(noise_dB / 20.0) * noise_fun(num_points)
    [b, a] = scipy.signal.filter_design.butter(4,
                                               bw * 2 * pylab.pi / sr,
                                               btype='low',
                                               analog=0,
                                               output='ba')
    g = scipy.signal.lfilter(b, a, g)
    # Phase modulation with *filtered* noise (side-band modulation should be narrow-band at bw)
    x = pylab.sin((2.0 * pylab.pi * cf / sr) * pylab.arange(num_points) + g)
    return x
Exemplo n.º 14
0
    def __init__(self, contact_area_percent=50.0):

        ######################################
        # begin: parameters to be specified

        self.contact_area_percent = contact_area_percent

        # resistor that is in series with the taxel (Ohms)
        self.r1 = 47.0

        # total voltage across the taxel and r1, which are in serise (Volts)
        self.vtot = 5.0

        # the maximum resistance of the taxel when no pressure is applied (Ohms)
        self.rtax_max = 50.0

        # the minimum force that will be applied to the taxel (Newtons)
        self.fz_min = 0.0

        # the maximum force that will be applied to the taxel (Newtons)
        self.fz_max = 45.0

        # the number of bits for the analog to digital conversion
        self.adc_bits = 10

        # the pressure sensitive area of the taxel (meters^2)
        self.taxel_area = 0.04 * 0.04

        # pressure that results in minimum resistance after which
        # further pressure does not result in a reduction in the
        # signal, since the sensor is saturated (Pascals = N/m^2)
        self.pressure_max = self.fz_max / (0.4 * self.taxel_area)

        # hack to specify the minimum resistance of the taxel, which
        # is associated with the maximum pressure. for now, it's
        # specified as a percentage of the maximum resistance, which
        # is associated with 0 applied pressure (no contact)
        self.r_min_percent_of_r_no_contact = 0.001  #

        # end
        ######################################

        self.r_no_contact = self.taxel_area * self.rtax_max
        self.r_min = self.r_no_contact * (self.r_min_percent_of_r_no_contact /
                                          100.0)
        self.fz_array = pl.arange(self.fz_min, self.fz_max, 0.001)  # N
        self.adc_range = pow(2.0, self.adc_bits)
        self.volts_per_adc_unit = self.vtot / self.adc_range  # V
        self.contact_area = self.taxel_area * (
            self.contact_area_percent / 100.0)  # m^2
        self.no_contact_area = self.taxel_area - self.contact_area  # m^2
        self.pressure_array = pl.array(
            [f / self.contact_area for f in self.fz_array])  # Pascals = N/m^2
        self.rtax_array = pl.array([self.rtax(f) for f in self.pressure_array])
        self.vdigi_array = pl.array(
            [self.output_voltage(r) for r in self.rtax_array])
        self.vdigi_max = self.output_voltage(self.rtax_max)
        self.adc_bias = self.vdigi_max / self.volts_per_adc_unit
        self.adc_array = self.vdigi_array / self.volts_per_adc_unit
        self.adc_plot = self.adc_bias - self.adc_array
Exemplo n.º 15
0
    def plot(self):
        self._logger.debug('plotting')
        colors = self._colors[:(len(self._categoryData))]
        ind = pylab.arange(len(self._xData))
        bar_width = 1.0 / (len(self._categoryData) + 1)
        bar_groups = []

        for c in range(len(self._categoryData)):
            bars = pylab.bar(ind + c * bar_width,
                             self._yData[c],
                             bar_width,
                             color=colors[c % len(colors)])
            bar_groups.append(bars)

        pylab.xticks(ind + bar_width, self._xData)
        if (self._usingLegend):
            pylab.legend((b[0] for b in bar_groups),
                         self._categoryData,
                         title=self._legendTitle,
                         loc=self._legendLocation,
                         labelspacing=self._legendLabelSpacing,
                         prop=self._legendFontProps,
                         bbox_to_anchor=self._legendBboxToAnchor)

        pylab.xlabel(self._xLabel, fontdict=self._font)
        pylab.ylabel(self._yLabel, fontdict=self._font)
        pylab.title(self._title, fontdict=self._font)
        if (self._saveFig):
            self._logger.debug('Saving plot as {}'.format(self._saveName))
            pylab.savefig(self._saveName)

        pylab.show()
Exemplo n.º 16
0
 def createZoneTransient(self):
     """make a dic of all times and values of each zone for each time"""
     lVar = self.Zones.keys() * 1
     dZone = {}
     tlist = []
     for var in lVar:  # make the list of times
         for z in self.getZoneList(var):
             if z.getForme() != 4 and type(z.getVal()) == type([5, 6]):  # if list of values : transient
                 slist = z.getVal()
                 for s in slist:
                     tlist.append(float(s.split(" ")[0]))
     # get the list from flow model
     t0 = self.model.getParm("Ecoulement", "Temps")  # final time, stress period l, nsteps
     tf = t0[0][1]
     per = t0[1][1]
     tf2 = arange(0.0, tf * 1.01, per)
     tflow = [round(x, 5) for x in tf2]
     tflow.extend(tlist)
     tlist = list(set(tflow))
     tlist.sort()
     # if tlist==[]: tlist=[100.] # cas permanent on met long 500
     dZone["tlist"] = tlist
     dZone["Transient"] = {}
     dZone["zlist"] = {}
     for var in lVar:  # loop into var to get transient values for each zone
         dZone["Transient"][var] = False
         dZone["zlist"][var] = []
         dZone[var] = None
         lZ = self.getZoneList(var)
         if len(lZ) == 0:
             continue
         tbl = zeros((len(tlist), len(lZ))) + 0.0
         for iz in range(len(lZ)):
             # get the list,
             slist = lZ[iz].getVal()
             form = lZ[iz].getForme()
             if type(slist) != type([5, 6]):
                 tbl[:, iz] = slist  # only one value
             elif form == 4:
                 tbl[:, iz] = slist[0]  # variable polygon take only 1st value
             else:  # a list of values : transient
                 vprec = 0
                 tl2 = []
                 vl2 = []
                 dZone["Transient"][var] = True
                 dZone["zlist"][var].append(iz)
                 for s in slist:
                     [a, b] = s.split()
                     tl2.append(float(a))
                     vl2.append(float(b))
                 for it in range(len(tlist)):
                     t = tlist[it]
                     if t in tl2:
                         v = vl2[tl2.index(t)]
                         tbl[it, iz] = v
                         vprec = v
                     else:
                         tbl[it, iz] = vprec
         dZone[var] = tbl
     self.zoneTrans = dZone
Exemplo n.º 17
0
def bar_chart(categories, xdata, ydata,
              title, xlabel, ylabel,
              font={'family':'serif','color':'black','weight':'normal','size':12,},
              plot=True, saveImage=False, imageName='fig.png'):

    colors = 'rgbcmyk'
    colors = colors[:(len(categories))]

    ind  = pylab.arange(len(xdata))
    bar_width = 1.0 / (len(categories) + 1)
    bar_groups = []

    # loop through categories and plot one bar in each category every loop (ie., one color at a time.)
    fig = pylab.figure()
    for c in range(len(categories)):
        bars = pylab.bar(ind+c*bar_width, ydata[c], bar_width, color=colors[c % len(colors)])
        bar_groups.append(bars)

    fontP = FontProperties()
    fontP.set_size('small')
    pylab.xticks(ind+bar_width, xdata)
    pylab.legend([b[0] for b in bar_groups], categories, 
                 loc='center right', title='Flow #', labelspacing=0,
                 prop=fontP, bbox_to_anchor=(1.125, .7))
    pylab.xlabel(xlabel, fontdict=font)
    pylab.ylabel(ylabel, fontdict=font)
    pylab.title(title, fontdict=font)

    # save the figure
    if saveImage:
        pylab.savefig(imageName)

    # plot the figure
    if plot:
        pylab.show()
Exemplo n.º 18
0
    def plot(self):
        self._logger.debug('plotting')
        colors = self._colors[:(len(self._categoryData))]
        ind = pylab.arange(len(self._xData))
        bar_width = 1.0 / (len(self._categoryData) + 1)
        bar_groups = []

        for c in range(len(self._categoryData)):
            bars = pylab.bar(ind+c*bar_width, self._yData[c], bar_width, color=colors[c % len(colors)])
            bar_groups.append(bars)

        pylab.xticks(ind+bar_width, self._xData)
        if (self._usingLegend):
            pylab.legend((b[0] for b in bar_groups), self._categoryData,
                         title = self._legendTitle, loc = self._legendLocation,
                         labelspacing = self._legendLabelSpacing, 
                         prop = self._legendFontProps, bbox_to_anchor = self._legendBboxToAnchor)

        pylab.xlabel(self._xLabel, fontdict=self._font)
        pylab.ylabel(self._yLabel, fontdict=self._font)
        pylab.title(self._title, fontdict=self._font)
        if(self._saveFig):
            self._logger.debug('Saving plot as {}'.format(self._saveName))
            pylab.savefig(self._saveName)

        pylab.show()
Exemplo n.º 19
0
def sol_tov(rho_center):
    rmin = dr
    rmax = 20000.0
    r = pylab.arange(rmin, rmax + dr, dr)
    m = pylab.zeros_like(r)
    P = pylab.zeros_like(r)
    m[0] = (4.0 / 3.0) * pi * rho_center * r[0]**3
    P[0] = eos(rho_center)
    y = pylab.array([P[0], m[0]])
    i = 0
    while P[i] > 0.0 and i < len(r) - 1:
        y = rk4(tov, y, r[i], dr)
        P[i + 1] = y[0]
        m[i + 1] = y[1]
        i = i + 1
    rho = pylab.array(list(map(lambda p: inv_eos(p), P)))

    m, r, rho, P = m[:i], r[:i], rho[:i], P[:i]  # Give restriction for region

    if isentropic:  # Consider the isentropic case
        # We use conventional finite differencing to handle this case
        drhodr = get_dfdr(rho, dr)
        rprime = (r[-1] - r)[::-1]  # Inward integration variable
        # Specific internal energy from thermo identities
        u_integrand = (-P * drhodr / (rho**2))[::-1]
        # Interpolation
        integrand_interpol = iterp1d(rprime, u_integrand, kind='cubic')
        urhs = lambda u, rprime: integrand_interpol(rprime)
        u = np.zeros_like(r)
        for i in range(0, len(rprime) - 1):
            u[i + 1] = rk4(urhs, u[i], rprime[i], dr)  # Using RK4 to integrate
            u = u[::-1]  # Reverse u
    else:
        u = np.zeros_like(r)
    return m, m[-1] / Msun, r, rho, P, u  # Return the mass and radius of star
Exemplo n.º 20
0
    def _chroma_hcqft(self):
        """
        ::

            Chromagram formed by high-pass liftering in cepstral domain, then usual self.nbpo-BPO folding.
        """
        fp = self._check_feature_params()
        if not self._hcqft():
            return False
        a, b = self.HCQFT.shape
        complete_octaves = a / self.nbpo  # integer division, number of complete octaves
        #complete_octave_bands = complete_octaves * self.nbpo
        # column-major ordering, like a spectrogram, is in FORTRAN order
        self.CHROMA = P.zeros((self.nbpo, b))
        for k in P.arange(complete_octaves):
            self.CHROMA += self.HCQFT[k * self.nbpo:(k + 1) * self.nbpo, :]
        self.CHROMA /= complete_octaves
        self._have_chroma = True
        if self.verbosity:
            print(
                "Extracted HCQFT CHROMA: lcoef=%d, ncoef=%d, intensified=%d" %
                (self.lcoef, self.ncoef, self.intensify))
        self.inverse = self.ichroma
        self.X = self.CHROMA
        return True
Exemplo n.º 21
0
def generateHist(data, bins = 20, rnge = None,
                       histogram = None):
    """ Generate an histogram of data
    
    :arg data: Array-like
    :arg bins: Number of bins
    :arg rnge: 2-tuple with min and max values,
               if None it is calculated from the data
    :arg histogram: Array-like updated histogram rather then create new one
                    If histogram is not None, also a range must be given
    """
    
    if histogram == None:
        histogram = [0]*bins
    else:
        if rnge == None:
            raise Exception("Histogram given but not range")
        bins = len(histogram)
    if rnge == None:
        rnge = [min(data),max(data)]
    delta_bin = 1.*(rnge[1] - rnge[0])/bins
    
    for d in data:
        if (d>=rnge[0]) and (d<rnge[1]):
            histogram[ int((d-rnge[0])/delta_bin) ] += 1
    return ( (pl.arange(bins)+ 0.5)*delta_bin + rnge[0]), histogram
Exemplo n.º 22
0
    def plotimx(self, im):
        if self.type == "polygon":
            for reg in ("ap", "bg0", "bg1"):
                ci = self.imcoords(im, reg=reg)
                pl.plot(ci[:, 0], ci[:, 1])
        elif self.type == "circle":
            n = 33
            t = pl.arange(n) * pl.pi * 2 / n
            ct = pl.cos(t)
            st = pl.sin(t)
            for reg in ("ap", "bg0", "bg1"):
                ci = self.imcoords(im, reg=reg)
                #print "center of circle= %f %f" % ci[0:2]
                r = ci[2]  # in pix
                pl.plot(ci[0] + r * ct, ci[1] + r * st)

            # point north:  XXX TODO make general
            from astropy import wcs
            w = wcs.WCS(im.header)
            # use origin=0 i.e. NOT FITS convention, but pl.imshow sets origin
            # to 0,0 so do that here so we can overplot on pl.imshow axes
            origin = 0
            c = self.bg1coords  # only works below for circle
            ctr = w.wcs_world2pix([c[0:2]], origin)[0]
            # north
            ctr2 = w.wcs_world2pix([c[0:2] + pl.array([c[2], 0])], origin)[0]
            pl.plot([ctr[0], ctr2[0]], [ctr[1], ctr2[1]])
Exemplo n.º 23
0
 def _stft(self):
     if not self._have_x:
         print(
             "Error: You need to load a sound file first: use self.load_audio('filename.wav')"
         )
         return False
     fp = self._check_feature_params()
     num_frames = len(self.x)
     self.STFT = P.zeros((self.nfft / 2 + 1, num_frames), dtype='complex')
     self.win = P.ones(self.wfft) if self.window == 'rect' else P.np.sqrt(
         P.hanning(self.wfft))
     x = P.zeros(self.wfft)
     buf_frames = 0
     for k, nex in enumerate(self.x):
         x = self._shift_insert(x, nex, self.nhop)
         if self.nhop >= self.wfft - k * self.nhop:  # align buffer on start of audio
             self.STFT[:, k - buf_frames] = P.rfft(self.win * x,
                                                   self.nfft).T
         else:
             buf_frames += 1
     self.STFT = self.STFT / self.nfft
     self._fftfrqs = P.arange(
         0, self.nfft / 2 + 1) * self.sample_rate / float(self.nfft)
     self._have_stft = True
     if self.verbosity:
         print("Extracted STFT: nfft=%d, hop=%d" % (self.nfft, self.nhop))
     self.inverse = self._istftm
     self.X = abs(self.STFT)
     if not self.magnitude:
         self.X = self.X**2
     return True
Exemplo n.º 24
0
 def plotPerB(self, figu, stats, classstats, f_num, c_num, name, li):
     ax = figu.add_subplot(1, 1, 1)
     ind = pylab.arange(c_num)
     width = 1
     i = 0
     maxi = 1
     ctags = []
     mytags = []
     # means
     means = self.defineAvg(stats, f_num, c_num)
     # cumul means
     cumul = self.disCumul(means)
     # plot bars
     self.barPlot(ax, ind, width, f_num, ctags, stats, i)
     # plot labels
     ax.set_title(name + '\n\n', fontstyle='normal', fontsize='12')
     # setting axis
     ax.axis([0, c_num, 0, maxi])
     # plot class names
     for c in range(c_num):
         mytags.append(c + (width / 2))
     ax.set_xticks(mytags)
     ax.set_xticklabels(ctags, rotation='75', fontsize='12')
     ax.grid(False)
     # set y axis label
     ax.set_ylabel("relative frequency", fontsize='12')
     self.plotLegend(ax, li)
def plot_prob_effector(sens, fpr, xmax=1, baserate=0.1):
    """Plots a line graph of P(effector|positive test) against
    the baserate of effectors in the input set to the classifier.
        
    The baserate argument draws an annotation arrow
    indicating P(pos|+ve) at that baserate
    """
    assert 0.1 <= xmax <= 1, "Max x axis value must be in range [0,1]"
    assert 0.01 <= baserate <= 1, "Baserate annotation must be in range [0,1]"
    baserates = pylab.arange(0, 1.05, xmax * 0.005)  
    probs = [p_correct_given_pos(sens, fpr, b) for b in baserates]
    pylab.plot(baserates, probs, 'r')
    pylab.title("P(eff|pos) vs baserate; sens: %.2f, fpr: %.2f" % (sens, fpr))
    pylab.ylabel("P(effector|positive)")
    pylab.xlabel("effector baserate")
    pylab.xlim(0, xmax)
    pylab.ylim(0, 1)
    # Add annotation arrow
    xpos, ypos = (baserate, p_correct_given_pos(sens, fpr, baserate))
    if baserate < xmax:
        if xpos > 0.7 * xmax:
            xtextpos = 0.05 * xmax
        else:
            xtextpos = xpos + (xmax-xpos)/5.
        if ypos > 0.5:
            ytextpos = ypos - 0.05
        else:
            ytextpos = ypos + 0.05
        pylab.annotate('baserate: %.2f, P(pos|+ve): %.3f' % (xpos, ypos), 
                       xy=(xpos, ypos), 
                       xytext=(xtextpos, ytextpos),
                       arrowprops=dict(facecolor='black', shrink=0.05))
    else:
        pylab.text(0.05 * xmax, 0.95, 'baserate: %.2f, P(pos|+ve): %.3f' %
                   (xpos, ypos))
Exemplo n.º 26
0
def sol_tov(rho_center):
    rmin = dr
    rmax = 2.e10

    r = pylab.arange(rmin, rmax + dr, dr)
    m = pylab.zeros_like(r)
    P = pylab.zeros_like(r)
    rho = pylab.zeros_like(r)

    i = 0
    rho[i] = rho_center
    m[i] = (4.0 / 3.0) * pi * dr**3
    P[i] = eos(rho_center, 0.0)

    y = pylab.array([P[i], m[i]])

    while P[i] > 0.0 and i < len(r) - 1:
        y = rk4(tov, y, r[i], dr, rho[i])
        m[i + 1] = y[1]
        P[i + 1] = y[0]
        rho[i + 1] = bisection(2 * rho[i], 0.1 * rho[i], 1.e-8, P[i + 1])
        i = i + 1

    if P[i] < 0.0:
        P[i] = 0.0
        rho[i] = 0.0

    m, r, rho, P = m[:i], r[:i], rho[:i], P[:i]  # Give restriction for region

    return m, m[-1] / Msun, r, rho, P  # Return the mass and radius of star
Exemplo n.º 27
0
def plmyfig(df, bgname, dirname, tar, count=10):
    #plot fig!
    print("Starting Plot %s %s" % (dirname, bgname))
    if len(df) > count:
        df = df.head(count)
    pos = plt.arange(len(df)) + 0.5
    ytick = _getTerm(df['Term_description'], df['Term_ID'], bgname)
    xs = [float(n) for n in df[' -log10(pvalue)']]
    ytick.reverse()
    xs.reverse()
    plt.barh(pos, xs, align = 'center', height = 0.5, alpha = 1, color='orange')
    plt.yticks(pos, ytick, size = 'x-small')
    plt.xlabel('$-Log10(pValue)$')
    plt.title('%s' % bgname)
    ax = plt.gca()
    ax.spines['right'].set_visible(False)
    ax.spines['top'].set_visible(False)
    ax.yaxis.set_ticks_position('left')
    ax.xaxis.set_ticks_position('bottom')
    try:
        plt.tight_layout()
    except ValueError:
        pass
    filename = os.path.join(tar, dirname, dirname + '_' + bgname)
    plt.savefig(filename + '.png', dpi = 72)
    plt.savefig(filename + '.pdf')
    plt.close()
Exemplo n.º 28
0
    def _chroma(self):
        """
        ::

            Chromagram, like 12-BPO CQFT modulo one octave. Energy is folded onto first octave.
        """
        fp = self._check_feature_params()
        lo = self.lo
        self.lo = 63.5444  # set to quarter tone below C
        if not self._cqft():
            return False
        self.lo = lo  # restore original lo edge
        a, b = self.CQFT.shape
        complete_octaves = a / self.nbpo  # integer division, number of complete octaves
        #complete_octave_bands = complete_octaves * self.nbpo
        # column-major ordering, like a spectrogram, is in FORTRAN order
        self.CHROMA = P.zeros((self.nbpo, b))
        for k in P.arange(complete_octaves):
            self.CHROMA += self.CQFT[k * self.nbpo:(k + 1) * self.nbpo, :]
        self.CHROMA = (self.CHROMA / complete_octaves)
        self._have_chroma = True
        if self.verbosity:
            print("Extracted CHROMA: intensified=%d" % self.intensify)
        self.inverse = self.ichroma
        self.X = self.CHROMA
        return True
Exemplo n.º 29
0
def plotTraces(): 
    tracesList = f.cfg['recordTraces'].keys()
    tracesList.sort()
    gidList = [trace for trace in f.cfg['plotCells'] if isinstance(trace, int)]
    popList = [trace for trace in f.cfg['plotCells'] if isinstance(trace, str)]
    if 'all' in popList:
        gidList = [cell['gid'] for cell in f.net.allCells]
        popList = []
    duration = f.cfg['duration']
    recordStep = f.cfg['recordStep']

    for gid in gidList:
        figure() # Open a new figure
        fontsiz = 12
        for itrace, trace in enumerate(tracesList):
            try:
                data = f.allSimData[trace]['cell_'+str(gid)]
                t = arange(0, duration+recordStep, recordStep)
                subplot(len(tracesList),1,itrace+1)
                plot(t[:len(data)], data, linewidth=1.5)
                xlabel('Time (ms)', fontsize=fontsiz)
                ylabel(trace, fontsize=fontsiz)
                xlim(0,f.cfg['duration'])
            except:
                pass
        if tracesList: subplot(len(tracesList),1,1)
        title('Cell %d'%(int(gid)))

    for popLabel in popList:
        fontsiz = 12
        for pop in f.net.pops:
            if pop.tags['popLabel'] == popLabel and pop.cellGids:
                figure() # Open a new figure
                gid = pop.cellGids[0] 
                for itrace, trace in enumerate(tracesList):
                    try:
                        data = f.allSimData[trace]['cell_'+str(gid)]
                        t = arange(0, len(data)*recordStep, recordStep)
                        subplot(len(tracesList),1,itrace+1)
                        plot(t, data, linewidth=1.5)
                        xlabel('Time (ms)', fontsize=fontsiz)
                        ylabel(trace, fontsize=fontsiz)
                        xlim(0,f.cfg['duration'])
                    except:
                        pass
        subplot(len(tracesList),1,1)
        title('Pop %s, Cell %d'%(popLabel, int(gid)))
Exemplo n.º 30
0
 def _phase_map(self):
     self.dphi = (2 * P.pi * self.nhop *
                  P.arange(self.nfft / 2 + 1)) / self.nfft
     A = P.diff(P.angle(self.STFT), 1)  # Complete Phase Map
     U = P.c_[P.angle(self.STFT[:, 0]), A - P.atleast_2d(self.dphi).T]
     U = U - P.np.round(U / (2 * P.pi)) * 2 * P.pi
     self.dPhi = U
     return U
Exemplo n.º 31
0
    def update_graph(self, val):
        freq = self.sfreq.val
        self.theta_max = freq*math.pi*2.0
        self.theta = pylab.arange(0.0, self.theta_max, self.theta_max/len(self.r))
        self.theta = self.theta[:len(self.r)]

        self.myplot.set_xdata(self.theta)
        draw()
Exemplo n.º 32
0
def movie_b(dataMatrix, showFrame = 0, nbrLoop = 1):
	
	matrixSize = dataMatrix.shape[1]
	ax = pl.subplot(111)	
	line, = pl.plot(dataMatrix[:,1])

	if showFrame:
		pl.title("Movie - Frame: " + str(1))

	for j in pl.arange(nbrLoop):
		for i in pl.arange(matrixSize):
		  	line.set_ydata(dataMatrix[:,i])
			maxValue = dataMatrix[:,i].max()
			ax.set_ylim((0,maxValue))
			if showFrame:
				pl.title("Movie - Frame: " + str(i))
				pl.draw()
Exemplo n.º 33
0
    def update_graph(self, val):
        freq = self.sfreq.val
        self.theta_max = freq * math.pi * 2.0
        self.theta = pylab.arange(0.0, self.theta_max,
                                  self.theta_max / len(self.r))
        self.theta = self.theta[:len(self.r)]

        self.myplot.set_xdata(self.theta)
        draw()
    def __init__(self, contact_area_percent=50.0):

        ######################################
        # begin: parameters to be specified

        self.contact_area_percent = contact_area_percent

        # resistor that is in series with the taxel (Ohms)
        self.r1 = 47.0 

        # total voltage across the taxel and r1, which are in serise (Volts)
        self.vtot = 5.0 

        # the maximum resistance of the taxel when no pressure is applied (Ohms)
        self.rtax_max = 50.0 

        # the minimum force that will be applied to the taxel (Newtons)
        self.fz_min = 0.0 

        # the maximum force that will be applied to the taxel (Newtons)
        self.fz_max = 45.0 

        # the number of bits for the analog to digital conversion 
        self.adc_bits = 10 

        # the pressure sensitive area of the taxel (meters^2)
        self.taxel_area = 0.04 * 0.04 

        # pressure that results in minimum resistance after which
        # further pressure does not result in a reduction in the
        # signal, since the sensor is saturated (Pascals = N/m^2)
        self.pressure_max = self.fz_max/(0.4 * self.taxel_area) 

        # hack to specify the minimum resistance of the taxel, which
        # is associated with the maximum pressure. for now, it's
        # specified as a percentage of the maximum resistance, which
        # is associated with 0 applied pressure (no contact)
        self.r_min_percent_of_r_no_contact = 0.001 #

        # end
        ######################################

        self.r_no_contact = self.taxel_area * self.rtax_max 
        self.r_min = self.r_no_contact * (self.r_min_percent_of_r_no_contact/100.0)
        self.fz_array = pl.arange(self.fz_min, self.fz_max, 0.001) # N
        self.adc_range = pow(2.0, self.adc_bits)
        self.volts_per_adc_unit = self.vtot/self.adc_range # V 
        self.contact_area = self.taxel_area * (self.contact_area_percent/100.0) # m^2
        self.no_contact_area = self.taxel_area - self.contact_area # m^2
        self.pressure_array = pl.array([f/self.contact_area for f in self.fz_array]) # Pascals = N/m^2
        self.rtax_array = pl.array([self.rtax(f) for f in self.pressure_array])
        self.vdigi_array = pl.array([self.output_voltage(r) for r in self.rtax_array])
        self.vdigi_max = self.output_voltage(self.rtax_max)
        self.adc_bias = self.vdigi_max/self.volts_per_adc_unit
        self.adc_array = self.vdigi_array/self.volts_per_adc_unit
        self.adc_plot = self.adc_bias - self.adc_array
Exemplo n.º 35
0
    def plotLog(self, figu, stats, classstats, f_num, c_num, name):
        ax2 = figu.add_subplot(1, 2, 2)
        myind = pylab.arange(c_num)
        ind = myind
        log = log10(ind + 1)
        logind = log
        # avg means
        fmeans = self.defineAvg2(stats, f_num, c_num)
        fmax = fmeans[len(fmeans) - 1]
        fmin = fmeans[0]
        print "###################################################"
        print fmax, "= max avg freq"
        print log10(fmax), "= max avg freq (log)"
        print fmin, "= min avg freq"
        print log10(fmin), "= min avg freq (log)"
        # avg cumul means
        fcumul = self.disCumul(fmeans)
        cumax = fcumul[len(fcumul) - 1]
        print "###################################################"
        print cumax, "= max avg cumul freq"
        print log10(cumax), "= max avg cumul freq (log)"
        # plot log powerlaw (cumul)
        ax2.plot(logind, self.powerLog(ind, fcumul)[1], '-', color='k')
        ax2.plot(logind, self.powerLog(ind, fmeans)[1], '--', color='k')
        ax2.plot(logind,
                 log10((scipy.array(fcumul) + 0.01) * 100),
                 'o',
                 color='k')
        ax2.plot(logind,
                 log10((scipy.array(fmeans) + 0.01) * 100),
                 'x',
                 color='k')
        # title
        ax2.set_title(name + ' (log-log best fit)\n\n',
                      fontstyle='normal',
                      fontsize='12')
        # setting axis
        ax2.axis([log10(c_num) + 0.5, -0.1, -0.1, log10(cumax) + 3.0])
        ax2.grid(False)
        ax2.set_xlabel("log rank", fontsize='12')
        ax2.set_ylabel("log frequency", fontsize='12')

        # make the y, x-axis ticks formatted to 1 decimal places
        ax2.xaxis.set_major_formatter(FormatStrFormatter('%.1f'))
        ax2.yaxis.set_major_formatter(FormatStrFormatter('%.1f'))

        # plot legend
        formc = self.powerLog(ind, fcumul)[0]
        formm = self.powerLog(ind, fmeans)[0]
        li = ('y=' + '%.2f' % formc['(sl,int)'][1] + "-" +
              '%.2f' % formc['(sl,int)'][0] + 'x, R2=' + '%.2f' % formc['R^2'],
              'y=' + '%.2f' % formm['(sl,int)'][1] + "-" +
              '%.2f' % formm['(sl,int)'][0] + 'x, R2=' + '%.2f' % formm['R^2'])
        self.plotLegend(ax2, li)
Exemplo n.º 36
0
def display():
    word_freqs = nltk.FreqDist(brown.words(categories="news")).most_common()
    words_by_freq = [w for (w, _) in word_freqs]
    cfd = nltk.ConditionalFreqDist(brown.tagged_words(categories="news"))
    sizes = 2 ** pylab.arange(15)
    perfs = [performance(cfd, words_by_freq[:size]) for size in sizes]
    pylab.plot(sizes, perfs, "-bo")
    pylab.title("Lookup Tagger Performance with Varying Model Size")
    pylab.xlabel("Model Size")
    pylab.ylabel("Performance")
    pylab.show()
Exemplo n.º 37
0
def makeMSFrame(dirname,msname,ra0,dec0,nchan):
  msstokes='RR LL';
  feedtype='perfect R L';


  ## Directory for the MS
  if(not os.path.exists(dirname)):
    cmd = 'mkdir ' + dirname;
    os.system(cmd);

  vx = [41.1100006,  -34.110001,  -268.309998,  439.410004,  -444.210022]
  vy = [3.51999998, 129.8300018,  +102.480003, -182.149994, -277.589996]
  vz = [0.25,       -0.439999998, -1.46000004, -3.77999997, -5.9000001]
  d = [25.0,       25.0,         25.0,         25.0,       25.0]
  an = ['VLA1','VLA2','VLA3','VLA4','VLA5'];
  nn = len(vx)*2.0;
  x = 0.5*(vx - (sum(pl.array(vx))/(nn)));
  y = 0.5*(vy - (sum(pl.array(vy))/(nn)));
  z = 0.5*(vz - (sum(pl.array(vz))/(nn)));

  ####  This call will get locations for all 27 vla antennas.
  #d, an, x, y, z = getAntLocations()


  obspos = me.observatory('EVLA');
  #obspos = me.position('ITRF', '-0.0m', '0.0m', '3553971.510m');

  ## Make MS Frame
  sm.open(ms=msname);
  sm.setconfig(telescopename='EVLA',x=x.tolist(),y=y.tolist(),z=z.tolist(),dishdiameter=d,
               mount=['alt-az'], antname=an,
               coordsystem='local',referencelocation=obspos);
  sm.setspwindow(spwname="CBand",freq="6.0GHz",deltafreq='500MHz',
                 freqresolution='2MHz',nchannels=nchan,stokes=msstokes);
  sm.setfeed(mode=feedtype,pol=['']);
  sm.setfield( sourcename="fake",sourcedirection=me.direction(rf='J2000',v0=ra0,v1=dec0) );
  sm.setlimits(shadowlimit=0.01, elevationlimit='10deg');
  sm.setauto(autocorrwt=0.0);
  sm.settimes(integrationtime='1800s', usehourangle=True,
                       referencetime=me.epoch('UTC','2013/05/10/00:00:00'));
  # Every 30 minutes, from -3h to +3h
  ostep = 0.5
  for loop in pl.arange(-3.0,+3.0,ostep):
    starttime = loop
    stoptime = starttime + ostep
    print starttime, stoptime
    for ch in range(0,nchan):
        sm.observe(sourcename="fake",spwname='CBand',
                   starttime=str(starttime)+'h', stoptime=str(stoptime)+'h');
  sm.close();

  listobs(vis=msname)

  return d
Exemplo n.º 38
0
def bar_chart(categories,
              xdata,
              ydata,
              title,
              xlabel,
              ylabel,
              font={
                  'family': 'serif',
                  'color': 'black',
                  'weight': 'normal',
                  'size': 12,
              },
              plot=True,
              saveImage=False,
              imageName='fig.png'):

    colors = 'rgbcmyk'
    colors = colors[:(len(categories))]

    ind = pylab.arange(len(xdata))
    bar_width = 1.0 / (len(categories) + 1)
    bar_groups = []

    # loop through categories and plot one bar in each category every loop (ie., one color at a time.)
    fig = pylab.figure()
    for c in range(len(categories)):
        bars = pylab.bar(ind + c * bar_width,
                         ydata[c],
                         bar_width,
                         color=colors[c % len(colors)])
        bar_groups.append(bars)

    fontP = FontProperties()
    fontP.set_size('small')
    pylab.xticks(ind + bar_width, xdata)
    pylab.legend([b[0] for b in bar_groups],
                 categories,
                 loc='center right',
                 title='Flow #',
                 labelspacing=0,
                 prop=fontP,
                 bbox_to_anchor=(1.125, .7))
    pylab.xlabel(xlabel, fontdict=font)
    pylab.ylabel(ylabel, fontdict=font)
    pylab.title(title, fontdict=font)

    # save the figure
    if saveImage:
        pylab.savefig(imageName)

    # plot the figure
    if plot:
        pylab.show()
Exemplo n.º 39
0
def campana(f0, A0, I0, duracion, fsampl):
    ts = pyl.arange(0, duracion, 1 / fsampl)  # creo el espacio temporal
    Tao = duracion / 3
    fc = f0
    fm = 2 * f0

    I_t = I0 * pyl.exp(-ts / Tao)  # para la campana
    A_t = A0 * pyl.exp(-ts / Tao)  # para la campana
    ym = pyl.sin(2 * pyl.pi * fm * ts)  # función modulada
    # Señal FM final con sus componentes que varían en el tiempo
    yc = A_t * pyl.sin(2 * pyl.pi * fc * ts + (I_t * ym))
    return yc
Exemplo n.º 40
0
  def getdata(self):
    chartdata = self.charttable.gettablerows(self.attribs.get('filter',None))
    xlabels = [str(row[self.xcolumn]) for row in chartdata]
    self.ydata = [[self.mapfromNone(row[ycolumn]) for row in chartdata] for ycolumn in self.ycolumns]
    self.legendlabels = [str(ycolumn) for ycolumn in self.ycolumns]

    #x axis tick labels should be uniformly distributed in this case
    startVal = self.attribs.get('startval', 0)
    endVal = self.attribs.get('endval', len(self.xdata))
    self.xvalues = matlab.arange(startVal, endVal, float(endVal - startVal)/len(xlabels))
    self.xMajorLocator = ticker.LinearLocator(self.attribs.get('numticks',10))
    self.xMajorFormatter = ticker.FixedFormatter(xlabels)
Exemplo n.º 41
0
def draw_snd(data, freq):

    timA = arange(0, len(data), 1)
    timA = timA / freq

    #plot
    #print(timA.shape)#

    pyplot.plot(timA, data, color='k', alpha=0.5)
    ylabel = ('Amp')
    xlabel('Time (ms)')
    show()
Exemplo n.º 42
0
    def plotCurve(self,figu,stats,c_num,name,pos):
        '''
        curve chart plotter
        
        feature x precision, recall, f-measure          
        '''
        
        ax      = figu.add_subplot(1,1,pos)
        ind     = pylab.arange(c_num)
        width   = 1         # 1cm per unit of length
        maxi    = 1         # y-axis ranges from 0 to 0.6
        
        # methods, annotators
        ctags   = stats[0]
        pre     = stats[1]
        rec     = stats[2]
        fme     = stats[3]
        acc     = stats[4]
        
        # line identifiers
        li = ("MetaMap","BabelFly","TagMe","WordNet")    
                              
        # plot 1
        ax.plot((ind+width/2),pre,'o--',# change line type
                color='r',linewidth=3,label="MetaMap") # change color

        # plot 1
        ax.plot((ind+width/2),rec,'x--',# change line type
                color='k',linewidth=3,label="BabelFly") # change color        
        
        # plot 2
        ax.plot((ind+width/2),fme,'x-',# change line type
                color='b',linewidth=1,label="TagMe")    # change color
        # plot 3
        ax.plot((ind+width/2),acc,'o-',# change line type
                color='g',linewidth=3,label="WordNet")  # change color
            
        ax.axis([0,c_num,0,maxi])
            
        # set name of y-axis      
        ax.set_ylabel(name+" ",fontsize='12')
        
        # plot feature names
        mytags = []
        for c in range(c_num):
            mytags.append(c+(width/2))
        ax.set_xticks(mytags)
        ax.set_xticklabels(ctags,rotation='45',fontsize='12')
        ax.grid(False)
     
        # plot legend
        self.plotLegend(ax,li)
Exemplo n.º 43
0
def sinusoid(**params):
    """
    ::

        Generate a sinusoidal audio signal from signal_params: see default_signal_params() 
          **params - signal_params dict, see default_signal_params()

    """
    params = _check_signal_params(**params)
    t = pylab.arange(params['num_points'])
    x = pylab.sin(TWO_PI * params['f0'] / params['sr'] * t +
                  params['phase_offset'])
    return x
Exemplo n.º 44
0
    def _cqft_intensified(self):
        """
        ::

            Constant-Q Fourier transform using only max abs(STFT) value in each band
        """
        if not self._have_stft:
            if not self._stft():
                return False
        self._make_log_freq_map()
        r, b = self.Q.shape
        b, c = self.STFT.shape
        self.CQFT = P.zeros((r, c))
        for i in P.arange(r):
            for j in P.arange(c):
                self.CQFT[i, j] = (self.Q[i, :] *
                                   P.absolute(self.STFT[:, j])).max()
        self._have_cqft = True
        self._is_intensified = True
        self.inverse = self.icqft
        self.X = self.CQFT
        return True
Exemplo n.º 45
0
 def plotLog(self, figu, stats, classstats, f_num, c_num, name):
     ax2 = figu.add_subplot(1, 2, 2)
     myind = pylab.arange(c_num)
     ind = myind
     log = log10(ind + 1)
     logind = log
     width = 0.1
     i = 0
     ctags = []
     mytags = []
     # means
     fmeans = self.defineAvg2(stats, f_num, c_num)
     fmax = fmeans[len(fmeans) - 1]
     print fmax, "is max freq"
     # cumul means
     fcumul = self.disCumul(fmeans)
     cumax = log10(fcumul[len(fcumul) - 1])
     print "###################################################"
     print cumax, "is max cumul freq"
     # plot log powerlaw (cumul)
     ax2.plot(logind, self.powerLog(ind, fcumul)[1], '-', color='k')
     ax2.plot(logind, self.powerLog(ind, fmeans)[1], '--', color='k')
     ax2.plot(logind,
              log10((scipy.array(fcumul) + 0.01) * 100),
              'o',
              color='k')
     ax2.plot(logind,
              log10((scipy.array(fmeans) + 0.01) * 100),
              'x',
              color='k')
     # title
     ax2.set_title('log-log best fit (' + name + ')\n\n',
                   fontstyle='normal',
                   fontsize='12')
     # setting axis
     ax2.axis([1.05, -0.1, 0, 7])
     ax2.grid(False)
     # plot legend
     formc = self.powerLog(ind, fcumul)[0]
     formm = self.powerLog(ind, fmeans)[0]
     li = (
         'best fit (cum.), y = ' + '%.2f' % formc['(sl,int)'][1] + " - " +
         '%.2f' % formc['(sl,int)'][0] + 'x, r^2 = ' +
         '%.2f' % formc['R^2'],
         'best fit (incr.), y = ' + '%.2f' % formm['(sl,int)'][1] + " - " +
         '%.2f' % formm['(sl,int)'][0] + 'x, r^2 = ' +
         '%.2f' % formm['R^2'],
         #'averages (cum.)',
         #'averages (inc..)'
     )
     self.plotLegend(ax2, li)
Exemplo n.º 46
0
def plotSigmoid():
    t = plab.arange(-60.0, 60.3, 0.1)
    s = 1 / (1 + plab.exp(-t))
    ax = plab.subplot(211)
    ax.plot(t, s)
    ax.axis([-5, 5, 0, 1])
    plt.xlabel('x')
    plt.ylabel('Sigmoid(x)')
    ax = plab.subplot(212)
    ax.plot(t, s)
    ax.axis([-60, 60, 0, 1])
    plt.xlabel('x')
    plt.ylabel('Sigmoid(x)')
    plab.show()
Exemplo n.º 47
0
def movielog(dataMatrix, showFrame):
	
	matrixSize = dataMatrix.shape[1]
	ax = pl.subplot(111)	
	line, = pl.semilogy(dataMatrix[:,1])

	if showFrame:
		pl.title("Movie - Frame: " + str(1))

	for i in pl.arange(matrixSize):
	  	line.set_ydata(dataMatrix[:,i])
		ax.relim() 
		ax.autoscale_view()
		if showFrame:
			pl.title("Movie - Frame: " + str(i))
    		pl.draw()
Exemplo n.º 48
0
 def plotCurve(self,figu,stats,c_num,name,pos):
     
     ax      = figu.add_subplot(1,1,pos)
     ind     = pylab.arange(c_num)
     width   = 1         # 1cm per unit of length
     maxi    = 0.5      # y-axis ranges from 0 to 0.5
     
     # methods, annotators
     ctags    = stats[0]
     mmap     = stats[1] # MMap
     bfly     = stats[2] # BFly
     tagm     = stats[3] # TGMe
     wnet     = stats[4] # Wnet
     
     # line identifiers
     li = ("MetaMap","BabelFly","TagMe","WordNet")    
             
     # plot 0
     ax.plot((ind+width/2),mmap,'x--',# change line type
             color='k',linewidth=3,label=li[3]) # change color                              
     # plot 1
     ax.plot((ind+width/2),bfly,'o--',# change line type
             color='r',linewidth=3,label=li[2]) # change color
     # plot 2
     ax.plot((ind+width/2),tagm,'x-',# change line type
             color='b',linewidth=1,label=li[1])    # change color
     # plot 3
     ax.plot((ind+width/2),wnet,'o-',# change line type
             color='g',linewidth=3,label=li[0])  # change color
         
     ax.axis([0,c_num,0,maxi])
         
     # set name of y-axis
     ax.set_ylabel(name+" (avg.)",fontsize='12')
     
     # plot feature names
     mytags = []
     for c in range(c_num):
         mytags.append(c+(width/2))
     ax.set_xticks(mytags)
     ax.set_xticklabels(ctags,rotation='45',fontsize='12')
     ax.grid(False)
  
     # plot legend
     self.plotLegend(ax,li)
Exemplo n.º 49
0
def mat_plot(funclist,suplist,lab1=None,lab2=None,ftype='continuous'):
    """
    Procedure Name: mat_plot
    Purpose: Create a matplotlib plot of a random variable
    Arguments:  1. RVar: A random variable
                2. suplist: The support of the plot
    Output:     1. A plot of the random variable
    """
    # if the random variable is continuous, plot the function
    if ftype=='continuous':
        for i in range(len(funclist)):
            if funclist[i]=='0':
                continue
            if 'x' in funclist[i]:
                x=arange(suplist[i],suplist[i+1],0.01)
                s=eval(funclist[i])
                plot(x,s,linewidth=1.0,color='green')
            else:
                plot([suplist[i],suplist[i+1]],
                     [funclist[i],funclist[i]],
                      linewidth=1.0,color='green')
        if lab1=='idf':
            xlabel('s')
        else:
            xlabel('x')
        if lab1!=None:
            ylabel(lab1)
        if lab2!=None:
            title(lab2)
        grid(True)
    # If the random variable is discrete, plot the function
    if ftype=='discrete':
        plot(suplist,funclist,'ro')
        if lab1=='F-1(s)':
            xlabel('s')
        else:
            xlabel('x')
        if lab1!=None:
            ylabel(lab1)
        if lab2!=None:
            title(lab2)
        grid(True)
Exemplo n.º 50
0
def plot_mc_results(output, dz = 1):
    Z,N = output.shape
    x = dz*np.arange(Z)
    f, ax = pl.subplots(figsize=(12,4))
    ax.imshow(abs(output.T)**2, aspect='auto', interpolation='none', cmap='Reds')
    ax.set_xlabel('Propagation dx')
    f, [ax1, ax2] = pl.subplots(1,2, figsize=(12,3))
    
    ax1.plot(x,abs(output[:,N//2])**2)  # Plot few channels
    ax1.plot(x,abs(output[:,N//2-1])**2)
    ax1.plot(x,abs(output[:,N//2+1])**2)  # Plot few channels
    ax1.plot(x,abs(output[:,N//2-2])**2)
    ax1.plot(x,abs(output[:,N//2+2])**2)  # Plot few channels
    ax1.plot(x,abs(output[:,N//2-3])**2)
    ax1.plot(x,abs(output[:,N//2+3])**2)  # Plot few channels

    ax1.plot(x,np.sum(abs(output)**2,1), c = 'k', ls = '--') # Total Energy
    ax1.set_xlabel('Propagation dx')
    
    ax2.bar(pl.arange(N)-N/2,abs(output[-1])**2)
Exemplo n.º 51
0
 def bar_freq(self,fre,n,fig,k,i,id,li):
     words = fre.keys()[:n]
     freqs = []
     for word in words:
         freqs.append(fre.freq(word))
     ax = fig.add_subplot(k,1,i)
     ind = pylab.arange(len(freqs))
     width = 1
     ax.bar(ind,freqs,width,facecolor='gray')
     ax.set_title('Word relative frequencies (top ' + `n` + ' words)\n\n',
                  fontstyle='normal',fontsize='12')
     max = fre.freq(fre.max())
     ax.axis([0,len(words),0,max])
     tags = []
     for c in range(len(words)):
         tags.append(width+c)
     ax.set_xticks(tags)
     self.plotticks(ax,words)
     ax.grid(False)
     self.plotLegend(ax, li)
Exemplo n.º 52
0
def main(argv=None):
    if argv is None:
        argv = sys.argv
    if len(argv) != 4:
        print "Usage: " + argv[0] + " <report name> <report dir> <graph dir>"
        return 1

    report = argv[1]
    report_dir = argv[2]
    graph_dir = argv[3]

    file_list = glob(os.path.join(report_dir, 'part*'))
    #Copy the raw data file to the graph_dir
    raw_file = os.path.join(graph_dir, report + '.tsv')
    shutil.copyfile(file_list[0], raw_file)

    #Process the file into a graph, ideally I would combine the two into one but for now I'll stick with two
    data_file = csv.DictReader(open(raw_file, 'rb'), fieldnames = ['IP', 'Requests', 'Bytes'], delimiter="\t")
    ips = []
    requests = []
    num_bytes = []
    for row in data_file:
        ips.append(row['IP'])
        requests.append(int(row['Requests']))
        num_bytes.append(int(row['Bytes']))

    if len(ips) > 25:
        length = 25
    else:
        length = len(ips)

    fig = pylab.figure(1)
    pos = pylab.arange(length) + .5
    pylab.barh(pos, requests[:length], align='center', aa=True, ecolor='r')
    pylab.yticks(pos, ips[:length])
    pylab.xlabel('Requests')
    pylab.title('Top %d ips ordered by # of requests' % length)
    pylab.grid(True)

    #Save the figure
    pylab.savefig(os.path.join(graph_dir, report + '.pdf'), bbox_inches='tight', pad_inches=1)
Exemplo n.º 53
0
def prob_plot(Sample,Fitted,plot_type):
    """
    Procedure Name: prob_plot
    Purpose: Create a mat plot lib plot to compare sample distributions
                with theoretical models
    Arguments:  1. Sample: Data sample quantiles
                2. Model: Model quantiles
    Output:     1. A probability plot that compares data with a model
    """
    plot(Fitted,Sample,'ro')
    x=arange(min(min(Sample),min(Fitted)),
             max(max(Sample),max(Fitted)),0.01)
    s=x
    plot(x,s,linewidth=1.0,color='red')
    if plot_type=='QQ Plot':
        xlabel('Model Quantiles')
        ylabel('Sample Quantiles')
    elif plot_type=='PP Plot':
        xlabel('Model CDF')
        ylabel('Sample CDF')
    title(plot_type)
    grid(True)
Exemplo n.º 54
0
 def plotFigPerTrace(subGids):
     for itrace, trace in enumerate(tracesList):
         figs['_trace_'+str(trace)] = figure() # Open a new figure
         fontsiz = 12
         for igid, gid in enumerate(subGids):
             if 'cell_'+str(gid) in sim.allSimData[trace]:
                 data = sim.allSimData[trace]['cell_'+str(gid)][int(timeRange[0]/recordStep):int(timeRange[1]/recordStep)]
                 t = arange(timeRange[0], timeRange[1]+recordStep, recordStep)
                 tracesData.append({'t': t, 'cell_'+str(gid)+'_'+trace: data})
                 color = colorList[igid%len(colorList)]
                 if not overlay:
                     subplot(len(subGids),1,igid+1)
                     color = 'blue'
                     ylabel(trace, fontsize=fontsiz)
                 plot(t[:len(data)], data, linewidth=1.5, color=color, label='Cell %d, Pop %s '%(int(gid), gidPops[gid]))
                 xlabel('Time (ms)', fontsize=fontsiz)
                 xlim(timeRange)
                 title('Cell %d, Pop %s '%(int(gid), gidPops[gid]))
         if overlay:
             maxLabelLen = 10
             subplots_adjust(right=(0.9-0.012*maxLabelLen)) 
             legend(fontsize=fontsiz, bbox_to_anchor=(1.04, 1), loc=2, borderaxespad=0.)
Exemplo n.º 55
0
def main(argv=None):
    if argv is None:
        argv = sys.argv
    if len(argv) != 4:
        print "Usage: " + argv[0] + " <report name> <report dir> <graph dir>"
        return 1

    report = argv[1]
    report_dir = argv[2]
    graph_dir = argv[3]

    file_list = glob(os.path.join(report_dir, "part*"))
    # Copy the raw data file to the graph_dir
    raw_file = os.path.join(graph_dir, report + ".tsv")
    shutil.copyfile(file_list[0], raw_file)

    # Process the file into a graph, ideally I would combine the two into one but for now I'll stick with two
    data_file = csv.DictReader(open(raw_file, "rb"), fieldnames=["page", "avgTime"], delimiter="\t")
    pages = []
    avg_time = []
    for row in data_file:
        pages.append(row["page"])
        avg_time.append(int(row["avgTime"]))

    if len(pages) > 25:
        length = 25
    else:
        length = len(pages)

    fig = pylab.figure(1)
    pos = pylab.arange(length) + 0.5
    pylab.barh(pos, avg_time[:length], align="center", aa=True, ecolor="r")
    pylab.yticks(pos, pages[:length])
    pylab.xlabel("Average Time Taken")
    pylab.title("Top %d pages ordered by average time taken" % length)
    pylab.grid(True)

    # Save the figure
    pylab.savefig(os.path.join(graph_dir, report + ".pdf"), bbox_inches="tight", pad_inches=2)
    def plot_per_position_fragment_analysis(self, position, target_axis = None):
        from matplotlib import pylab

        position = int(position)
        
        if position < 0 or position >= self.selected_fragment_rmsds.shape[0]:
            raise ValueError("Invalid fragment position specified.")
            
        if target_axis is None:
            target_axis = pylab.gca()

        fragment_rmsds = numpy.sort(self.selected_fragment_rmsds[position], axis=-1)

        target_axis.set_title("Fragment %s rmsd distribution." % position)
        target_axis.hist(fragment_rmsds, bins=50, normed=True)
        target_axis.grid(False, axis="y")
        target_axis.set_xlabel("RMSD")
        target_axis = target_axis.twinx()
        target_axis.set_yscale("symlog")
        target_axis.plot(fragment_rmsds, pylab.arange(len(fragment_rmsds)), label="Fragment count at RMSD.", color="red")

        target_axis.legend()
        
        return target_axis
Exemplo n.º 57
0
    def compute_initial_figure(self,dados):
        self.axes.grid(True)

        self.dias = dados
        d = []
        a = []
        for i in self.dias.keys():
            for j in self.dias.get(i).keys():
                a.append(self.dias.get(i).get(j))
            d.append( copy(a) )
            del a[:]

        
        data = d
        a = 1
        
        if (a==0):
            
                
    
            
            dim = len(data[0])
            w = 0.75
            dimw = w / dim
            
            x = pylab.arange(len(data))
            
            cor = ['r','y', 'b','p','c','w']
            g = [] 
            for i in range(len(data[0])) :
                y = [d[i] for d in data]
                g.append( self.axes.bar(x + i * dimw, y, dimw, color=cor[i], bottom=0.001) )
               
    
    
    #        self.axes.legend( (g[0][0],g[1][0],g[2][0]), ('Men', 'Women','Both') )
        else:
            
            
            
            t = range( len(data) )
            s = []
            
            
            def millions(x, t):
                'The two args are the value and tick position'
                return 'maria'
            
            formatter = FuncFormatter(millions)

            #self.axes.xaxis.set_major_formatter(formatter)

            
            
            #para cada um dos nutrientes
            for k in range(len(data[0])):
                #para cada um dos dias
                for a in range(len(data)):
                    s.append( data[ a ][k])
                    
                self.axes.plot(t, s)
                del s[:]
                
                
                
            w = 0.75
            
            x = pylab.arange(len(data))
            vb = self.dias.keys()
            self.axes.set_xticklabels(vb)
            vb = arange(len(vb))
            self.axes.set_xticks(vb)
            print x + w / 2
Exemplo n.º 58
0
import csv
import numpy as np
import matplotlib.pylab as plt

class_dict = {}
class_list = []
with open("data.txt", 'rb') as csvfile:
	spamreader = csv.reader(csvfile, delimiter=',')
	for row in spamreader:
		class_dict[ int (row[4]) ] = int (row[5])
		#class_list.append( row[4] )


print class_dict.keys()
print "###########################"
print class_dict
plt.xlim(0, len(class_dict)+1)
plt.ylim(0,4)
#x = np.linspace(0, 15, len(class_list))
plt.plot(class_dict.keys(),class_dict.values(), "gp")
x = plt.arange(100)
##plt.plot(20*plt.ones(100), x , 'ro', label = 'x = 10')
plt.show()



Exemplo n.º 59
0
def plot_data(data_without_orig,data_with_orig,title,x_axis,x_axis2,
              filename):
    
    data_without = []
    data_with = []
    for idx in range(len(data_without_orig)):
         
        avg = numpy.mean(data_without_orig[idx])
        data_without.append( data_without_orig[idx] / avg )
        data_with.append( data_with_orig[idx] / avg )
        
        
    index = numpy.arange(len(data_with))
      
    y_min = 1
    y_max = 1
    for row in data_without:
        if numpy.min(row) < y_min:
            y_min = numpy.min(row)
        if numpy.max(row) > y_max:
            y_max = numpy.max(row)
            
    for row in data_with:
        print numpy.min(row)
        if numpy.min(row) < y_min:
            y_min = numpy.min(row)
        if numpy.max(row) > y_max:
            y_max = numpy.max(row)    
                
    print (y_min,y_max)
    
    
    plt.figure()
    axes = plt.axes()
    
    
#     ax = fig.add_axes([0,len(data_without), y_min, y_max])
    
    plot_mean = axes.plot([numpy.average(x) for x in data_without],
               "-", 
               color="black",
               label='Uninstrumented Mean')
    
    axes.plot([numpy.max(x) for x in data_without],
             "+--", 
             color="black",
             label="Uninstrumented Max.")
    axes.plot([numpy.min(x) for x in data_without],
             ".--",
             color="black",
             label="Uninstrumented Min.")
     
    pylab.plot([numpy.mean(x) for x in data_with], "o")
     
    axes.errorbar(range(len(data_with)),
                   [numpy.mean(x) for x in data_with],
                   [numpy.std(x) for x in data_with],
                   fmt="o",
                   color="red",
                   label="With LO-PHI")
#     
    
#     axes.boxplot(data_with,
#                  sym='')
    
#     pylab.errorbar(range(len(read_data_with)),
#                    [numpy.mean(x) for x in data_without],
#                    [numpy.std(x) for x in data_without],
#                    fmt="k")

    plt.xlim(-1,len(x_axis))


    plt.title(title, fontsize=50)
    plt.ylabel("Normalized Throughput", fontsize=60)
    plt.xlabel("Total Size (KB) : Record Size(B)",labelpad=20, fontsize=60)
    
    plt.xticks(pylab.arange(len(x_axis)), x_axis, rotation=45)
#     

# #     axes2.spines['bottom']
    
#     axes.set_xticks(x_ticks,minor=False)
#     axes.set_xticklabels(x_axis)
#    axes.minorticks_on()
    plt.setp(axes)
    
    plt.tick_params(axis='x', which='major', labelsize=20)
    
    
    for key in x_axis2:
        plt.annotate(key, (x_axis2[key],0), (8, -25), 
                 xycoords='axes fraction', 
                 textcoords='offset points', 
                 va='top')
    
    plt.legend( 
               loc='upper right', 
               frameon=False,
               prop={'size':20})
    plt.show()