File size: 24,332 Bytes
b04beb1 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 |
from __future__ import absolute_import, print_function
import os
import shlex
from pystata import config as _config
from pystata.stexception import IPythonError, IPyKernelError
try:
from IPython.core.magic import (Magics, magics_class, line_cell_magic, needs_local_scope, line_magic)
from pystata.ipython.ipy_utils import get_ipython_stata_cache_dir
except ImportError:
_config.stipython = 1
raise IPythonError('failed to import IPython package')
from pystata import stata as _stata
if not _stata.has_num_pand['pknum']:
_config.stipython = 3
elif not _stata.has_num_pand['pkpand']:
_config.stipython = 4
if _stata.has_num_pand['pknum']:
import numpy as np
if _stata.has_num_pand['pkpand']:
import pandas as pd
from pystata.core import numpy2mata as _mata
from IPython import get_ipython
def _parse_arguments(inopts, allowopts, magicname):
res = {}
errmsg = 'Type ' + magicname + ' to get more information.'
opts = shlex.split(inopts, posix=False)
olen = len(opts)
if olen==0:
return res
i = 0
while i < olen:
if opts[i][0]=='-' and ':' in opts[i]:
raise SyntaxError('option ' + opts[i] + ' not allowed. ' + errmsg)
if i==(olen-1):
if opts[i] in allowopts:
res[opts[i]] = ''
i += 1
else:
tmp = opts[i] + ':'
if tmp in allowopts:
raise SyntaxError('A value is required for option ' + opts[i] + '. ' + errmsg)
else:
raise SyntaxError('option ' + opts[i] + ' not allowed. ' + errmsg)
else:
if opts[i] in allowopts:
if opts[i+1][0] != '-':
raise SyntaxError('option ' + opts[i] + ' misspecified. ' + errmsg)
else:
res[opts[i]] = ''
i += 1
else:
tmp = opts[i] + ':'
if tmp in allowopts:
if opts[i+1][0] == '-':
raise SyntaxError('option ' + opts[i] + ' misspecified. ' + errmsg)
else:
res[opts[i]] = opts[i+1]
i += 2
else:
raise SyntaxError('option ' + opts[i] + ' not allowed. ' + errmsg)
return res
def _get_input_type(o):
if _stata.has_num_pand['pknum']:
if isinstance(o, np.ndarray):
return 1
if _stata.has_num_pand['pkpand']:
if isinstance(o, pd.DataFrame):
return 2
return 0
@magics_class
class PyStataMagic(Magics):
def __init__(self, shell):
super(PyStataMagic, self).__init__(shell)
self._lib_dir = os.path.join(get_ipython_stata_cache_dir(), 'stata')
if not os.path.exists(self._lib_dir):
os.makedirs(self._lib_dir)
@needs_local_scope
@line_cell_magic
def stata(self, line, cell=None, local_ns=None):
'''
Execute one line or a block of Stata commands.
When the line magic command **%stata** is used, a one-line Stata
command can be specified and executed, as it would be in Stata's
Command window. When the cell magic command **%%stata** is used, a
block of Stata commands can be specified and executed all at once.
This is similar to executing a series of commands from a do-file.
Cell magic syntax:
%%stata [-d DATA] [-f DFLIST|ARRLIST] [-force]
[-doutd DATAFRAME] [-douta ARRAY] [-foutd FRAMELIST] [-fouta FRAMELIST]
[-ret DICTIONARY] [-eret DICTIONARY] [-sret DICTIONARY] [-qui] [-nogr]
[-gw WIDTH] [-gh HEIGHT]
Optional arguments:
-d DATA Load a NumPy array or pandas dataframe
into Stata as the current working dataset.
-f DFLIST|ARRLIST Load one or multiple NumPy arrays or
pandas dataframes into Stata as frames.
The arrays and dataframes should be
separated by commas. Each array or
dataframe is stored in Stata as a separate
frame with the same name.
-force Force loading of the NumPy array or pandas
dataframe into Stata as the current working
dataset, even if the dataset in memory has
changed since it was last saved; or force
loading of the NumPy arrays or pandas dataframes
into Stata as separate frames even if one or
more of the frames already exist in Stata.
-doutd DATAFRAME Save the dataset in memory as a pandas
dataframe when the cell completes.
-douta ARRAY Save the dataset in memory as a NumPy
array when the cell completes.
-foutd FRAMELIST Save one or multiple Stata frames as pandas
dataframes when the cell completes. The Stata
frames should be separated by commas. Each
frame is stored in Python as a pandas
dataframe. The variable names in each frame
will be used as the column names in the
corresponding dataframe.
-fouta FRAMELIST Save one or multiple Stata frames as NumPy
arrays when the cell completes. The Stata frames
should be separated by commas. Each frame is
stored in Python as a NumPy array.
-ret DICTIONARY Store current r() results into a dictionary.
-eret DICTIONARY Store current e() results into a dictionary.
-sret DICTIONARY Store current s() results into a dictionary.
-qui Run Stata commands but suppress output.
-nogr Do not display Stata graphics.
-gw WIDTH Set graph width in inches, pixels, or centimeters;
default is inches.
-gh HEIGHT Set graph height in inches, pixels, or centimeters;
default is inches.
Line magic syntax:
%stata stata_cmd
'''
if cell is None:
if not line:
raise SyntaxError("Stata command required")
_stata.run(line)
return
allowopts = ['-d:', '-f:', '-force', '-doutd:', '-douta:', '-foutd:', '-fouta:', '-ret:', '-eret:', '-sret:', '-qui', '-nogr', '-gw:', '-gh:']
args = _parse_arguments(line, allowopts, '%%stata?')
if local_ns is None:
local_ns = {}
force_to_load = False
if '-force' in args:
force_to_load = True
if '-d' in args:
if not _stata.has_num_pand['pknum'] and not _stata.has_num_pand['pkpand']:
raise SystemError('NumPy package or pandas package is required to use this argument.')
data = args['-d']
try:
val = local_ns[data]
except KeyError:
try:
val = self.shell.user_ns[data]
except KeyError:
raise NameError("name '%s' is not defined" % data)
input_dtype = _get_input_type(val)
if input_dtype > 0:
try:
if input_dtype==2:
_stata.pdataframe_to_data(val, force=force_to_load)
else:
_stata.nparray_to_data(val, force=force_to_load)
except SystemError:
raise
except:
raise SystemError("Exception occurred. Data could not be loaded.")
else:
raise TypeError("A pandas dataframe or NumPy array is required.")
if '-f' in args:
if not _stata.has_num_pand['pknum'] and not _stata.has_num_pand['pkpand']:
raise SystemError('NumPy package or pandas package is required to use this argument.')
frames = args['-f'].replace("'", '').replace('"', '').split(',')
for fr in frames:
try:
frval = local_ns[fr]
except KeyError:
try:
frval = self.shell.user_ns[fr]
except KeyError:
raise NameError("name '%s' is not defined" % fr)
input_ftype = _get_input_type(frval)
if input_ftype==0:
raise TypeError("%s is not a pandas dataframe or NumPy array. Only dataframe or array is allowed." % fr)
for fr in frames:
try:
frval = local_ns[fr]
except KeyError:
frval = self.shell.user_ns[fr]
input_ftype = _get_input_type(frval)
try:
if input_ftype==2:
_stata.pdataframe_to_frame(frval, fr, force=force_to_load)
else:
_stata.nparray_to_frame(frval, fr, force=force_to_load)
except SystemError:
raise
except:
raise SystemError("Exception occurred. %s could not be loaded." % fr)
if '-gw' in args or '-gh' in args:
gwidth = _config.get_graph_size_str('gw')
gheight = _config.get_graph_size_str('gh')
if '-gw' in args and '-gh' in args:
_config.set_graph_size(args['-gw'], args['-gh'])
elif '-gw' in args:
_config.set_graph_size(args['-gw'], None)
else:
_config.set_graph_size(None, args['-gh'])
if '-nogr' in args:
grshow = _config.stconfig['grshow']
_config.set_graph_show(False)
if '-qui' in args:
_stata.run(cell, quietly=True, inline=_config.stconfig['grshow'])
else:
_stata.run(cell, quietly=False, inline=_config.stconfig['grshow'])
if '-gw' in args or '-gh' in args:
_config.set_graph_size(gwidth, gheight)
if '-nogr' in args:
_config.set_graph_show(grshow)
if '-ret' in args or '-eret' in args or '-sret' in args:
if '-ret' in args:
val = _stata.get_return()
self.shell.push({args['-ret']: val })
if '-eret' in args:
val = _stata.get_ereturn()
self.shell.push({args['-eret']: val })
if '-sret' in args:
val = _stata.get_sreturn()
self.shell.push({args['-sret']: val })
if '-doutd' in args:
if not _stata.has_num_pand['pkpand']:
raise SystemError('pandas package is required to use this argument.')
try:
output_df = _stata.pdataframe_from_data()
self.shell.push({args['-doutd']: output_df })
except:
print("Exception occurred. Data could not be written to the dataframe.")
if '-douta' in args:
if not _stata.has_num_pand['pknum']:
raise SystemError('NumPy package is required to use this argument.')
try:
output_arr = _stata.nparray_from_data()
self.shell.push({args['-douta']: output_arr })
except:
print("Exception occurred. Data could not be written to the array.")
if '-foutd' in args:
if not _stata.has_num_pand['pkpand']:
raise SystemError('pandas package is required to use this argument.')
frames = args['-foutd'].replace("'", '').replace('"', '').split(',')
output_dfs = {}
for fr in frames:
try:
output_df = _stata.pdataframe_from_frame(fr)
output_dfs[fr] = output_df
except:
raise SystemError("Exception occurred. Stata's frame %s could not be saved to a pandas dataframe." % fr)
self.shell.push(output_dfs)
if '-fouta' in args:
if not _stata.has_num_pand['pknum']:
raise SystemError('NumPy package is required to use this argument.')
frames = args['-fouta'].replace("'", '').replace('"', '').split(',')
output_arrs = {}
for fr in frames:
try:
output_arr = _stata.nparray_from_frame(fr)
output_arrs[fr] = output_arr
except:
raise SystemError("Exception occurred. Stata's frame %s could not be saved to a NumPy array." % fr)
self.shell.push(output_arrs)
@needs_local_scope
@line_cell_magic
def mata(self, line, cell=None, local_ns=None):
'''
Execute one line or a block of Mata code.
When the **%mata** line magic command is used, one line of Mata code
can be specified and executed. This is similar to specifying
**mata: istmt** within Stata. When the **%%mata** cell magic command is
used, a block of Mata code can be specified. The code is executed just
as it would be in a do-file.
Cell magic syntax:
%%mata [-m ARRAYLIST] [-outm MATLIST] [-qui] [-c]
Execute a block of Mata code. This is equivalent to running a
block of Mata code within a do-file. You do not need to
explicitly place the code within the mata[:] and end block.
Optional arguments:
-m ARRAYLIST Load multiple NumPy arrays into Mata's
interactive environment. The array names
should be separated by commas. Each array is
stored in Mata as a matrix with the same name.
-outm MATLIST Save Mata matrices as NumPy arrays when the
cell completes. The matrix names should be
separated by commas. Each matrix is stored
in Python as a NumPy array.
-qui Run Mata code but suppress output.
-c This argument specifies that Mata code be
executed in mata: mode. This means that
if Mata encounters an error, it will stop
execution and return control to Python. The
error will then be thrown as a Python SystemError
exception.
Line magic syntax:
%mata [-c]
Enter Mata's interactive environment. This is equivalent to
typing mata or mata: in the Stata Command window.
Optional argument:
-c Enter interactive Mata environment in mata:
mode. The default is to enter in mata mode.
%mata istmt
Run a single-line Mata statement. This is equivalent to executing
mata: istmt within Stata.
'''
if cell is None:
if not line:
stcmd = "mata"
elif line=='-c':
stcmd = 'mata:'
else:
stcmd = "mata: " + line
_stata.run(stcmd, inline=False)
else:
allowopts = ['-qui', '-c', '-m:', '-outm:']
args = _parse_arguments(line, allowopts, '%%mata?')
if local_ns is None:
local_ns = {}
if '-m' in args:
if not _stata.has_num_pand['pknum']:
raise SystemError('NumPy package is required to use this argument.')
mats = args['-m'].replace("'", '').replace('"', '').split(',')
for m in mats:
try:
mval = local_ns[m]
except KeyError:
try:
mval = self.shell.user_ns[m]
except KeyError:
raise NameError("name '%s' is not defined" % m)
for m in mats:
try:
mval = local_ns[m]
except KeyError:
mval = self.shell.user_ns[m]
try:
_mata.array_to_mata(mval, m)
except:
print("Exception occurred. Array %s could not be loaded." % m)
if cell[-1:] != '\n':
cell = cell + '\n'
if '-c' in args:
cell = "mata:\n" + cell + "end"
else:
cell = "mata\n" + cell + "end"
if '-qui' in args:
_stata.run(cell, quietly=True, inline=False)
else:
_stata.run(cell, inline=False)
if '-outm' in args:
if not _stata.has_num_pand['pknum']:
raise SystemError('NumPy package is required to use this argument.')
mats = args['-outm'].replace("'", '').replace('"', '').split(',')
output_arrs = {}
for m in mats:
try:
output_arr = _mata.array_from_mata(m)
output_arrs[m] = output_arr
except:
print("Exception occurred. Mata matrix %s could not be written to a NumPy array." % m)
self.shell.push(output_arrs)
@line_magic
def pystata(self, line):
'''
Stata utility commands.
Line magic syntax:
%pystata status
Display current system information and settings.
%pystata set graph_show True|False [, perm]
Set whether Stata graphics are displayed. The default is to
display the graphics. Note that if multiple graphs are
generated, only the last one is displayed. To display multiple
graphs, use the name() option with Stata's graph commands.
%pystata set graph_size w #[in|px|cm] [, perm]
%pystata set graph_size h #[in|px|cm] [, perm]
%pystata set graph_size w #[in|px|cm] h #[in|px|cm] [, perm]
Set dimensions for Stata graphs. The default is a 5.5-inch width
and 4-inch height. Values may be specified in inches, pixels, or
centimeters; the default unit is inches. Either the width or height
must be specified, or both. If only one is specified, the other one
is determined by the aspect ratio.
%pystata set graph_format svg|png|pdf [, perm]
Set the graphic format used to display Stata graphs. The default
is svg. If svg or png is specified, the graphs will be embedded.
If pdf is specified, the graphs will be displayed, and exported
to PDF files and stored in the current working directory with
numeric names, such as 0.pdf, 1.pdf, 2.pdf, etc. Storing the PDF
graph files in the current directory allows you to embed them in
the notebook when exporting the notebook to a PDF via Latex.
'''
if line is None:
raise SyntaxError('subcommand required')
scmd = line.strip().split(',')
if len(scmd)>=3:
raise SyntaxError('invalid syntax')
sperm = False
sopt = scmd[1].strip() if len(scmd)==2 else ""
if sopt!="":
if sopt not in ["perm", "perma", "perman", "permane", "permanen", "permanent", "permanentl", "permanently"]:
raise SyntaxError('option ' + sopt + ' not allowed')
else:
sperm = True
scmd = scmd[0].strip().split()
slen = len(scmd)
if slen==0:
raise SyntaxError('subcommand required')
subcmd = scmd[0]
if subcmd=='help':
if slen!=1:
topic = ' '.join(scmd[1:])
else:
topic = 'help_advice'
_stata.run('help ' + topic, inline=False)
elif subcmd=='status':
if slen!=1:
raise SyntaxError(' '.join(scmd[1:]) + ' not allowed')
else:
_config.status()
elif subcmd=='set':
if slen==1:
raise SyntaxError('set type must be specified')
sset = scmd[1:]
settype = sset[0]
if settype=="graph_size":
if len(sset)!=3 and len(sset)!=5:
raise ValueError('invalid graph_size set')
if len(sset)==3:
if sset[1]=='width' or sset[1]=='w':
_config.set_graph_size(width=sset[2], height=None, perm=sperm)
elif sset[1]=='height' or sset[1]=='h':
_config.set_graph_size(width=None, height=sset[2], perm=sperm)
else:
raise ValueError('invalid graph_size value')
else:
if (sset[1]=='width' or sset[1]=='w') and (sset[3]=='height' or sset[3]=='h'):
_config.set_graph_size(width=sset[2], height=sset[4], perm=sperm)
elif (sset[1]=='height' or sset[1]=='h') and (sset[3]=='width' or sset[3]=='w'):
_config.set_graph_size(width=sset[4], height=sset[2], perm=sperm)
else:
raise ValueError('invalid graph_size value')
elif settype=="graph_show":
if len(sset)!=2:
raise SyntaxError('invalid graph_show set; only True or False allowed.')
if sset[1]=='True':
_config.set_graph_show(show=True, perm=sperm)
elif sset[1]=='False':
_config.set_graph_show(show=False, perm=sperm)
else:
raise ValueError('invalid graph_show value')
elif settype=="graph_format":
if len(sset)!=2:
raise SyntaxError('invalid graph_format set')
if sset[1]=='svg':
_config.set_graph_format(gformat='svg', perm=sperm)
elif sset[1]=='png':
_config.set_graph_format(gformat='png', perm=sperm)
elif sset[1]=='pdf':
_config.set_graph_format(gformat='pdf', perm=sperm)
else:
raise ValueError('invalid graph_format value; only svg, png, or pdf allowed.')
elif settype=="streaming_output_mode":
if len(sset)!=2:
raise SyntaxError('invalid streaming_output_mode set')
if sset[1]=='on':
_config.set_streaming_output_mode(flag='on', perm=sperm)
elif sset[1]=='off':
_config.set_streaming_output_mode(flag='off', perm=sperm)
else:
raise ValueError('invalid streaming_output_mode value; only on or off allowed.')
else:
raise SyntaxError('invalid set type')
else:
raise SyntaxError('invalid subcommand')
try:
ip = get_ipython()
ip.register_magics(PyStataMagic)
except AttributeError:
_config.stipython = 2
raise IPyKernelError('register_magics not found') |