コード例 #1
0
class SlicerTestCase(unittest.TestCase):
    def setUp(self):
        self.w = Workspace()
        self.w.add_slicer("myslicer",
                          "http://localhost:5010",
                          username=os.environ.get("SLICER_USERNAME"),
                          password=os.environ.get("SLICER_PASSWORD"))

        self.cube_list = self.w.list_cubes()

    def first_date_dim(self, cube):
        for d in cube.dimensions:
            if (d.info.get('is_date')):
                return d
        raise BrowserError("No date dimension in cube %s" % cube.name)

    def test_basic(self):
        for c in self.cube_list:
            if c.get('category') is not None and 'Mix' in c.get(
                    'category', ''):
                continue
            print("Doing %s..." % c.get('name')),
            cube = self.w.cube(c.get('name'))
            date_dim = self.first_date_dim(cube)
            cut = cubes.browser.RangeCut(date_dim, [2013, 9, 25], None)
            cell = cubes.browser.Cell(cube, [cut])
            drill_levels = [
                l for l in date_dim.hierarchy().levels
                if l.name in ('day', 'date')
            ]
            if not drill_levels:
                print "Skipping cube %s with no day/date drilldown available." % c.get(
                    'name')
                continue
            drill = cubes.browser.Drilldown(
                [(date_dim, None, date_dim.level(drill_levels[0]))], cell)
            b = self.w.browser(cube)
            try:
                attr_dim = cube.dimension("attr")
                split = cubes.browser.PointCut(attr_dim, ['paid', 'pnb'])
            except:
                split = None
            try:
                kw = {}
                if cube.aggregates:
                    kw['aggregates'] = [cube.aggregates[0]]
                elif cube.measures:
                    kw['measures'] = [cube.measures[0]]
                else:
                    raise ValueError(
                        "Cube has neither aggregates nor measures")
                result = b.aggregate(cell, drilldown=drill, split=split, **kw)
                print result.cells
            except:
                traceback.print_exc()
コード例 #2
0
ファイル: test_slicer.py プロジェクト: 6si/cubes
class SlicerTestCase(unittest.TestCase):
    def setUp(self):
        self.w = Workspace()
        self.w.add_slicer("myslicer", "http://localhost:5010",
                          username=os.environ.get("SLICER_USERNAME"),
                          password=os.environ.get("SLICER_PASSWORD"))

        self.cube_list = self.w.list_cubes()

    def first_date_dim(self, cube):
        for d in cube.dimensions:
            if ( d.info.get('is_date') ):
                return d
        raise BrowserError("No date dimension in cube %s" % cube.name)

    def test_basic(self):
        for c in self.cube_list:
            if c.get('category') is not None and 'Mix' in c.get('category', ''):
                continue

            cube = self.w.cube(c.get('name'))
            date_dim = self.first_date_dim(cube)
            cut = cubes.browser.RangeCut(date_dim, [ 2013, 9, 25 ], None)
            cell = cubes.browser.Cell(cube, [ cut ])
            drill_levels = [ l for l in date_dim.hierarchy().levels if l.name in ('day', 'date') ]
            if not drill_levels:
                continue

            drill = cubes.browser.Drilldown([(date_dim, None, date_dim.level(drill_levels[0]))], cell)
            b = self.w.browser(cube)
            try:
                attr_dim = cube.dimension("attr")
                split = cubes.browser.PointCut(attr_dim, ['paid', 'pnb'])
            except:
                split = None
            try:
                kw = {}
                if cube.aggregates:
                    kw['aggregates'] = [cube.aggregates[0]]
                elif cube.measures:
                    kw['measures'] = [ cube.measures[0] ]
                else:
                    raise ValueError("Cube has neither aggregates nor measures")
                result = b.aggregate(cell, drilldown=drill, split=split, **kw)
            except:
                traceback.print_exc()
コード例 #3
0
ファイル: slicertest.py プロジェクト: Squarespace/cubes
class SlicerTestCase(unittest.TestCase):
    def setUp(self):
        self.w = Workspace()
        self.w.add_slicer("myslicer", "http://localhost:5010")

        self.cube_list = self.w.list_cubes()

    def first_date_dim(self, cube):
        for d in cube.dimensions:
            if ( d.info.get('is_date') ):
                return d
        raise BrowserError("No date dimension in cube %s" % cube.name)

    def test_basic(self):
        for c in self.cube_list:
            if c.get('category') is not None and 'Mix' in c.get('category', ''):
                continue
            print ("Doing %s..." % c.get('name')),
            cube = self.w.cube(c.get('name'))
            date_dim = self.first_date_dim(cube)
            cut = cubes.browser.RangeCut(date_dim, [ 2013, 9, 25 ], None)
            cell = cubes.browser.Cell(cube, [ cut ])
            drill = cubes.browser.Drilldown([(date_dim, None, date_dim.level('day'))], cell)
            b = self.w.browser(cube)
            try:
                attr_dim = cube.dimension("attr")
                split = cubes.browser.PointCut(attr_dim, ['paid', 'pnb'])
            except:
                split = None
            try:
                kw = {}
                if cube.aggregates:
                    kw['aggregates'] = [cube.aggregates[0]]
                elif cube.measures:
                    kw['measures'] = [ cube.measures[0] ]
                else:
                    raise ValueError("Cube has neither aggregates nor measures")
                result = b.aggregate(cell, drilldown=drill, split=split, **kw)
                print result.cells
            except:
                import sys
                print sys.exc_info()
コード例 #4
0
class TweetCube:

    def __init__(self, concept):
        self.createCube()
        self.concept = concept

    def createCube(self):
        self.workspace = Workspace()
        self.workspace.register_default_store("sql",
                                         url="mysql://root:@localhost/datawarehouse")
        model = cubes.read_model_metadata_bundle("../CubeModelisation/model/")
        self.workspace.import_model(model)
        self.browserTweet = self.workspace.browser("tweet")

    def getPieChartSource(self):
        cube = self.workspace.cube("tweet")
        cube.browser = self.browserTweet

        cut = [PointCut("concept", [self.concept])]
        cell = Cell(cube, cut)

        result = self.browserTweet.aggregate(cell, drilldown=["location","source"],aggregates=["numberOfTweets_sum"])
        output = defaultdict(lambda: defaultdict())

        for row in result.table_rows("location"):
            continent = row.record['location.continentName']
            source = row.record['source.sourceName']
            output[continent][source] = row.record['numberOfTweets_sum']
        temp = {'continentName': '',
                'sources': [{'source': '', 'numberOfTweets': ''}, {'source': '', 'numberOfTweets': ''},
                            {'source': '', 'numberOfTweets': ''}, {'source': '', 'numberOfTweets': ''}]}
        print("output ",output)
        i = 0
        data = []
        continentsList = ['Asia','Africa','Australia','Europe','North America','South America']
        for continent in continentsList:
            temp['continentName'] = continent
            if output[continent]:
                temp['sources'][i]['source'] = "iPhone"
                temp['sources'][i]['numberOfTweets'] = output[continent].get('iPhone', 0)
                i += 1
                temp['sources'][i]['source'] = "Android"
                temp['sources'][i]['numberOfTweets'] = output[continent].get('Android', 0)
                i += 1
                temp['sources'][i]['source'] = "Web"
                temp['sources'][i]['numberOfTweets'] = output[continent].get('Web', 0)
                i += 1
                temp['sources'][i]['source'] = "Unknown"
                temp['sources'][i]['numberOfTweets'] = output[continent].get('Unknown', 0)
            else:
                temp['sources'][i]['source'] = "iPhone"
                temp['sources'][i]['numberOfTweets'] = 0
                i += 1
                temp['sources'][i]['source'] = "Android"
                temp['sources'][i]['numberOfTweets'] = 0
                i += 1
                temp['sources'][i]['source'] = "Web"
                temp['sources'][i]['numberOfTweets'] = 0
                i += 1
                temp['sources'][i]['source'] = "Unknown"
                temp['sources'][i]['numberOfTweets'] = 0

            i = 0
            data.append(temp)
            temp = {'continentName': '',
                'sources': [{'source': '', 'numberOfTweets': ''}, {'source': '', 'numberOfTweets': ''},
                            {'source': '', 'numberOfTweets': ''}, {'source': '', 'numberOfTweets': ''}]}
        return data

    def getBarChartRaceByLanguageAndDate(self):
        cube = self.workspace.cube("tweet")
        cube.browser = self.browserTweet

        cut = [PointCut("concept", [self.concept])]
        cell = Cell(cube, cut)

        result = self.browserTweet.aggregate(cell, drilldown=["time:day", "language"],
                                             aggregates=["numberOfTweets_sum"])
        output = []
        for row in result.table_rows("time"):
            output.append(row.record)
        data = defaultdict(lambda: defaultdict(lambda: defaultdict()))
        languagesList = []
        for row in output:
            date = row['time.day'] + "/" + row['time.month'] + "/" + row['time.year']
            language = row['language.languageName']
            languagesList.append(language)
            # creating data structure containing all languages
            data[date][language]['numberOfTweets'] = row['numberOfTweets_sum']

        #GET LIST OF LANGUAGES FROM FILE
        import pickle
        with open('../Docs/languagesStructure.pickle', 'rb') as file:
            languagesList = pickle.load(file)
        print(len(languagesList))
        element = {'date': '', 'languagesList': []}
        dataList = []
        for date in data:
            element['date'] = date
            element['languagesList'] = []
            print(len(languagesList))
            for language in languagesList:
                if language in data[date]:
                    element['languagesList'].append({'language':language,'numberOfTweets':data[date][language]['numberOfTweets']})
                else:
                    element['languagesList'].append({'language':language,'numberOfTweets':0})
            dataList.append(element)
        return dataList


    def getBarChartRaceBySentimentAndDate(self):
        cube = self.workspace.cube("tweet")
        cube.browser = self.browserTweet

        cut = [PointCut("concept", [self.concept])]
        cell = Cell(cube, cut)

        result = self.browserTweet.aggregate(cell, drilldown=["time:day", "sentiment"],
                                             aggregates=["numberOfTweets_sum"])

        output = []
        for row in result.table_rows("time"):
            output.append(row.record)

        data = defaultdict(lambda: defaultdict(lambda: defaultdict()))
        for row in output:
            date = row['time.day'] + "/" + row['time.month'] + "/" + row['time.year']
            sentiment = row['sentiment.sentimentLabel']
            data[date][sentiment]['numberOfTweets'] = row['numberOfTweets_sum']
        dataList = []
        element = {'date': '', 'sentimentsList': []}
        for date in data:
            element['date'] = date
            sentimentElement = {'sentiment': '', 'numberOfTweets': 0}
            mySentimentsList = []
            for sentiment in data[date]:
                sentimentElement['sentiment'] = sentiment
                sentimentElement['numberOfTweets'] = data[date][sentiment]['numberOfTweets']
                mySentimentsList.append(sentimentElement)
                sentimentElement = {'sentiment': '', 'numberOfTweets': 0}
            element['sentimentsList'] = mySentimentsList
            dataList.append(element)
            element = {'date': '', 'sentimentsList': []}
        return dataList
コード例 #5
0
        count += 1
        if (count >= level):
            count = 1
            break
        new_cell = cell.drilldown(dimension, row.key)
        drilldown(new_cell, dimension, level)


# 1. Creating a workspace
workspace = Workspace()
workspace.register_default_store("sql", url="sqlite:///restaurant.sqlite")
workspace.import_model("model.json")

# 2. Getting a browser
cube = workspace.cube("restaurant_details")
browser = workspace.browser(cube)
dimension = cube.dimension("location")


# Rolling up to State
print("\n"
      "Roll up to state\n"
      "================")

cell = Cell(browser.cube)
rollup(cell, "location")


# Drilling down into the cities of each state
print("\n"
コード例 #6
0
ファイル: olap.py プロジェクト: HannahWang/CS838-Data-Science
                              ("hours_fri_close", "string"),
                              ("hours_sat_open", "string"),
                              ("hours_sat_close", "string")],
                      create_id=False)

from cubes import Workspace
from cubes import PointCut
from cubes import Cell

from prettytable import PrettyTable

workspace = Workspace()
workspace.register_default_store("sql", url="sqlite:///data.sqlite")
workspace.import_model("model.json")
browser = workspace.browser("restaurants")
cube = workspace.cube("restaurants")
'''
# aggregation by zipcode
print("ZipCode, # Restaurants")
result = browser.aggregate(drilldown=["location:zipcode"])
for record in result:
    print(record['location.zipcode'], ",", record['num_restaurants'])
print()
'''

# aggregation by restaurant price
result = browser.aggregate(drilldown=["price"])
t = PrettyTable([
    'Price', '# Restaurants', 'Avg. Rating', '# Reviews/Restaurant',
    'Weekend Open Hours', 'Weekend Close Hours', 'Weekday Open Hours',
    'Weekday Close Hours'