Ejemplo n.º 1
0
def interpolation_search(sorted_list, to_find, variance):
    """
    Interpolation  search algorithm for determining the location of the
    point according to sorted_list, the sorted_list has a constant step
    and can thus give this algorithm the complexity of just ``O(log log(n))``

    :param list sorted_list: The sorted list to search in
    :param float to_find: The point to find
    :param float variance: A constant variance allowed for finding the point

    :rtype: float
    """
    low = 0
    high = len(sorted_list) - 1

    while sorted_list[low] <= to_find <= sorted_list[high]:
        mid = int(low + ((to_find - sorted_list[low]) * (high - low)) /
                  (sorted_list[high] - sorted_list[low]))

        if sorted_list[mid] < to_find:
            low = mid + 1
        elif sorted_list[mid] > to_find:
            high = mid - 1
        else:
            return mid
    t_var = variance
    while abs(sorted_list[low] - to_find) > variance:
        t_var += .001
    if variance != t_var:
        logger.warning(
            "interpolation variance expanded to %f to meet requirements" %
            variance)
    logger.debug("Interpolation low: %s", low)
    return low
Ejemplo n.º 2
0
    def fill_rectangle(self, event):
        """
        Informs the correct shape list's blank object to draw a rectangle to the screen
        using the provided coordinates

        :param event: A backend passed ``matplotlib.backend_bases.MouseEvent`` object
        """
        if self.__current_plot == Plot.baseplot:
            logger.warning("Cannot draw to BASE_PLOT")
            return
        if event.xdata and event.ydata:
            if len(self.__current_list[-1].get_coordinates()) is 0:
                return
            logger.debug('Filling: %d, %d' % (event.xdata, event.ydata))
            logger.info('Creating rectangle')
            self.__current_list[-1].fill_rectangle(event, self.__current_plot,
                                                   self.__hdf, self.__figure,
                                                   ShapeManager.outline_toggle)
            self.__current_list[-1].set_tag(self.generate_tag())
            self.__current_list.append(Shape(self.__canvas))
            self.__canvas.show()
        else:
            logger.error('Bounds out of plot range, skipping')
            self.__current_list[-1].set_coordinates([])
            self.__canvas._tkcanvas.delete(self.__current_list[-1].lastrect)
Ejemplo n.º 3
0
    def close_polygon(self, event, plot, fl, fig, fill=False):
        if len(self.__coordinates) > 3:
            index = self.__can_draw()
            if index > -1:
                logger.debug("Creating polygon from points")
                a1 = tuple_to_nparray(self.__coordinates[index])
                a2 = tuple_to_nparray(self.__coordinates[index + 1])
                b1 = tuple_to_nparray(self.__coordinates[-1])
                b2 = tuple_to_nparray(self.__coordinates[-2])
                x = get_intersection(a1, a2, b1, b2)
                pair = nparray_to_tuple(x)
                self.__coordinates[index] = pair

                del self.__coordinates[:index]
                self.__coordinates.pop()
                for line in self.__lines:
                    line.remove()
                self.__drawing_line.remove()
                self.__drawing_line = None
                self.__lines = []
                self.draw(fig, plot, fill)
                self.__plot = plot
                self.__hdf = fl
                return True
        else:
            logger.warning('Not enough points')
Ejemplo n.º 4
0
    def run(self, custom_classfy):
        """
        遍历config文件和音频路径,将其新平台文本格式信息写到output/result.txt文件中
        :param custom_classfy: 自定义分包条数,默认按spk音频数分包
        """
        logger.debug('遍历wav_name,text信息')
        wav_suf = 'wav'
        counter = 0
        wavs_info_map = {}

        for SPK, wav_name, content in self.get_info():  # 遍历说话人id,音频名,音频内容 信息
            logger.debug('[wav_info] %s - %s - %s ' % (SPK, wav_name, content))

            if wav_name not in self.wav_time_map:
                logger.warning('[%s] 未找到音频' % wav_name)
                continue

            wav_time = self.wav_time_map.get(wav_name)
            if not wav_time:
                continue

            wav_info = [  # 填充新平台文本格式
                {
                    "Wav_name": wav_name,
                    "Length_time": wav_time,
                    "Data": [
                        {
                            "text": content,
                            "start_time": 0,
                            "end_time": wav_time
                        }
                    ],
                    "Wav_suf": wav_suf
                }
            ]

            if custom_classfy:  # 指定分包数的模式
                id = f'line_{counter}'
                if id not in wavs_info_map:
                    wavs_info_map[id] = [wav_info]

                else:
                    if len(wavs_info_map[id]) == custom_classfy - 1:
                        counter += 1

                    wavs_info_map[id].append(wav_info)

            else:  # 默认分包模式
                if SPK not in wavs_info_map:
                    wavs_info_map[SPK] = [wav_info]
                else:
                    wavs_info_map[SPK].append(wav_info)

        logger.info(
            '处理完毕! 共 %s 批次 [%s]' % (len(wavs_info_map.keys()), wavs_info_map.keys()) if not custom_classfy else (
                counter, len(reduce(lambda x, y: x + y, wavs_info_map.values()))))

        with open(self._save_path, 'w', encoding='utf-8') as f:
            for key, value in wavs_info_map.items():
                f.write(json.dumps(value, ensure_ascii=False) + '\n')
Ejemplo n.º 5
0
 def __can_draw(self):
     if not self.__coordinates:
         logger.warning('Attempting to ask to draw empty shape, probably just ' + \
                        'toggling a button after using free draw? See ticket #92')
         return -1
     b1 = tuple_to_nparray(self.__coordinates[-1])
     b2 = tuple_to_nparray(self.__coordinates[-2])
     for i in range(len(self.__coordinates) - 3):
         a1 = tuple_to_nparray(self.__coordinates[i])
         a2 = tuple_to_nparray(self.__coordinates[i + 1])
         if is_intersecting(a1, a2, b1, b2):
             logger.debug("Polygon labeled for draw")
             return i
     return -1
Ejemplo n.º 6
0
    def properties(self, event):
        """
        Return the properties.rst of the shape clicked on by the user and create a small
        tooltip which displays these properties.rst

        :param event: A passed ``matplotlib.backend_bases.PickEvent`` object
        """
        target = event.artist
        logger.debug("Creating property window")
        for shape in self.__current_list:
            if shape.get_itemhandler() is target:
                # if self.property_window is not None:
                #    self.destroy_property_window()
                PropertyDialog(self.__master.get_root(), shape)
                return
        logger.warning("Shape not found")
Ejemplo n.º 7
0
def time_to_seconds(t):
    """
    Convert a time string into a strings containing only seconds

    :param str t: time in *%Y-%m-%d %H:%M:%S.%f* format
    :rtype: :py:class:`str`
    """
    t = str(t)
    t = t[:-6]
    t = datetime.strptime(t, '%Y-%m-%d %H:%M:%S.%f')
    ret = datetime.timedelta(hours=t.hour,
                             minutes=t.minute,
                             seconds=t.second,
                             microseconds=t.microsecond).total_seconds()
    logger.debug("Seconds %s", ret)
    return ret
Ejemplo n.º 8
0
    def set_current(self, plot, fig):
        """
        Set the current view to ``plot``, and draw any shapes that exist in the manager for
        this plot. This is called each time a new view is rendered to the screen by
        ``set_plot`` in *Calipso*

        :param int plot: Acceptable plot constant from ``constants.py``
        """
        logger.debug('Settings plot to %s' % plot)
        self.__figure = fig
        self.set_plot(plot)
        # Check if persistent shapes, use backscatter as the shapes list if so
        if CONF.persistent_shapes:
            self.__current_list = self.__shape_list[Plot.backscattered]
        if len(self.__current_list) > 1:
            logger.info('Redrawing shapes')
            for shape in self.__current_list[:-1]:
                if not shape.is_empty():
                    shape.loaded_draw(self.__figure,
                                      ShapeManager.outline_toggle)
            self.__canvas.show()
Ejemplo n.º 9
0
 def __str__(self):
     logger.debug('Stringing %s' % self.__tag)
     time_cords = [
         mpl.dates.num2date(x[0]).strftime('%H:%M:%S')
         for x in self.__coordinates
     ]
     altitude_cords = [x[1] for x in self.__coordinates]
     string = 'Name:\n\t%s\n' % self.__tag
     string += 'Time Scale:\n\t%s - %s\n' % (min(time_cords),
                                             max(time_cords))
     string += 'Latitude Scale:\n\t%s\n' % self.generate_lat_range()
     string += 'Altitude Scale:\n\t%.4f km - %.4f km\n' % (
         min(altitude_cords), max(altitude_cords))
     string += 'Color:\n\t%s\n' % self.__color
     if len(self.__attributes) > 0:
         string += 'Attributes:\n'
         for item in self.__attributes:
             string += '\t%s\n' % item
     if self.__note != '':
         string += 'Notes:\n\t%s' % self.__note
     return string
Ejemplo n.º 10
0
    def plot_point(self, event, plot, fl, fig, fill=False):
        """
        Plot a single point to the shape, connect any previous existing
        points and fill to a shape if the current coordinate intersects
        the beginning point.

        :param event: A ``matplotlib.backend_bases.MouseEvent`` passed object
        :param plot: an integer indicating which plot it was draw on
        :param fl: A string representing the HDF it was drawn on
        :param fig: The figure to be drawing the canvas to
        :param bool fill: Whether the shape will have a solid fill or not
        """
        self.__coordinates.append((event.xdata, event.ydata))
        logger.debug("Plotted point at (%0.5f, %0.5f)", event.xdata,
                     event.ydata)
        if len(self.__coordinates) > 1:
            logger.debug("Drawing line from plot")
            self.__lines.append(
                mlines.Line2D((self.__prev_x, event.xdata),
                              (self.__prev_y, event.ydata),
                              linewidth=2.0,
                              color='#000000'))
            fig.add_artist(self.__lines[-1])
            self.__canvas.show()

        if len(self.__coordinates) > 3:
            index = self.__can_draw()
            if index > -1:
                logger.debug("Creating polygon from points")
                a1 = tuple_to_nparray(self.__coordinates[index])
                a2 = tuple_to_nparray(self.__coordinates[index + 1])
                b1 = tuple_to_nparray(self.__coordinates[-1])
                b2 = tuple_to_nparray(self.__coordinates[-2])
                x = get_intersection(a1, a2, b1, b2)
                pair = nparray_to_tuple(x)
                self.__coordinates[index] = pair

                del self.__coordinates[:index]
                self.__coordinates.pop()
                for line in self.__lines:
                    line.remove()
                self.__drawing_line.remove()
                self.__drawing_line = None
                self.__lines = []
                self.draw(fig, fl, plot=plot, fill=fill)
                self.__plot = plot
                self.__hdf = fl
                return True

        self.__prev_x = event.xdata
        self.__prev_y = event.ydata
Ejemplo n.º 11
0
def acquire_time(wav_path):
    """
    获取音频时长
    :param wav_path: 音频路径
    :return: 音频时长
    """
    # if re.match('.*?sox.*', os.getenv('PATH')):                     # 判断环境变量里是否有sox
    cmd = 'sox --i -D %s' % wav_path
    logger.debug('[cmd]%s' % cmd)
    p = subprocess.Popen(
        cmd,  # 使用sox计算音频时长
        stdout=subprocess.PIPE,
        shell=True,
        stdin=subprocess.PIPE,
        stderr=subprocess.PIPE)
    out = p.stdout.read().decode()
    err = p.stderr.read().decode()

    if out and re.match('[0-9.]+', out) and not err:  # 判断sox是否成功
        logger.debug('[out] %s' % out)
        wav_time = float(out)
        return wav_time
    else:
        logger.debug('[err] %s' % err)

    logger.warning('[%s] 文件未能通过sox统计时长 ' % wav_path)
    try:
        with contextlib.closing(wave.open(wav_path, 'r')) as f:
            frames = f.getnframes()
            rate = f.getframerate()
            duration = frames / float(rate)
            return duration
    except Exception:
        pass
        # raise CustomError('[%s] 未能获取音频时长,请检查音频格式') from None
    return None
Ejemplo n.º 12
0
 def rebind_tools_to_canvas(self, new_canvas):
     logger.debug('Changing from ' + str(self.__canvas) + ' to ' +
                  str(new_canvas))
     self.__canvas = new_canvas
     self.setup_toolbar_buttons()
Ejemplo n.º 13
0
    def commit_to_db(self, poly_list, time):
        """
        Takes a list of polygons and commits them into the database,
        used in polygonList to commit all visible polygons

        :param poly_list: the current polygonList corresponding to the active plot
        :param time: time of the JSON's creation
        """
        logger.info('Committing to database')
        session = self.__Session()
        # for every polygon object in the list except the end
        for polygon in poly_list:
            # if the ID does not exist we have a new object to commit
            if polygon.get_id() is None:
                logger.debug('committing new shape: %s' % polygon.get_tag())
                cords = polygon.get_coordinates()
                time_cords = [mpl.dates.num2date(x[0]) for x in cords]
                altitude_cords = [x[1] for x in cords]

                f = polygon.get_hdf()
                blat = polygon.get_min_lat()
                elat = polygon.get_max_lat()
                btime = min(time_cords)
                etime = max(time_cords)
                balt = min(altitude_cords)
                ealt = max(altitude_cords)

                obx = \
                    DatabasePolygon(tag=polygon.get_tag(),
                                    time_=time,
                                    hdf=f.rpartition('/')[2],
                                    plot=PLOTS[polygon.get_plot()],
                                    color=polygon.get_color(),
                                    attributes=str(polygon.get_attributes()),
                                    coordinates=str(polygon.get_coordinates()),
                                    notes=polygon.get_notes(),
                                    begin_lat = blat,
                                    end_lat = elat,
                                    begin_time = btime,
                                    end_time = etime,
                                    begin_alt = balt,
                                    end_alt = ealt)
                session.add(obx)
                session.commit()
                polygon.set_id(obx.id)
            # otherwise we simply update the entries of the existing database object
            else:
                logger.debug('updating existing entry: %s' % polygon.get_tag())
                poly = session.query(DatabasePolygon).get(polygon.get_id())
                if poly is None:
                    logger.critical(
                        'This should never happen, why did it happen?')
                    continue
                poly.time_ = time
                f = polygon.get_hdf()
                poly.hdf = f.rpartition('/')[2]
                poly.plot = PLOTS[polygon.get_plot()]
                poly.color = unicode(polygon.get_color())
                poly.attributes = str(polygon.get_attributes())
                poly.coordinates = str(polygon.get_coordinates())
                poly.notes = polygon.get_notes()
                session.commit()
            if not polygon.get_saved():
                polygon.save()
        session.close()
Ejemplo n.º 14
0
 def send(self, params=None, data=None, **kwargs):
     response = self.session.request(method=self.method, url=self.url, params=params, data=data, **kwargs)
     response.encoding = 'utf-8'
     logger.debug('{0} {1}'.format(self.method, self.url))
     logger.debug('请求成功: {0}\n{1}'.format(response, response.text))
     return response
Ejemplo n.º 15
0
 def test_baidu_http(self):
     res = self.client.send()
     logger.debug(res.text)
     assertHTTPCode(res, [400])
     self.assertIn('百度一下,你就知道', res.text)