Ejemplo n.º 1
0
def main():
    with open('TDStep1.svg') as file:
        tdstep = benedict.from_xml(file.read())
    with open('PrimalGraphStep1.svg') as file:
        primal = benedict.from_xml(file.read())
    with open('IncidenceGraphStep1.svg') as file:
        incid = benedict.from_xml(file.read())

    result = append_svg(tdstep, incid)
    result = append_svg(result, primal)
    result['svg']['@preserveAspectRatio'] = "xMinYMin"
    with open("benedict.svg", "w") as file:
        result.to_xml(output=file)
Ejemplo n.º 2
0
 def test_scaleddown_join(self):
     """Scale larger to same size - compare to result."""
     with open(self.FILE1) as file1:
         im_1 = benedict.from_xml(file1.read())
         with open(self.FILE2) as file2:
             im_2 = benedict.from_xml(file2.read())
             result = append_svg(im_2, im_1, v_bottom=0, v_top=1)
             result['svg']['@preserveAspectRatio'] = "xMinYMin"
             # # to write:
             # with open('result_scaled_join.svg', 'w') as file:
             #     result.to_xml(output=file, pretty=True)
             with open(join(self.DIR, 'result_scaled_join.svg'),
                       'r') as expected:
                 self.assertEqual(result,
                                  benedict.from_xml(expected.read()))
Ejemplo n.º 3
0
 def test_simple_join_padding(self):
     """Test the horizontal padding - compare to result."""
     with open(self.FILE1) as file1:
         im_1 = benedict.from_xml(file1.read())
         with open(self.FILE2) as file2:
             im_2 = benedict.from_xml(file2.read())
             result = append_svg(im_2, im_1, centerpad=100)
             result['svg']['@preserveAspectRatio'] = "xMinYMin"
             # # to write:
             # with open('result_simple_join_padding.svg', 'w') as file:
             #     result.to_xml(output=file, pretty=True)
             with open(join(self.DIR, 'result_simple_join_padding.svg'),
                       'r') as expected:
                 self.assertEqual(result,
                                  benedict.from_xml(expected.read()))
Ejemplo n.º 4
0
 def test_simple_join(self):
     """Combine two example svg images to a new one - compare to result."""
     with open(self.FILE1) as file1:
         im_1 = benedict.from_xml(file1.read())
         with open(self.FILE2) as file2:
             im_2 = benedict.from_xml(file2.read())
             result = append_svg(im_1, im_2)
             result['svg']['@preserveAspectRatio'] = "xMinYMin"
             # # to write:
             # with open('result_simple_join.svg', 'w') as file:
             #     result.to_xml(output=file, pretty=True)
             with open(join(self.DIR, 'result_simple_join.svg'),
                       'r') as expected:
                 self.assertEqual(result,
                                  benedict.from_xml(expected.read()))
Ejemplo n.º 5
0
def test_append_svg_pinned(otherargs, filename, reverse, padding, write=WRITE):
    """Combine two example svg images - scaling with v_bottom and v_top."""
    filename += f"_padding{padding}_{reverse=}.svg"
    with open(FILE1) as file1:
        im_1 = benedict.from_xml(file1.read())
        with open(FILE2) as file2:
            im_2 = benedict.from_xml(file2.read())
            order = (im_2, im_1) if reverse else (im_1, im_2)
            result = append_svg(*order, centerpad=padding, **otherargs)

            result['svg']['@preserveAspectRatio'] = "xMinYMin"
            if write:
                makedirs(DIR, exist_ok=True)
                with open(join(DIR, filename), 'w') as outfile:
                    result.to_xml(output=outfile, pretty=True)
            with open(join(DIR, filename), 'r') as expected:
                assert result == benedict.from_xml(expected.read())
Ejemplo n.º 6
0
def test_append_svg_scaled(otherargs, filename, reverse, scale2, write=WRITE):
    """Combine two example svg images - different scalings for second image."""
    filename += f"_scale{int(scale2*10)}to10_{reverse=}.svg"
    with open(FILE1) as file1:
        im_1 = benedict.from_xml(file1.read())
        with open(FILE2) as file2:
            im_2 = benedict.from_xml(file2.read())
            order = (im_2, im_1) if reverse else (im_1, im_2)
            result = append_svg(*order, scale2=scale2, **otherargs)

            result['svg']['@preserveAspectRatio'] = "xMinYMin"
            if write:
                makedirs(DIR, exist_ok=True)
                with open(join(DIR, filename), 'w') as outfile:
                    result.to_xml(output=outfile, pretty=True)
            with open(join(DIR, filename), 'r') as expected:
                assert result == benedict.from_xml(expected.read())
Ejemplo n.º 7
0
 def test_centered_larger_join(self):
     """Center the smaller image - compare to result."""
     with open(self.FILE1) as file1:
         im_1 = benedict.from_xml(file1.read())
         with open(self.FILE2) as file2:
             im_2 = benedict.from_xml(file2.read())
             result = append_svg(im_2,
                                 im_1,
                                 v_bottom='center',
                                 v_top='center')
             result['svg']['@preserveAspectRatio'] = "xMinYMin"
             # # to write:
             # with open('result_centered_join2.svg', 'w') as file:
             #     result.to_xml(output=file, pretty=True)
             with open(join(self.DIR, 'result_centered_join2.svg'),
                       'r') as expected:
                 self.assertEqual(result,
                                  benedict.from_xml(expected.read()))
Ejemplo n.º 8
0
async def get_link(request):
    xml_stuff = await request.text()
    data = bdict.from_xml(xml_stuff)
    t_list = data['feed.entry.link']
    link = t_list["@href"]
    with open('downloaded.txt', 'r') as out:
        result = out.read()
        if link in result:
            return

    with open('downloaded.txt', 'a') as out:
        out.write("\n" + link)
    asyncio.create_task(download_youtube(link))
    return web.Response(text="NO")
Ejemplo n.º 9
0
def parse_xml(xml_dir):
    xml_files = glob.glob(xml_dir + "*.xml")
    for i in xml_files:
        print(i)
        with open(i, "r") as xml_file:
            contents = xml_file.read()
            data = bdict.from_xml(contents)
            out_data = data["annotation.object"]
            result = []
            if type(out_data) != type(list()):
                out_data = out_data["bndbox"]
                result.append(out_data)
            else:
                for j in out_data:
                    result.append(j["bndbox"])
            for region in result:
                new_name = i[:-3] + "jpg"
                with open("out.txt", "a") as out:
                    xmin = int(float(region["xmin"]))
                    ymin = int(float(region["ymin"]))
                    xmax = int(float(region["xmax"]))
                    ymax = int(float(region["ymax"]))
                    out.write("{},{},{},{},{},sugarcane\n".format(
                        new_name, xmin, ymin, xmax, ymax))
Ejemplo n.º 10
0
def svg_join(in_names: list,
             folder: str = '',
             num_images: int = 1,
             outname: str = 'combined',
             padding: int = 0,
             preserve_aspectratio: str = 'xMinYMin',
             suffix: str = '%d.svg',
             scale2: float = 1,
             v_top: Union[float, str] = 'top',
             v_bottom: Union[float, str] = None):
    """
    Joines different svg-images from tdvisu placed in 'folder' for every timestep
    in the horizontal order specified in 'in_names'.

    Parameters
    ----------
    in_names : list
        Base names of the images to join.
        The method appends the extra 'name'+'%d'+'.svg' to every name with the
        numbering for '%d' starting from 1.
    folder : str, optional
        The working directory. The default is the current directory.
    num_images : int, optional
        Expected maximum for 1<=i<=num_images. The default is only 1.
    outname : str, optional
        The base name for the combined svg. The default is "combined".
        The same timestep and ending '.svg' gets appended to the base name.
    padding : int, optional
        Additional padding in units between every two images. The default is 0.
    preserve_aspectratio : str, optional
        See https://developer.mozilla.org/en-US/docs/Web/SVG/Attribute/preserveAspectRatio.
        or https://css-tricks.com/scale-svg/#article-header-id-1 .
        The default is "xMinYMin".
    suffix : str, optional
        Change the prefix for each file. The default is "%d.svg".
    scale2 : float, optional
        Scale the second image. Only used if either v_bottom or v_top is None.
    v_bottom : float or str, optional
        Expected position of bottom of second image. The default is None.
    v_top : float or str, optional
        Expected position of bottom of second image. The default is 'top'.


    Returns
    -------
    None.

    """
    # names empty?
    if not in_names:
        LOGGER.warning("svg_join found no images to combine!")
        return
    # only one?
    if len(in_names) == 1:
        LOGGER.warning("svg_join called with one file - nothing to join!")
        return
    # could use path library for normalizing the path
    if folder:
        folder.replace('\\', '/')
        if not folder.endswith('/'):
            folder += '/'

    resultname = folder + outname + suffix
    names = [folder + name + suffix for name in in_names]

    for step in range(1, num_images + 1):
        # first - needs at least two images
        with open(names[0] % step) as file:
            im_1 = benedict.from_xml(file.read())
        with open(names[1] % step) as file:
            im_2 = benedict.from_xml(file.read())

        result = append_svg(im_1,
                            im_2,
                            padding,
                            v_bottom=v_bottom,
                            v_top=v_top,
                            scale2=scale2)

        # rest:
        for name in names[2:]:
            with open(name % step) as file:
                image = benedict.from_xml(file.read())
            result = append_svg(result,
                                image,
                                padding,
                                v_bottom=v_bottom,
                                v_top=v_top,
                                scale2=scale2)

        result['svg']['@preserveAspectRatio'] = preserve_aspectratio
        with open(resultname % step, 'w') as file:
            result.to_xml(output=file, pretty=True)
            LOGGER.info("Wrote combined: %s", resultname % step)
Ejemplo n.º 11
0
    def get_labels(self) -> 'Annotations':
        annotations_path = self.path.joinpath('annotations')
        annotations = []
        for img_xml in annotations_path.iterdir():
            picture_bboxes = []
            data = bdict.from_xml(img_xml.__str__())
            if len(data['annotation']['object']) != 6:
                for object in data['annotation']['object']:
                    picture_bboxes.append(
                        od.Annotation(
                            BBox(
                                int(object['bndbox']['xmin']),
                                int(object['bndbox']['ymin']),
                                int(object['bndbox']['xmax']) -
                                int(object['bndbox']['xmin']),
                                int(object['bndbox']['ymax']) -
                                int(object['bndbox']['ymin'])), 1,
                            'license_plate'))
                    #print(object)
            else:
                #print(data['annotation']['object']['bndbox']['xmin'])
                try:
                    picture_bboxes.append(
                        od.Annotation(
                            BBox(
                                int(data['annotation']['object']['bndbox']
                                    ['xmin']),
                                int(data['annotation']['object']['bndbox']
                                    ['ymin']),
                                int(data['annotation']['object']['bndbox']
                                    ['xmax']) -
                                int(data['annotation']['object']['bndbox']
                                    ['xmin']),
                                int(data['annotation']['object']['bndbox']
                                    ['ymax']) -
                                int(data['annotation']['object']['bndbox']
                                    ['ymin'])), 1, 'license_plate'))
                except:
                    for object in data['annotation']['object']:
                        picture_bboxes.append(
                            od.Annotation(
                                BBox(
                                    int(object['bndbox']['xmin']),
                                    int(object['bndbox']['ymin']),
                                    int(object['bndbox']['xmax']) -
                                    int(object['bndbox']['xmin']),
                                    int(object['bndbox']['ymax']) -
                                    int(object['bndbox']['ymin'])), 1,
                                'license_plate'))

            annotations.append(picture_bboxes)

        annos = dataset_merger.merging.DatasetMerger.create_dict_from_annotations_detected(
            self, annotations)
        with open(
                self.path.joinpath('Our_detections').joinpath(
                    'detections.json')) as file:
            detections = json.load(file)
        for index in range(len(detections['content'])):
            #add annootations

            for x in CarLicensePlates.find_anos(
                    annos, detections['content'][index]['file_name']):
                detections['content'][index]['annotations'].append(x)

        dataset_merger.merging.DatasetMerger.file_write('detections.json',
                                                        detections,
                                                        indent=4)

        #Add our annotations

        return detections
    def test_stackoverflow_question_58692636(self):
        """
        https://stackoverflow.com/questions/58692636/python-script-fails-to-extract-data-from-xml/58695393#58695393
        """
        from benedict import benedict as bdict

        data_xml = """
        <feed xml:base="http://data.treasury.gov/Feed.svc/">
          <title type="text">DailyTreasuryYieldCurveRateData</title>
          <id>
            http://data.treasury.gov/feed.svc/DailyTreasuryYieldCurveRateData
          </id>
          <updated>2019-11-04T07:15:32Z</updated>
          <link rel="self" title="DailyTreasuryYieldCurveRateData" href="DailyTreasuryYieldCurveRateData"/>
          <entry>
            <id>
              http://data.treasury.gov/Feed.svc/DailyTreasuryYieldCurveRateData(7258)
            </id>
            <title type="text"/>
            <updated>2019-11-04T07:15:32Z</updated>
            <author>
              <name/>
            </author>
            <link rel="edit" title="DailyTreasuryYieldCurveRateDatum" href="DailyTreasuryYieldCurveRateData(7258)"/>
            <category term="TreasuryDataWarehouseModel.DailyTreasuryYieldCurveRateDatum" scheme="http://schemas.microsoft.com/ado/2007/08/dataservices/scheme"/>
            <content type="application/xml">
              <m:properties>
                <d:Id m:type="Edm.Int32">7258</d:Id>
                <d:NEW_DATE m:type="Edm.DateTime">2019-01-02T00:00:00</d:NEW_DATE>
                <d:BC_1MONTH m:type="Edm.Double">2.4</d:BC_1MONTH>
                <d:BC_2MONTH m:type="Edm.Double">2.4</d:BC_2MONTH>
                <d:BC_3MONTH m:type="Edm.Double">2.42</d:BC_3MONTH>
                <d:BC_6MONTH m:type="Edm.Double">2.51</d:BC_6MONTH>
                <d:BC_1YEAR m:type="Edm.Double">2.6</d:BC_1YEAR>
                <d:BC_2YEAR m:type="Edm.Double">2.5</d:BC_2YEAR>
                <d:BC_3YEAR m:type="Edm.Double">2.47</d:BC_3YEAR>
                <d:BC_5YEAR m:type="Edm.Double">2.49</d:BC_5YEAR>
                <d:BC_7YEAR m:type="Edm.Double">2.56</d:BC_7YEAR>
                <d:BC_10YEAR m:type="Edm.Double">2.66</d:BC_10YEAR>
                <d:BC_20YEAR m:type="Edm.Double">2.83</d:BC_20YEAR>
                <d:BC_30YEAR m:type="Edm.Double">2.97</d:BC_30YEAR>
                <d:BC_30YEARDISPLAY m:type="Edm.Double">2.97</d:BC_30YEARDISPLAY>
              </m:properties>
            </content>
          </entry>
          <entry>
          <id>
            http://data.treasury.gov/Feed.svc/DailyTreasuryYieldCurveRateData(7259)
          </id>
          <title type="text"/>
          <updated>2019-11-04T07:15:32Z</updated>
          <author>
            <name/>
          </author>
          <link rel="edit" title="DailyTreasuryYieldCurveRateDatum" href="DailyTreasuryYieldCurveRateData(7259)"/>
          <category term="TreasuryDataWarehouseModel.DailyTreasuryYieldCurveRateDatum" scheme="http://schemas.microsoft.com/ado/2007/08/dataservices/scheme"/>
          <content type="application/xml">
            <m:properties>
              <d:Id m:type="Edm.Int32">7259</d:Id>
              <d:NEW_DATE m:type="Edm.DateTime">2019-01-03T00:00:00</d:NEW_DATE>
              <d:BC_1MONTH m:type="Edm.Double">2.42</d:BC_1MONTH>
              <d:BC_2MONTH m:type="Edm.Double">2.42</d:BC_2MONTH>
              <d:BC_3MONTH m:type="Edm.Double">2.41</d:BC_3MONTH>
              <d:BC_6MONTH m:type="Edm.Double">2.47</d:BC_6MONTH>
              <d:BC_1YEAR m:type="Edm.Double">2.5</d:BC_1YEAR>
              <d:BC_2YEAR m:type="Edm.Double">2.39</d:BC_2YEAR>
              <d:BC_3YEAR m:type="Edm.Double">2.35</d:BC_3YEAR>
              <d:BC_5YEAR m:type="Edm.Double">2.37</d:BC_5YEAR>
              <d:BC_7YEAR m:type="Edm.Double">2.44</d:BC_7YEAR>
              <d:BC_10YEAR m:type="Edm.Double">2.56</d:BC_10YEAR>
              <d:BC_20YEAR m:type="Edm.Double">2.75</d:BC_20YEAR>
              <d:BC_30YEAR m:type="Edm.Double">2.92</d:BC_30YEAR>
              <d:BC_30YEARDISPLAY m:type="Edm.Double">2.92</d:BC_30YEARDISPLAY>
            </m:properties>
          </content>
        </entry>
        <entry>
          <id>
            http://data.treasury.gov/Feed.svc/DailyTreasuryYieldCurveRateData(7260)
          </id>
          <title type="text"/>
          <updated>2019-11-04T07:15:32Z</updated>
          <author>
            <name/>
          </author>
          <link rel="edit" title="DailyTreasuryYieldCurveRateDatum" href="DailyTreasuryYieldCurveRateData(7260)"/>
          <category term="TreasuryDataWarehouseModel.DailyTreasuryYieldCurveRateDatum" scheme="http://schemas.microsoft.com/ado/2007/08/dataservices/scheme"/>
          <content type="application/xml">
             <m:properties>
               <d:Id m:type="Edm.Int32">7260</d:Id>
               <d:NEW_DATE m:type="Edm.DateTime">2019-01-04T00:00:00</d:NEW_DATE>
               <d:BC_1MONTH m:type="Edm.Double">2.4</d:BC_1MONTH>
               <d:BC_2MONTH m:type="Edm.Double">2.42</d:BC_2MONTH>
               <d:BC_3MONTH m:type="Edm.Double">2.42</d:BC_3MONTH>
               <d:BC_6MONTH m:type="Edm.Double">2.51</d:BC_6MONTH>
               <d:BC_1YEAR m:type="Edm.Double">2.57</d:BC_1YEAR>
               <d:BC_2YEAR m:type="Edm.Double">2.5</d:BC_2YEAR>
               <d:BC_3YEAR m:type="Edm.Double">2.47</d:BC_3YEAR>
               <d:BC_5YEAR m:type="Edm.Double">2.49</d:BC_5YEAR>
               <d:BC_7YEAR m:type="Edm.Double">2.56</d:BC_7YEAR>
               <d:BC_10YEAR m:type="Edm.Double">2.67</d:BC_10YEAR>
               <d:BC_20YEAR m:type="Edm.Double">2.83</d:BC_20YEAR>
               <d:BC_30YEAR m:type="Edm.Double">2.98</d:BC_30YEAR>
               <d:BC_30YEARDISPLAY m:type="Edm.Double">2.98</d:BC_30YEARDISPLAY>
             </m:properties>
           </content>
         </entry>
        </feed>
        """

        data = bdict.from_xml(data_xml)
        # print(data.dump())
        entries = data['feed.entry']
        for entry in entries:
            props = bdict(bdict(entry)['content.m:properties'])
            # print(props.dump())
            for key, value in props.items():
                # print(key, value['#text'])
                pass