<commit_before>#!/usr/bin/python

from __future__ import division, print_function

import sys
import mrs
try:
    import numpy
except ImportError:
    import numpypy as numpy

try:
    range = xrange
except NameError:
    pass

class HaltonSequence(object):

    def __init__(self, start):
        self.index = start
        self.q0 = numpy.zeros(63, dtype='float64')
        self.d0 = numpy.zeros(63, dtype='int32')
        self.q1 = numpy.zeros(40, dtype='float64')
        self.d1 = numpy.zeros(40, dtype='int32')

        k = start
        self.x0 = 0.0
        for j in range(63):
            self.q0[j] = (1 if j == 0 else self.q0[j - 1]) / 2
            self.d0[j] = int(k % 2)
            k = (k - self.d0[j]) // 2
            self.x0 += self.d0[j] * self.q0[j]

        k = start
        self.x1 = 0.0
        for j in range(40):
            self.q1[j] = (1 if j == 0 else self.q1[j - 1]) / 3
            self.d1[j] = int(k % 3)
            k = (k - self.d1[j]) // 3
            self.x1 += self.d1[j] * self.q1[j]

    def next_point(self):
        self.index += 1
        for j in range(63):
            self.d0[j] += 1
            self.x0 += self.q0[j]
            if self.d0[j] < 2:
                break
            self.d0[j] = 0
            self.x0 -= 1 if j == 0 else self.q0[j - 1]

        for j in range(63):
            self.d1[j] += 1
            self.x1 += self.q1[j]
            if self.d1[j] < 3:
                break
            self.d1[j] = 0
            self.x1 -= 1 if j == 0 else self.q1[j - 1]
        return (self.x0, self.x1)

class SamplePi(mrs.MapReduce):
    def map(self, key, value):
        halton = HaltonSequence(value)
        inside = 0

        num_points = int(self.opts.num_points)
        for _ in range(num_points):
            point = halton.next_point()
            x = point[0] - .5
            y = point[1] - .5
            if x * x + y * y <= .25:
                inside += 1

        yield (True, inside)
        yield (False, num_points - inside)

    def reduce(self, key, values):
        yield sum(values)

    def run(self, job):
        points = int(self.opts.num_points)
        tasks = self.opts.num_tasks
        kvpairs = ((i, i * points) for i in range(tasks))
        source = job.local_data(kvpairs)

        intermediate = job.map_data(source, self.map)<commit_msg>Reduce attribute access in the pure_pi example<commit_after>#!/usr/bin/python

from __future__ import division, print_function

import sys
import mrs
try:
    import numpy
except ImportError:
    import numpypy as numpy

try:
    range = xrange
except NameError:
    pass

class HaltonSequence(object):

    def __init__(self, start):
        self.index = start
        q0 = numpy.zeros(63, dtype='float64')
        d0 = numpy.zeros(63, dtype='int32')
        q1 = numpy.zeros(40, dtype='float64')
        d1 = numpy.zeros(40, dtype='int32')

        k = start
        x0 = 0.0
        for j in range(63):
            q0[j] = (1 if j == 0 else q0[j - 1]) / 2
            d0[j] = int(k % 2)
            k = (k - d0[j]) // 2
            x0 += d0[j] * q0[j]

        k = start
        x1 = 0.0
        for j in range(40):
            q1[j] = (1 if j == 0 else q1[j - 1]) / 3
            d1[j] = int(k % 3)
            k = (k - d1[j]) // 3
            x1 += d1[j] * q1[j]

        self.x0, self.x1 = x0, x1
        self.d0, self.d1 = d0, d1
        self.q0, self.q1 = q0, q1

    def next_point(self):
        self.index += 1
        x0, x1 = self.x0, self.x1
        d0, d1 = self.d0, self.d1
        q0, q1 = self.q0, self.q1

        for j in range(63):
            d0[j] += 1
            x0 += q0[j]
            if d0[j] < 2:
                break
            d0[j] = 0
            x0 -= 1 if j == 0 else q0[j - 1]

        for j in range(63):
            d1[j] += 1
            x1 += q1[j]
            if d1[j] < 3:
                break
            d1[j] = 0
            x1 -= 1 if j == 0 else q1[j - 1]

        self.x0, self.x1 = x0, x1
        return (x0, x1)

class SamplePi(mrs.MapReduce):
    def map(self, key, value):
        halton = HaltonSequence(value)
        inside = 0

        num_points = int(self.opts.num_points)
        for _ in range(num_points):
            point = halton.next_point()
            x = point[0] - .5
            y = point[1] - .5
            if x * x + y * y <= .25:
                inside += 1

        yield (True, inside)
        yield (False, num_points - inside)

    def reduce(self, key, values):
        yield sum(values)

    def run(self, job):
        points = int(self.opts.num_points)
        tasks = self.opts.num_tasks
        kvpairs = ((i, i * points) for i in range(tasks))
        source = job.local_data(kvpairs)

        intermediate = job.map_data(source, self.map)
{'fraction_non_alphanumeric': 0.13214285714285715, 'fraction_numerical': 0.046638655462184875, 'mean_word_length': 1.4377880184331797, 'pattern_counts': {'":': 0, '<': 9, '<?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': '6114002', 'n_tokens_mistral': 1818, 'n_tokens_neox': 1707, 'n_words': 459}