Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add rload function #89

Closed
maedoc opened this issue Jul 26, 2019 · 8 comments
Closed

Add rload function #89

maedoc opened this issue Jul 26, 2019 · 8 comments
Assignees

Comments

@maedoc
Copy link

maedoc commented Jul 26, 2019

Summary:

Add a rload that reads a file generated by rdump.

Description:

Some of my workflows find use for reading an Rdump file back into memory, e.g. instead of rerunning an expensive script that generates the Rdump file in the first place. In pycmdstan I had a rload but it doesn't seem to have survived.

Additional Information:

From here it reads

def rload(fname):
    """Load a dict of data from an R dump format file.
    """
    with open(fname, 'r') as fd:
        lines = fd.readlines()
    data = {}
    for line in lines:
        lhs, rhs = [_.strip() for _ in line.split('<-')]
        if rhs.startswith('structure'):
            *_, vals, dim = rhs.replace('(', ' ').replace(')', ' ').split('c')
            vals = [float(v) for v in vals.split(',')[:-1]]
            dim = [int(v) for v in dim.split(',')]
            val = np.array(vals).reshape(dim[::-1]).T
        elif rhs.startswith('c'):
            val = np.array([float(_) for _ in rhs[2:-1].split(',')])
        else:
            try:
                val = int(rhs)
            except:
                try:
                    val = float(rhs)
                except:
                    raise ValueError(rhs)
        data[lhs] = val
    return data

and may find use within other library functions such as the read_metric function.

Current Version:

I just pip installed it, 0.4.1 I think.

@ahartikainen
Copy link
Contributor

ahartikainen commented Jul 26, 2019

Does this handle dataframe input?

Or can Stan handle dataframe input?

@maedoc
Copy link
Author

maedoc commented Jul 26, 2019

Does this handle dataframe input?

Unless dataframe means the A <- structure(1, 2, 3, 4, c=(2,2)) thing, no. I put this together by inspecting Rdump files meant for Stan models.

@ahartikainen
Copy link
Contributor

Ok, just making sure. At least that is what PyStan etc implement.

There are however some dataframe in rdump format in example models, and I have yet tried if that works for CmdStan / RStan. They are not working for PyStan.

@mitzimorris
Copy link
Member

will add this back in!

@mitzimorris mitzimorris mentioned this issue Jul 29, 2019
2 tasks
@mitzimorris
Copy link
Member

@ahartikainen - re:

some dataframe in rdump format in example models

which ones are these? there's stuff in repo example models that doesn't work - we need to flag them and do some cleanup.

@ahartikainen
Copy link
Contributor

@mitzimorris
Copy link
Member

thanks!
that example can only be run via RStan - Stan's parser (stan::io::dump) somehow accepts the input file, but the resulting parse is incorrect. will file an issue against the Stan io module.

@mitzimorris
Copy link
Member

for CmdStanPy, supporting Rdump data as described in Appendix D of CmdStan manual - not trying to parse R dataframes. Updated docstring for rload function specifying supported variable types.

@mitzimorris mitzimorris self-assigned this Jul 31, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants