<commit_before>from net.wyun.mer.ink.stroke import Stroke
from net.wyun.mer.ink import scginkparser
import numpy as np
from net.wyun.mer.ink.stroke import get_bounding_box
from scipy import misc

class ScgImage(object):

    def __init__(self, scg_content, scg_id=1):
        self.id = scg_id
        self.content = scg_content
        #self.dummySample = Sample('data/inkml/65_alfonso.inkml')
        #self.w_h_ratio = 1.0 # initialize here, updated in replace_traces()
        self.traces = {}
        self.load_traces()

        (self.IMGxMIN, self.IMGyMIN, self.IMGxMAX, self.IMGyMAX) = get_bounding_box_h1000(self.traces)
        #self.dummySample.re_calculate_IMG_MINMAX()


    def load_traces(self):
        '''
        replace the traces in dummySample with the one generated from scg_content
        :return:
        '''
        strokes = scginkparser.parse_scg_ink_file(self.content, self.id)

        #for st in strokes:
            #print st

        traces = self.traces

        trace_id_int = 0
        for st in strokes:
            coords = np.zeros((2, len(st)))
            idx = 0
            for x_y in st:
                coords[:, idx] = [float(x_y[0]), float(x_y[1])]
                idx += 1
            traces[trace_id_int] = Stroke(trace_id_int, coords)
            trace_id_int += 1

        # //Compute bounding box of the input expression
        x_min, y_min, x_max, y_max = get_bounding_box(traces)  # bounding box for the whole math expression

        # Just in case there is only one point or a sequence of	points perfectly aligned with the x or y axis
        if x_max == x_min: x_max = x_min + 1;
        if y_max == y_min: y_max = y_min + 1;

        self.w_h_ratio = float(x_max - x_min) / (y_max - y_min)
        # Renormalize to height [0,10000] keeping the aspect ratio
        H = 10000.0
        W = H * (x_max - x_min) / (y_max - y_min)
        for trace_key, trace_v in traces.iteritems():
            trace_v.calc_coords_h10000(H, W, x_min, y_min, x_max, y_max)

        #self.dummySample.traces = traces

    def save_image(self, path):
        img, W, H = self.render()
        print 'save image to: ', path
        misc.imsave(path, img)

    def render(self, pW=0, pH=0):
        (xMIN, yMIN, xMAX, yMAX) = (self.IMGxMIN, self.IMGyMIN, self.IMGxMAX, self.IMGyMAX)
        # Image dimensions
        W = xMAX - xMIN + 1
        H = yMAX - yMIN + 1

        R = float(W*1.0/H)
        #//Keeping the aspect ratio (R), scale to 256 pixels height
        H = 80 #256
        W = int(H * R)
        if W <= 0: W = 1

        #Give some margin to the image
        W += 10<commit_msg>renormalize to height 100 instead of 10000. to avoid memory error in production<commit_after>from net.wyun.mer.ink.stroke import Stroke
from net.wyun.mer.ink import scginkparser
import numpy as np
from net.wyun.mer.ink.stroke import get_bounding_box
from scipy import misc

class ScgImage(object):

    def __init__(self, scg_content, scg_id=1):
        self.id = scg_id
        self.content = scg_content
        #self.dummySample = Sample('data/inkml/65_alfonso.inkml')
        #self.w_h_ratio = 1.0 # initialize here, updated in replace_traces()
        self.traces = {}
        self.load_traces()

        (self.IMGxMIN, self.IMGyMIN, self.IMGxMAX, self.IMGyMAX) = get_bounding_box_h1000(self.traces)
        #self.dummySample.re_calculate_IMG_MINMAX()


    def load_traces(self):
        '''
        replace the traces in dummySample with the one generated from scg_content
        :return:
        '''
        strokes = scginkparser.parse_scg_ink_file(self.content, self.id)

        #for st in strokes:
            #print st

        traces = self.traces

        trace_id_int = 0
        for st in strokes:
            coords = np.zeros((2, len(st)))
            idx = 0
            for x_y in st:
                coords[:, idx] = [float(x_y[0]), float(x_y[1])]
                idx += 1
            traces[trace_id_int] = Stroke(trace_id_int, coords)
            trace_id_int += 1

        # //Compute bounding box of the input expression
        x_min, y_min, x_max, y_max = get_bounding_box(traces)  # bounding box for the whole math expression

        # Just in case there is only one point or a sequence of	points perfectly aligned with the x or y axis
        if x_max == x_min: x_max = x_min + 1;
        if y_max == y_min: y_max = y_min + 1;

        self.w_h_ratio = float(x_max - x_min) / (y_max - y_min)
        # Renormalize to height [0,10000] keeping the aspect ratio
        H = 100.0
        W = H * (x_max - x_min) / (y_max - y_min)
        for trace_key, trace_v in traces.iteritems():
            trace_v.calc_coords_h10000(H, W, x_min, y_min, x_max, y_max)

        #self.dummySample.traces = traces

    def save_image(self, path):
        img, W, H = self.render()
        print 'save image to: ', path
        misc.imsave(path, img)

    def render(self, pW=0, pH=0):
        (xMIN, yMIN, xMAX, yMAX) = (self.IMGxMIN, self.IMGyMIN, self.IMGxMAX, self.IMGyMAX)
        # Image dimensions
        W = xMAX - xMIN + 1
        H = yMAX - yMIN + 1

        R = float(W*1.0/H)
        #//Keeping the aspect ratio (R), scale to 256 pixels height
        H = 80 #256
        W = int(H * R)
        if W <= 0: W = 1

        #Give some margin to the image
        W += 10
{'fraction_non_alphanumeric': 0.10086401202103681, 'fraction_numerical': 0.021036814425244178, 'mean_word_length': 2.09953434225844, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '748167', 'n_tokens_mistral': 1952, 'n_tokens_neox': 1821, 'n_words': 513}