Skip to main content

Open a bidirectional pipe to R, julia, matlab, or python (etc.) and communicate with it via a socket.

Project description

talk2stat

A python package to communicate with statistical software

talk2stat allows programs (such as latextopdf, nodejs) to communicate with statistical packages. Currently, we connect with R, julia, matlab, or python but extending it to other tools like Stata, SAS, or Python is very simple. The package contains two main functions: server() talks to the client program (e.g latextopdf) via a socket (inet), and to the stats package (R, julia, matlab, python) via a bi-directional pipe. The client() function is called by the user (e.g. latextopdf) to establish a connection with the server and pass code to the statistical package, and get the results.

Prerequisites and installation

talk2stat requires Python 3.8 and up. It uses several built-in packages: configparser, sys, os, os.path, unicodedata, re, and socket. Also, it uses the pexpect package to manage the bi-directional pipe to the statistical software. When you install talk2stat, pexpect will also be installed (if not already installed). To install it manually, just use

pip3 install pexpect

And, similarly, to install talk2stat use

pip3 install talk2stat

To install from a local copy,

pip3 install Dropbox/testpipe/pkg/dist/talk2stat-0.0.1.tar.gz 

Usage

An invocation of talk2stat is associated with a specific project. For now, it should be used by a single user, although in the future we plan to implement shared sessions. The reason for associating each instance of talk2stat with a single project is to prevent conflicting namespaces, variables, etc. In the current version talk2stat connects with one statistical software per invocation, but we consider a multithreaded version which will allow a user to use the same server to connect with multiple software tools.

In the project directory, create a configuration file for each statistical software you want talk2stat to use. For example, if you plan to use R, create a file called R_config which contains something like the following:

[SERVERCONFIG]
PORT = 65432
DEBUGFILE = Rdebug.txt
PIPETIMEOUT = 300

If you use talk2stat in multiple concurrent projects you must use a different port number for each. Note that currently the IP address of the server is hard-coded to 127.0.0.1. The debug file name (which contains the messages to and from R, in this case) can be the same across projects, since they are stored in different directories. The PIPETIMEOUT is used to specify how long (in seconds) the bidirectional pipe will wait for a response from the statistical package.

To start the server for a specific project we have to specify the location of the configuration file, and the selected language (a project can use more than one statistical package, but as mentioned above, each one uses a different invocation of the server). To start a server which listens to the selected inet port and connects to the selected statistical package (R, julia, matlab, python), use the following:

from talk2stat import server
server(dirname, language)

For example,

from talk2stat import server
server('myproject/', 'R')

The directory must contain a file called R_config. If you plan to use julia, create a file called julia_config and be sure to use a unique port number. Then you can run (a possibly concurrent) server process by running

from talk2stat import server
server('myproject/', 'julia')

The server will continue to run until you stop it (see below how.) This is useful to maintain a continuous session. This is faster and more efficient than invoking R/julia/matlab/python each time a code fragment is executed. The server() function can take an optional third argument called doFork (default = True). If set to False, the server will still continue to run, but it will be blocking and will not return to prompt-mode until the server is stopped. When the server is started by a latex compiler, it must be in non-blocking mode (doFork=True). The blocking mode is appropriate if the server is started manually at a terminal.

Setting the `messages' argument in the server function to True will result in the user session messages to be printed to the screen.

To connect to a running server, we use the client() function, like this:

from talk2stat import client
client(dirname, language, inputfile)

where inputfile contains the code fragment or program to be executed. The output from the server is printed to stdout. The code can also generate files (e.g. graphics, reports).

To stop the server, we send the following message (using our previous example) through the client:

client('myproject/','R','QUIT')

Authors

  • Haim Bar - Initial work

License

MIT

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

talk2stat-0.0.3.tar.gz (5.3 kB view details)

Uploaded Source

Built Distribution

talk2stat-0.0.3-py3-none-any.whl (6.1 kB view details)

Uploaded Python 3

File details

Details for the file talk2stat-0.0.3.tar.gz.

File metadata

  • Download URL: talk2stat-0.0.3.tar.gz
  • Upload date:
  • Size: 5.3 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.2.0 pkginfo/1.5.0.1 requests/2.24.0 setuptools/47.3.1 requests-toolbelt/0.9.1 tqdm/4.46.1 CPython/3.8.3

File hashes

Hashes for talk2stat-0.0.3.tar.gz
Algorithm Hash digest
SHA256 11046f31963e5e290424f700d80e086a50e44dfd3dbe276c5db076bc8104f478
MD5 f3ee3df97a37ef4519d85e2044703379
BLAKE2b-256 55b748db19a5fdc8a770bbb38d8d3a3582dabcb3ddb50938b6c22a11c9c55efa

See more details on using hashes here.

File details

Details for the file talk2stat-0.0.3-py3-none-any.whl.

File metadata

  • Download URL: talk2stat-0.0.3-py3-none-any.whl
  • Upload date:
  • Size: 6.1 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.2.0 pkginfo/1.5.0.1 requests/2.24.0 setuptools/47.3.1 requests-toolbelt/0.9.1 tqdm/4.46.1 CPython/3.8.3

File hashes

Hashes for talk2stat-0.0.3-py3-none-any.whl
Algorithm Hash digest
SHA256 e3e28f0d72dde357e772773e28d793f798219c0eb7ea50206ee8421a4367bb7e
MD5 dda2e866ea7f5c9d759e27d20ec942a5
BLAKE2b-256 571f58f94fea4496df1d2ad5cb8f1dcbb41dca199b1b0fba77127d9a5958b7d6

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page