Ejemplo n.º 1
0
class MapDataGeneration(object):
    
    
    def __init__(self):
        self.color = "235"
        self.lightness = "50%"
        self.saturation = "100%"
        self.count_generation = CountGeneration()
    
    def generate_map_data_array(self,collected_features_array,feature_name):
        feature_counts = self.count_generation.generate_counts(collected_features_array, feature_name) 

        max_count = self.get_max_count(feature_counts)
        map_data_array = []

        # yellow = Color("lime")
        # red = Color("red")
        # color_range = list(yellow.range_to(red, max_count))
        for label in feature_counts:
            feature_dir = {}
            #saturation = int(math.log(feature_counts[label]) / math.log(max_count) *100)

            #color = color_range[feature_counts[label]-1]
            ratio = float(feature_counts[label])/max_count
            color = self.get_color(ratio)
            hue = color.hsl[0] * 360
            feature_dir["color"] = "hsl(" + str(hue)+", "+ self.saturation+", "+self.lightness+")"
            feature_dir["count"] = str(feature_counts[label])
            feature_dir["label"] = str(pycountry.countries.get(alpha2=label).alpha3)
            
            map_data_array.append(feature_dir)

        return map_data_array

    def get_max_count(self,feature_counts):
        max_count = 0

        for label in feature_counts:
            if feature_counts[label] > max_count:
                max_count = feature_counts[label]

        return max_count
    
    # calculation based on: http://www.andrewnoske.com/wiki/Code_-_heatmaps_and_color_gradients
    def get_color(self, value):
        # blue rgb
        aR = 0
        aG = 0
        aB = 255
        
        # red rgb
        bR = 255
        bG = 0
        bB = 0
        
        red   = (float(bR - aR) * value + aR)/255
        green = (float(bG - aG) * value + aG)/255
        blue  = (float(bB - aB) * value + aB)/255

        return Color(rgb=(red,green,blue))

#import json
#
#with open("/srv/http/articles/de/Krimkrise/analysis.json") as json_file:
#    json_data = json.load(json_file)
#    print(json_data)
#    
#map_data_generation = MapDataGeneration()
#
#map_data_array = map_data_generation.generate_map_data_array(json_data, "classification-general")
#
#json_writer.write_json_file(map_data_array, "/srv/http/articles/de/Krimkrise/map-data.json")
Ejemplo n.º 2
0
class MapDataGeneration(object):
    def __init__(self):
        self.color = "235"
        self.lightness = "50%"
        self.saturation = "100%"
        self.count_generation = CountGeneration()

    def generate_map_data_array(self, collected_features_array, feature_name):
        feature_counts = self.count_generation.generate_counts(
            collected_features_array, feature_name)

        max_count = self.get_max_count(feature_counts)
        map_data_array = []

        # yellow = Color("lime")
        # red = Color("red")
        # color_range = list(yellow.range_to(red, max_count))
        for label in feature_counts:
            feature_dir = {}
            #saturation = int(math.log(feature_counts[label]) / math.log(max_count) *100)

            #color = color_range[feature_counts[label]-1]
            ratio = float(feature_counts[label]) / max_count
            color = self.get_color(ratio)
            hue = color.hsl[0] * 360
            feature_dir["color"] = "hsl(" + str(
                hue) + ", " + self.saturation + ", " + self.lightness + ")"
            feature_dir["count"] = str(feature_counts[label])
            feature_dir["label"] = str(
                pycountry.countries.get(alpha2=label).alpha3)

            map_data_array.append(feature_dir)

        return map_data_array

    def get_max_count(self, feature_counts):
        max_count = 0

        for label in feature_counts:
            if feature_counts[label] > max_count:
                max_count = feature_counts[label]

        return max_count

    # calculation based on: http://www.andrewnoske.com/wiki/Code_-_heatmaps_and_color_gradients
    def get_color(self, value):
        # blue rgb
        aR = 0
        aG = 0
        aB = 255

        # red rgb
        bR = 255
        bG = 0
        bB = 0

        red = (float(bR - aR) * value + aR) / 255
        green = (float(bG - aG) * value + aG) / 255
        blue = (float(bB - aB) * value + aB) / 255

        return Color(rgb=(red, green, blue))


#import json
#
#with open("/srv/http/articles/de/Krimkrise/analysis.json") as json_file:
#    json_data = json.load(json_file)
#    print(json_data)
#
#map_data_generation = MapDataGeneration()
#
#map_data_array = map_data_generation.generate_map_data_array(json_data, "classification-general")
#
#json_writer.write_json_file(map_data_array, "/srv/http/articles/de/Krimkrise/map-data.json")
Ejemplo n.º 3
0
        collected_features_with_fixed_outliers = article_extraction.fix_outliers(collected_features_with_fixed_outliers,"classification-general","classification-general-fixed",features)
        collected_features_array = article_extraction.get_as_array(collected_features_with_fixed_outliers)

        if len(collected_features_array) > 0:

            # generate directories if they don't exist
            if not os.path.exists(article_path):
                os.makedirs(article_path)
            if not os.path.exists(language_path):
                os.makedirs(language_path)

            json_writer.write_json_file(collected_features_array, article_analysis_path)

            count_features = ["ip-location","tld-location","website-language","classification-fixed","classification-general-fixed"]
            for count_feature in count_features:
                classification_general_counts = count_generation.generate_counts(collected_features_array, count_feature)
                classification_general_counts_array = count_generation.get_as_array(classification_general_counts, 20)

                article_count_path = os.path.join(article_path,"counts-"+count_feature+"-top-20.json")
                json_writer.write_json_file(classification_general_counts_array, article_count_path)

            # generate map data
            map_data = map_data_generation.generate_map_data_array(collected_features_array,"classification-general-fixed")
            article_map_data_path = os.path.join(article_path,"map-data.json")
            json_writer.write_json_file(map_data, article_map_data_path)

            # get execution date
            now = datetime.datetime.now()
            time_info = {}
            time_info["analysis-date"]= now.strftime("%Y-%m-%d")
            time_info["analysis-time"]= now.strftime("%H:%M:%S")