Code Monkey home page Code Monkey logo

Comments (6)

choldgraf avatar choldgraf commented on June 19, 2024

Ah ha, I think I figured this out - one would need to write a custom encoder for this, yes?

E.g., I got this working with the Altair chart using the following encoder:

from scrapbook.encoders import registry as encoder_registry
# add encoder to the registry

class AltairEncoder(object):
    def encode(self, scrap):
        # scrap.data is any type, usually specific to the encoder name
        scrap = scrap._replace(data=scrap.data.to_dict())
        return scrap

    def decode(self, scrap):
        # scrap.data is one of [None, list, dict, *six.integer_types, *six.string_types]
        scrap = scrap._replace(data=alt.Chart.from_dict(scrap.data))
        return scrap
    
    
encoder_registry.register("altair", AltairEncoder())

Followed by

ch = alt.Chart(data=df).mark_point().encode(
    x='a',
    y='b'
)
sb.glue('chart', ch, 'altair')

Then I could do:

image

neat!

In addition, for things like just matplotlib figures, you could use:

sb.glue('mplfig', fig, 'display')

and then access the display data with:

# To grab the serialized image
nb.scraps['mplfig'].display['data']['image/png']

# To display again (if in another notebook)
nb.reglue('mplfig')

Let me know if that is correct, and if so then would it be useful for me to add an example to the docs showing these use-cases?

from scrapbook.

choldgraf avatar choldgraf commented on June 19, 2024

Just a note here to describe the use-case I have in mind.

Imagine that you're writing a paper after doing a collection of analyses with notebooks. You probably have multiple notebooks that have some markup in them, statistics that were run, and figures that were generated. You don't want to write your paper in those notebooks because they're too messy. However, you'd like to reference what happens in those notebooks so that there is one canonical source of truth, and so that the values / plots update as you update those notebooks.

I'm imagining using scrapbook for something like this. If you were writing the paper in rST, then with some custom rST directives, you could imagine things like

In this analysis, there was a significant effect (p=:scrapbook:`notebookID:scrapPValueID`). See Figure 1 for more details.

.. scrapbook-figure:
	:notebook: notebookID
	:scrap: scrapFigureID
	:name: Figure 1

	Caption

That could be a really interesting way to store pieces of an analyses in separate notebooks, and then read them in to a single "finished product" document

from scrapbook.

MSeal avatar MSeal commented on June 19, 2024

Sorry for the delayed response. Glad you got some time to play with the project!

For the first question you can actually save the plot as-is, it just requires an extra field to stop the data being saved as well as the display object:

data = np.random.randn(2, 100)
fig, ax = plt.subplots()
plt.close(fig)
scat = ax.scatter(*data, c=data[1])
sb.glue("fig1", fig, encoder='display', display=True)

The encoder='display' is required for purely display objects. If you don't close the figure it will render twice since the glue call also renders the plot.

Then you can load with:

nb = sb.read_notebook('figure_gen.ipynb')
nb.reglue('fig1')

I am planning on improving this so it's more intuitive / figures out what to do automatically with the user needing to specify -- some of that is in master but it's still got some issues to fix.

from scrapbook.

MSeal avatar MSeal commented on June 19, 2024

Same for the second altair example:

import altair as alt
from vega_datasets import data
source = data.movies.url

fig2 = alt.Chart(source).mark_bar().encode(
    alt.X("IMDB_Rating:Q", bin=True),
    y='count()',
)
sb.glue('fig2', fig2, encoder='display', display=True)

then:

nb.reglue('fig2')

from scrapbook.

MSeal avatar MSeal commented on June 19, 2024

I do like the idea of adding some encoders for capturing the data and the display for various charting libraries. The matplotlib example I posted above only captures the display content and not the chart information itself. It was trying to do that encoding with the initial exception you hit, not knowing how to convert the figure object to a jsonizable type. This means you have to save the raw data in a separate scrap in the current version if you wanted to replot the data in a different way.

from scrapbook.

MSeal avatar MSeal commented on June 19, 2024

That could be a really interesting way to store pieces of an analyses in separate notebooks, and then read them in to a single "finished product" document

Yep that should be possible to do with some interface additions to an rst or markdown rendering engine. You can also imagine pointing to a notebook in github or a document store as the source for the plots and data points.

from scrapbook.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.