Beispiel #1
0
def combine_coords(labels):
    """
    Combine polygon coordinates into 3 categories

    Parameters:
    -----------
    labels: dict
        a dictionary that contains polygon coordinates

    Returns:
    --------
    tmp_dict_str: str
        a string that contains a dictionary of polygon coordinates for each combined rock type
    """
    try:
        tmp_dict = {}
        for key in ['DW', 'CW', 'ORE']:
            tmp_dict[key] = []
        labels = str2dict(labels)
        for key in labels.keys():
            key_combined = combine_type_tier1(key)
            for item in labels[key]:
                tmp_dict[key_combined].append(item)
        tmp_dict_str = str(tmp_dict)
        return tmp_dict_str
    except:
        return np.nan
Beispiel #2
0
    def get_user_info(self, item):
        """
        获取用户信息
        :param item:
        :return:
        """
        data_be_str = item.div.get("data-bt")
        user_id = str(utils.str2dict(data_be_str)["id"])

        # 获取user homepage url
        user_info = item.find(class_=self.clearfix_flag)
        user_homepage_url = user_info.a.get("href")
        user_homepage_url = utils.get_homepage_url(user_homepage_url)

        # user_name_block = user_info.div.find(class_=self.clearfix_flag).find_all("div")
        # user_name_class_name = user_name_block[-1].a.get("class")[0]
        user_name = user_info.find(class_="_32mo").span.text

        about_items = user_info.find_all("div")
        about_class = about_items[11].find_all("div")

        try:
            about = about_class[5].text
        except:
            about = ""

        return [user_name, user_id, user_homepage_url, about]
Beispiel #3
0
def flat_all_coords(label_annot):
    """
    Gets a dictionary with polygons and flattens all the coordinates.

    Parameters:
    ----------
    label_annot: dict
        a dictionary with polygon name and coordinates.

    Returns:
    ---------
    flat_coords: list
        list of tuples of points (x, y) that are a coordinate
        of the different polygons in a json array.
    """
    # list_of_coord = []
    flat_coords = []

    try:
        label_annot = str2dict(label_annot)
        for value in label_annot.values():
            # print("value")
            for item in value:
                for tup in item:
                    flat_coords.append(tup)
        return flat_coords

    except:
        return np.nan
Beispiel #4
0
 def create_roughness_tbl(self):
     """
     Generate roughness statistic table
     """
     img_tbl = pd.read_csv(os.path.join(self.img_tbl_path,
                                        "img_tbl_w_labels.csv"),
                           index_col=0)
     num_cols = len(FEATS)
     feat_array = np.zeros((1, num_cols))
     # len(feat_array)
     for i in range(len(img_tbl)):
         # print(i)
         # return image array in Blue Green Red order
         img = img_tbl.iloc[i]["file_name"]
         coord_dict = str2dict(
             img_tbl.iloc[i]["labels"])  # convert string to dict
         # generate array that contains all the information for each subimage
         for key in coord_dict.keys():
             if len(coord_dict[key]) == 1:
                 item_list = []
                 # item_list.append(i) # image index
                 item_list.append(img)  # image id
                 item_list.append(key)  # rock type
                 try:
                     _, out = self.extract_aoi(img, coord_dict[key][0])
                     _, feats = self.get_roughness_feat(out)
                     for feat in feats:
                         item_list.append(feat)
                 except:
                     for j in range(9):  # number of roughness features
                         item_list.append(np.nan)
                 feat_array = np.append(feat_array, [item_list], axis=0)
             else:
                 for item in coord_dict[key]:
                     item_list = []
                     # item_list.append(i) # image index
                     item_list.append(img)  # image id
                     item_list.append(key)  # rock type
                     try:
                         _, out = self.extract_aoi(img, item)
                         _, feats = self.get_roughness_feat(out)
                         for feat in feats:
                             item_list.append(feat)
                     except:
                         for j in range(9):  # number of roughness features
                             item_list.append(np.nan)
                     feat_array = np.append(feat_array, [item_list], axis=0)
     feat_array = np.delete(feat_array, obj=0, axis=0)
     img_tbl_w_roughness = pd.DataFrame(feat_array, columns=FEATS)\
                             .dropna(axis=0)\
                             .reset_index(drop=True)
     img_tbl_w_roughness["CombinedType"] = img_tbl_w_roughness[
         "Type"].apply(self.combine_rock_types)
     img_tbl_w_roughness.to_csv(
         os.path.join(self.out_folder_path, "img_tbl_w_roughness.csv"))
Beispiel #5
0
def listaccounts(minconf=''):
    fname = 'btc/listaccounts'
    params = {'minconf':minconf}
    ret,res = get(fname,params)
    if not ret: print(res); return []
    if res.status_code != 200: return []
    res_dict = str2dict(res.text)
    if res_dict['success'] == 'false' or \
       'data' not in res_dict.keys():
        return []
    return list(list2dict(res_dict['data']).keys())
Beispiel #6
0
def getransactions(account='', count=10, skips=0):
    fname = 'btc/listtransactions'
    params = {
                'account':account,
                'count':count,
                'from':skips
             }
    ret,res = post(fname,params)
    if not ret: print(res); return []
    if res.status_code != 200: return []
    res_dict = str2dict(res.text)
    if res_dict['success'] == 'false' or \
       'data' not in res_dict.keys(): return []
    result = list2dict(res_dict['data'])
    return result if isinstance(result,list) else [result]
Beispiel #7
0
async def get_graph_data_dictionary(ctx, dataset_name: str):
    author = ctx.author
    graph_data_str = None
    #Recieve graph data in string format from db
    try:
        graph_data_str = dbfunc.get_dataset_graph_data(author.id, dataset_name)
    except:
        description = f"You don't have a dataset with the name `{dataset_name}`!"
        await ctx.send(embed=utils.error_embed(description))
        return None

    graph_data_dict = None
    try:
        graph_data_dict = utils.str2dict(graph_data_str)
    except:
        description = f"An error happened with the graph dictionary formatting on our end. Please use `/report` to report the issue or get help in our support server: {plotvars.support_discord_link}"
        await ctx.send(embed=utils.error_embed(description))
        return None

    return graph_data_dict
Beispiel #8
0
async def get_data_dictionary(ctx, dataset_name: str):
    author = ctx.author
    datastr = None
    #Recieve data in string format from db
    try:
        datastr = dbfunc.get_dataset_data(author.id, dataset_name)
    except:
        description = f"You don't have a dataset with the name `{dataset_name}`!"
        await ctx.send(embed=utils.error_embed(description))
        return None

    #Turn data in string format to dict format (this should only fail if the bot did something wrong)
    datadict = None
    try:
        datadict = utils.str2dict(datastr)
    except:
        description = f"An error happened with the dictionary formatting on our end. Please use `/report` to report the issue or get help in our support server: {plotvars.support_discord_link}"
        await ctx.send(embed=utils.error_embed(description))
        return None

    return datadict
Beispiel #9
0
    def get_user_info(self, item):
        data_be_str = item.div.get("data-bt")
        user_id = utils.str2dict(data_be_str)["id"]

        # 获取user homepage url
        user_info = item.find(class_=self.clearfix_flag)
        user_homepage_url = user_info.a.get("href")

        user_name_block = user_info.div.find(
            class_=self.clearfix_flag).find_all("div")
        # user_name_class_name = user_name_block[-1].a.get("class")[0]
        user_name = user_name_block[-1].a.text

        about_items = user_info.find_all("div")
        about_class = about_items[11].find_all("div")

        try:
            about = about_class[5].text
        except:
            about = None

        return [user_name, user_id, user_homepage_url, about]
Beispiel #10
0
    def draw_img_polys(self, index=1):
        """
        Draw polygons on a given image and corresponding polygon coordinates
        selected from the image summary table

        Parameters:
        -----------
        index: int
            image row index; Default value is 1
        """
        if not isinstance(index, int):
            raise TypeError("index must be of int type")

        self.img_tbl = pd.read_csv(os.path.join(self.img_tbl_path,
                                                "img_tbl_w_labels.csv"),
                                   index_col=0)
        nrows = len(self.img_tbl)
        if index > nrows - 1:
            raise ValueError("index should be less than number of rows")

        self.sample_imageid = self.img_tbl["file_name"][index]
        img = Image.open(
            os.path.join(self.img_folder_path, self.sample_imageid))
        self.sample_coords = str2dict(self.img_tbl["labels"][index])
        fnt = ImageFont.truetype('/Library/Fonts/Arial Bold Italic.ttf',
                                 size=52)
        draw = ImageDraw.Draw(img)
        for i in range(len(self.sample_coords.keys())):
            key = list(self.sample_coords.keys())[i]
            if not self.sample_coords[key]:
                pass
            elif len(self.sample_coords[key]) == 1:
                draw = ImageDraw.Draw(img, "RGBA")
                pts = self.sample_coords[key][0]
                draw.polygon(pts,
                             fill=(100 + i * 20, 100 - i * 20, 100 + i * 20,
                                   125))
                draw.text(get_tup_avg(self.sample_coords[key][0]),
                          key,
                          font=fnt,
                          fill=(255, 255, 255, 128))
                del draw
            else:
                for element in self.sample_coords[key]:
                    draw = ImageDraw.Draw(img, "RGBA")
                    draw.polygon(element,
                                 fill=(100 + i * 20, 100 - i * 20,
                                       100 + i * 20, 125))
                    draw.text(get_tup_avg(element),
                              key,
                              font=fnt,
                              fill=(255, 255, 255, 128))
                    del draw
        if os.path.exists(
                os.path.join(self.out_folder_path, "img_poly_example.jpg")):
            os.remove(
                os.path.join(self.out_folder_path, "img_poly_example.jpg"))
            img.save(os.path.join(self.out_folder_path,
                                  "img_poly_example.jpg"))
        else:
            img.save(os.path.join(self.out_folder_path,
                                  "img_poly_example.jpg"))