def test_member_ages():
    pop_2 = sc.prettyobj()
    pop_2.schools = []
    student_lists = [[3, 4, 5, 6, 7]]
    teacher_lists = [[1, 2]]
    non_teaching_staff_lists = [[0, 8]]
    age_by_uid = {
        0: 69,
        1: 55,
        2: 45,
        3: 17,
        4: 15,
        5: 16,
        6: 19,
        7: 15,
        8: 58
    }
    age_by_uid_array = np.array([age_by_uid[k] for k in sorted(age_by_uid)])
    sps.populate_schools(pop_2,
                         student_lists=student_lists,
                         teacher_lists=teacher_lists,
                         non_teaching_staff_lists=non_teaching_staff_lists,
                         age_by_uid=age_by_uid,
                         school_mixing_types=['age_and_class_clustered'])
    assert set(pop_2.schools[0].student_ages(age_by_uid_array)) == set(
        [15, 16, 17, 19]), 'Check student_ages failed.'
    assert set(pop_2.schools[0].teacher_ages(age_by_uid_array)) == set(
        [45, 55]), 'Check teacher_ages failed.'
    assert set(
        pop_2.schools[0].non_teaching_staff_ages(age_by_uid_array)) == set(
            [58, 69]), 'Check non_teaching_staff_ages failed.'
    assert set(pop_2.schools[0].member_ages(age_by_uid_array)) == set(
        [15, 16, 17, 19, 45, 55, 58, 69]), 'Check member_ages failed.'
Example #2
0
def save_new_project(proj, username=None, uid=None):
    """
    If we're creating a new project, we need to do some operations on it to
    make sure it's valid for the webapp.
    """
    # Preliminaries
    new_project = sc.dcp(proj)  # Copy the project, only save what we want...
    new_project.uid = sc.uuid(uid)

    # Get unique name
    user = get_user(username)
    current_project_names = []
    for project_key in user.projects:
        proj = load_project(project_key)
        current_project_names.append(proj.name)
    new_project_name = sc.uniquename(new_project.name,
                                     namelist=current_project_names)
    new_project.name = new_project_name

    # Ensure it's a valid webapp project
    if not hasattr(new_project, 'webapp'):
        new_project.webapp = sc.prettyobj()
        new_project.webapp.username = username  # If we ever use Celery with HealthPrior: new_project.webapp.tasks = []
    new_project.webapp.username = username  # Make sure we have the current username

    # Save all the things
    key = save_project(new_project)
    if key not in user.projects:  # Let's not allow multiple copies
        user.projects.append(key)
        datastore.saveuser(user)
    return key, new_project
Example #3
0
def test_prepr():
    sc.heading('Test pretty representation of an object')
    n_attrs = 500
    myobj = sc.prettyobj()
    for i in range(n_attrs):
        key = f'attr{i:03d}'
        setattr(myobj, key, i**2)
    print(myobj)
    return myobj
Example #4
0
def test_printing():
    ''' Test printing functions '''
    example = sc.prettyobj()
    example.data = sc.vectocolor(10)
    print('sc.pr():')
    sc.pr(example)
    print('sc.pp():')
    sc.pp(example.data)
    string = sc.pp(example.data, doprint=False)
    return string
Example #5
0
def test_printing():
    sc.heading('test_printing()')
    example = sc.prettyobj()
    example.data = sc.vectocolor(10)
    print('sc.pr():')
    sc.pr(example)
    print('sc.pp():')
    sc.pp(example.data)
    string = sc.pp(example.data, doprint=False)
    print('sc.printdata():')
    sc.printdata(example.data)
    return string
def test_households_initialization():
    sp.logger.info("Test households initialization methods.")

    pop = sc.prettyobj()
    pop.households = []

    # test no households made
    sp.initialize_empty_households(pop)
    assert pop.n_households == 0, 'Check failed. households.n_households is not 0.'
    print(
        'Check passed. Initially without any households information, households.n_households is 0.'
    )

    homes_by_uids = [[1, 2, 3], [4], [7, 6, 5, 8, 9]]
    age_by_uid = {
        1: 88,
        2: 45,
        3: 47,
        4: 38,
        5: 12,
        6: 19,
        7: 55,
        8: 58,
        9: 99
    }

    pop.households = homes_by_uids
    pop.n_households = 2

    assert pop.n_households != len(
        pop.households
    ), 'Check failed. pop.n_households and len(pop.households_array) are not aligned.'
    print(
        'Check passed. Initially households.n_households do not match len(households.households_array).'
    )

    pop.households = []
    sp.initialize_empty_households(pop, n_households=5)
    for i in range(pop.n_households):
        assert isinstance(
            pop.households[i], sp.Household
        ) and pop.households[i][
            'hhid'] is None, 'Check failed. households[i] is not a household object.'
    print(f'Check passed. Initialized {pop.n_households} empty households.')

    # test that if there are not enough households when populating, we reinitialize that cover with the correct number
    pop.households = []
    sp.populate_households(pop, homes_by_uids, age_by_uid)
    assert len(pop.households) == len(homes_by_uids), 'Check failed.'
    print('Check passed.')
Example #7
0
def test_jsonpickle():
    sc.heading('Testing JSON read/write functions')

    myobj = sc.prettyobj()
    myobj.a = 3
    myobj.b = pd.DataFrame.from_dict({'a': [3, 5, 23]})

    jp = sc.jsonpickle(myobj)
    jps = sc.jsonpickle(myobj, tostring=True)
    myobj2 = sc.jsonunpickle(jp)
    myobj3 = sc.jsonunpickle(jps)

    assert myobj.b.equals(myobj2.b)
    assert myobj.b.equals(myobj3.b)

    return jp
def test_reset_household_values():
    sp.logger.info(
        "Test resetting household values. Warning these features should only be available when synthpops is set to use vital dynamics."
    )
    homes_by_uids = [[1, 2, 3], [4], [7, 6, 5, 8, 9]]
    age_by_uid = {
        1: 88,
        2: 45,
        3: 47,
        4: 38,
        5: 12,
        6: 19,
        7: 55,
        8: 58,
        9: 99
    }
    pop = sc.prettyobj()
    pop.households = []
    sp.populate_households(pop, homes_by_uids, age_by_uid)
def test_add_household():
    sp.logger.info(
        "Test creating a sp.Household object and adding it to an empty sp.Households class after generation."
    )
    home = [1, 2, 3]
    age_by_uid = {1: 88, 2: 45, 3: 47}
    household = sp.Household(member_uids=home,
                             reference_uid=home[0],
                             reference_age=age_by_uid[home[0]],
                             hhid=0)

    assert isinstance(household,
                      sp.Household), 'Check failed. Not a sp.Household object.'

    pop = sc.prettyobj()
    pop.households = []
    sp.add_household(pop, household)

    assert isinstance(
        pop.households[0], sp.Household
    ), 'Check failed. Did not add a sp.Household object to the list of households.'
    print(
        'Check passed. Added a sp.Household object to an sp.Households object.'
    )
Example #10
0
'savetext',
'loadtext',
'getfilelist',
'savezip',
'loadfailed',
]


def check(test, dependencies=None):
    if dependencies == None: dependencies = []
    tf = any([t in torun for t in [test]+dependencies])
    return tf
    
# Define filenames
filedir = 'files' + os.sep
files = sc.prettyobj()
files.excel  = filedir+'test.xlsx'
files.binary = filedir+'test.obj'
files.text   = filedir+'text.txt'
files.zip    = filedir+'test.zip'
tidyup = True

# Define the test data
nrows = 15
ncols = 3
testdata   = pl.zeros((nrows+1, ncols), dtype=object) # Includes header row
testdata[0,:] = ['A', 'B', 'C'] # Create header
testdata[1:,:] = pl.rand(nrows,ncols) # Create data

# Test spreadsheet writing, and create the file for later
if check('savespreadsheet', ['loadspreadsheet', 'Spreadsheet', 'savezip']):
def test_households_basic():
    sp.logger.info("Test creating generic households.")
    homes_by_uids = [[1, 2, 3], [4], [7, 6, 5, 8, 9]]
    age_by_uid = {
        1: 88,
        2: 45,
        3: 47,
        4: 38,
        5: 12,
        6: 19,
        7: 55,
        8: 58,
        9: 99
    }

    pop = sp.Pop(n=settings.pop_sizes.small)
    assert pop.get_household(1)['reference_uid'] == pop.homes_by_uids[1][
        0], 'Check failed on pop object.'
    print('Check passed on pop.get_household() method.')

    pop_1 = sc.dcp(pop)
    pop_1.initialize_empty_households(n_households=5)
    assert len(
        pop_1.households
    ) == 5, 'Check failed. Could not re-initialize to 5 households after generation.'
    print('Check passed on pop.initialize_empty_households().')

    pop_1.add_household(sc.dcp(pop.get_household(1)))
    assert len(pop_1.households
               ) == 6, 'Check failed. Could not add a new household to pop.'
    print('Check passed. Could add a new household with pop.add_household().')

    pop_2 = sc.prettyobj()
    pop_2.households = []
    sp.populate_households(pop_2, homes_by_uids, age_by_uid)

    assert pop_2.n_households == len(
        homes_by_uids), "number of households should match."
    for i in range(0, len(homes_by_uids)):
        assert pop_2.households[i]['reference_uid'] == homes_by_uids[i][0]
        assert sp.get_household(
            pop_2, i)['reference_age'] == age_by_uid[homes_by_uids[i][0]]
        assert len(pop_2.households[i]) == len(homes_by_uids[i])
    print(
        'Check passed. Generic households can be populated during class initialization.'
    )

    not_a_household = ''
    with pytest.raises(ValueError):
        sp.add_household(pop_2, not_a_household)
    print(
        'Check passed. Cannot add an object that is not a sp.Household using sp.add_household().'
    )

    with pytest.raises(TypeError):
        sp.get_household(pop_2, 0.5)
    print('Check passed. Cannot get a household with a non-int hhid')

    with pytest.raises(IndexError):
        sp.get_household(pop_2, len(pop_2.households) + 1)
    print(
        'Check passed. Cannot get a household with hhid larger than the number of households'
    )
Example #12
0
def test_legacy():
    '''
    Preserved for completeness, but too fragile to be used in automated unit testing
    due to reliance on openpyxl (which is not a required Sciris dependency).
    '''

    # Define filenames
    filedir = 'files' + os.sep
    files = sc.prettyobj()
    files.excel = filedir + 'test.xlsx'
    files.binary = filedir + 'test.obj'
    files.text = filedir + 'text.txt'
    files.zip = filedir + 'test.zip'
    tidyup = True

    # Define the test data
    nrows = 15
    ncols = 3
    testdata = pl.zeros((nrows + 1, ncols),
                        dtype=object)  # Includes header row
    testdata[0, :] = ['A', 'B', 'C']  # Create header
    testdata[1:, :] = pl.rand(nrows, ncols)  # Create data

    # Test spreadsheet writing, and create the file for later
    formats = {
        'header': {
            'bold': True,
            'bg_color': '#3c7d3e',
            'color': '#ffffff'
        },
        'plain': {},
        'big': {
            'bg_color': '#ffcccc'
        }
    }
    formatdata = pl.zeros(
        (nrows + 1, ncols),
        dtype=object)  # Format data needs to be the same size
    formatdata[1:, :] = 'plain'  # Format data
    formatdata[1:, :][testdata[
        1:, :] > 0.7] = 'big'  # Find "big" numbers and format them differently
    formatdata[0, :] = 'header'  # Format header
    sc.savespreadsheet(filename=files.excel,
                       data=testdata,
                       formats=formats,
                       formatdata=formatdata)

    # Test loading
    sc.heading('Loading spreadsheet')
    data = sc.loadspreadsheet(files.excel)
    print(data)

    excel_path = filedir + 'exampledata.xlsx'
    if os.path.exists(excel_path):
        sc.heading('Reading cells')
        wb = sc.Spreadsheet(
            filename=excel_path
        )  # Load a sample databook to try pulling cells from
        celltest = wb.readcells(method='xlrd',
                                sheetname='Baseline year population inputs',
                                cells=[[46, 2], [47,
                                                 2]])  # Grab cells using xlrd
        celltest2 = wb.readcells(
            method='openpyexcel',
            wbargs={'data_only': True},
            sheetname='Baseline year population inputs',
            cells=[[46, 2], [47, 2]]
        )  # Grab cells using openpyexcel.  You have to set wbargs={'data_only': True} to pull out cached values instead of formula strings
        print('xlrd output: %s' % celltest)
        print('openpyxl output: %s' % celltest2)
    else:
        print(f'{excel_path} not found, skipping...')

    sc.heading('Loading a blobject')
    blob = sc.Blobject(files.excel)
    f = blob.tofile()
    wb = openpyexcel.load_workbook(f)
    ws = wb.active
    ws['B7'] = 'Hi!     '
    wb.save(f)
    blob.load(f)
    blob.tofile(output=False)
    data = sc.loadspreadsheet(fileobj=blob.bytes)
    print(blob)
    sc.pp(data)

    # Test spreadsheet saving
    sc.heading('Using a Spreadsheet')
    S = sc.Spreadsheet(files.excel)
    S.writecells(cells=['A6', 'B7', 'C8', 'D9'],
                 vals=['This', 'is', 'a', 'test'])  # Method 1
    S.writecells(cells=[pl.array([7, 1]) + i for i in range(4)],
                 vals=['And', 'so', 'is', 'this'])  # Method 2
    newdata = (pl.rand(3, 3) * 100).round()
    S.writecells(startrow=14, startcol=1, vals=newdata,
                 verbose=True)  # Method 3
    S.save()
    data = S.readcells(header=False)
    print(S)
    sc.pp(data)

    sc.heading('Saveobj/loadobj')
    sc.saveobj(files.binary, testdata)

    obj = sc.loadobj(files.binary)
    print(obj)

    sc.heading('Savetext/loadtext')
    sc.savetext(files.text, testdata)

    obj = sc.loadtext(files.text)
    print(obj)

    sc.heading('Get files')
    print('Files in current folder:')
    sc.pp(sc.getfilelist())

    sc.heading('Save zip')
    sc.savezip(files.zip, [files.text, files.excel])
    '''
    Check that loading an object with a non-existent class works. The file
    deadclass.obj was created with:

    deadclass.py:
    -------------------------------------------------
    class DeadClass():
        def __init__(self, x):
            self.x = x
    -------------------------------------------------

    then:
    -------------------------------------------------
    import deadclass as dc
    import sciris as sc
    deadclass = dc.DeadClass(238473)
    sc.saveobj('deadclass.obj', deadclass)
    -------------------------------------------------
    '''
    dead_path = filedir + 'deadclass.obj'
    if os.path.exists(dead_path):
        sc.heading('Intentionally loading corrupted file')
        obj = sc.loadobj(dead_path)
        print('Loading corrupted object succeeded, x=%s' % obj.x)
    else:
        print(f'{dead_path} not found, skipping...')

    # Tidy up
    if tidyup:
        sc.blank()
        sc.heading('Tidying up')
        for fn in [files.excel, files.binary, files.text, files.zip]:
            try:
                os.remove(fn)
                print('Removed %s' % fn)
            except:
                pass

    print('Done, all fileio tests succeeded')

    return S
Example #13
0
if 'colorize' in torun:
    sc.colorize(showhelp=True)
    sc.colorize('green', 'hi')  # Simple example
    sc.colorize(['yellow', 'bgblack'])
    print('Hello world')
    print('Goodbye world')
    sc.colorize('reset')  # Colorize all output in between
    bluearray = sc.colorize(color='blue', string=str(range(5)), output=True)
    print("c'est bleu: " + bluearray)
    sc.colorize('magenta')  # Now type in magenta for a while
    print('this is magenta')
    sc.colorize('reset')  # Stop typing in magenta

# Test printing functions
if 'printing' in torun:
    example = sc.prettyobj()
    example.data = sc.vectocolor(10)
    print('sc.pr():')
    sc.pr(example)
    print('sc.pp():')
    sc.pp(example.data)
    string = sc.pp(example.data, doprint=False)

# Test profiling functions
if 'profile' in torun:

    def slow_fn():
        n = 10000
        int_list = []
        int_dict = {}
        for i in range(n):