def loaddata(self, filename=None, folder=None): ''' Load data from a spreadsheet ''' self.data = sc.loadspreadsheet(filename=filename, folder=folder) self.data.filtercols(self.colnames.values(), die=True) self.filename = filename self.parse() return None
if check('savespreadsheet', ['loadspreadsheet', 'Spreadsheet', 'savezip']): 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 if check('loadspreadsheet'): data = sc.loadspreadsheet(files.excel) print(data) if check('readcells'): wb = sc.Spreadsheet(filename=filedir+'exampledata.xlsx') # 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) if check('Blobject'): blob = sc.Blobject(files.excel) f = blob.tofile() wb = openpyxl.load_workbook(f)
import pylab as pl import sciris as sc dosave = True fig1 = 1 fig2 = 1 fig3 = 1 sc.heading('Loading data...') country_data = sc.loadspreadsheet('country-data.xlsx') interv_data = sc.loadspreadsheet('rapid_interventions.xlsx') D = sc.loadobj('results/rapid_data.obj') R = sc.loadobj('results/rapid_results.obj') def dalys_fig(label='', region=None, income=None): sc.heading('DALYs figure') idata = sc.dcp(interv_data) idata.sort(col='Short name') category1 = idata['Category 1'].tolist() category2 = idata['Category 2'].tolist() interv_category = [c1 + ': ' + c2 for c1, c2 in zip(category1, category2)] categories = sorted(set(interv_category)) ncategories = len(categories) nspends, nintervs = R[0]['dalys'].shape assert idata['Short name'].tolist() == R[0]['meta']['interv_names'].tolist( ) mapping = [] for i_c in interv_category:
import hiptool as hp sc.heading('Initializing...') sc.tic() dosave = True missing_data = ['remove', 'assumption'][1] # Choose how to handle missing data spendings = [0.1, 0.3, 1, 3, 10, 30, 100, 300, 1000]#, 3000, 10000] nspendings = len(spendings) colors = sc.vectocolor(len(spendings)) # Load input files D = sc.odict() # Data R = sc.odict() # Results bod_data = sc.loadobj('gbd-data.dat') country_data = sc.loadspreadsheet('country-data.xlsx') baseline_factor = country_data.findrow('Zambia', asdict=True)['icer_multiplier'] # Zambia was used for this # Create default P = hp.Project() P.loadburden(filename='rapid_BoD.xlsx') P.loadinterventions(filename='rapid_interventions.xlsx') ninterventions = P.intervsets[0].data.nrows # Load data missing_data_adjustment_factor = 2 sc.heading('Loading data...') for c,country in enumerate(country_data['name'].tolist()): print(f' Working on {country}...') D[country] = sc.dcp(P)
import pylab as pl import sciris as sc dosave = True fig1 = True sc.heading('Loading data...') interv_data = sc.loadspreadsheet('rapid_interventions.xlsx') spend_data = sc.loadspreadsheet('rapid_spending.xlsx') D = sc.loadobj('results/rapid_spend_data.obj') R = sc.loadobj('results/rapid_spend_results.obj') #%% Fig. 1 -- DALYs if fig1: sc.heading('Spending figure') interv_category = interv_data['Category 1'].tolist() categories = sorted(set(interv_category)) ncategories = len(categories) nintervs = len(R[0]['alloc']) mapping = [] for i_c in interv_category: for c, cat in enumerate(categories): if i_c == cat: mapping.append(c) allocdata = pl.zeros((2, ncategories)) for country in R.keys(): # tODO: different regions opt_alloc = R[country]['alloc'] orig_alloc = spend_data[country] for i in range(nintervs): c = mapping[i]
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
import pylab as pl import sciris as sc import hiptool as hp sc.heading('Initializing...') sc.tic() dosave = True missing_data = ['remove', 'assumption'][1] # Choose how to handle missing data # Load input files D = sc.odict() # Data R = sc.odict() # Results bod_data = sc.loadobj('gbd-data.dat') country_data = sc.loadspreadsheet('country-data.xlsx') spend_data = sc.loadspreadsheet('rapid_spending.xlsx') baseline_factor = country_data.findrow( 'Zambia', asdict=True)['icer_multiplier'] # Zambia was used for this # Get country list countries = sc.dcp(spend_data.cols) countries.remove('Short name') # Create default P = hp.Project() P.loadburden(filename='rapid_BoD.xlsx') P.loadinterventions(filename='rapid_interventions.xlsx') ninterventions = P.intervsets[0].data.nrows # Load data sc.heading('Loading data...')