Skip to content

Commit

Permalink
Merge pull request #238 from thyneb19/Database-Executor
Browse files Browse the repository at this point in the history
Additional Master branch changes, Update to travis.yml
  • Loading branch information
thyneb19 committed Jan 21, 2021
2 parents 56ff766 + 5d9a999 commit 105942a
Show file tree
Hide file tree
Showing 24 changed files with 1,369 additions and 19 deletions.
8 changes: 8 additions & 0 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,14 @@ install:
- pip install jupyter-client==6.1.6
- pip install -r requirements.txt
- pip install -r requirements-dev.txt
#command to generate postgres database
before_script:
- psql -c "ALTER USER postgres WITH PASSWORD 'lux';" -U postgres
- psql -c "ALTER USER postgres WITH SUPERUSER;" -U postgres
- psql -c "ALTER DATABASE postgres OWNER TO travis;"
- psql -c "DROP schema public cascade;" -U postgres
- psql -c "CREATE schema public;" -U postgres
- psql -c "CREATE DATABASE postgres;" -U postgres
# command to run tests
script:
- python lux/data/upload_car_data.py
Expand Down
14 changes: 14 additions & 0 deletions doc/source/guide/FAQ.rst
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,20 @@ How do I set the Lux widgets to show up on default?
.. code-block:: python
lux.config.default_display = "pandas"
How do I change the plotting library used for visualization?
"""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""
By default, we make use of `Altair <https://altair-viz.github.io/>`__ to generate `Vega-Lite <https://vega.github.io/vega-lite>`__ visualizations. We can modify the :code:`plotting_backend` config property to use `Matplotlib <https://matplotlib.org/>`__ as the plotting library instead:

.. code-block:: python
lux.config.plotting_backend = "matplotlib"
To switch back to Vega-Lite:

.. code-block:: python
lux.config.plotting_backend = "vegalite"
I want to change the opacity of my chart, add title, change chart font size, etc. How do I modify chart settings?
"""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""
Expand Down
23 changes: 22 additions & 1 deletion doc/source/guide/style.rst
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,8 @@ To change the plot configuration in Altair, we need to specify a function that t
Let's say that we want to change all the graphical marks of the charts to green and add a custom title. We can define this `change_color_add_title` function, which configures the chart's mark as green and adds a custom title to the chart.

.. code-block:: python
lux.config.plotting_backend = "altair" # or 'vegalite'
def change_color_add_title(chart):
chart = chart.configure_mark(color="green") # change mark color to green
chart.title = "Custom Title" # add title to chart
Expand All @@ -43,6 +44,26 @@ We now see that the displayed visualizations adopt these new imported settings.
:width: 700
:align: center

Similarly, we can change the plot configurations for Matplotlib charts as well.
The plot_config attribute for Matplotlib charts takes in both the figure and axis as parameters.
.. code-block:: python
lux.config.plotting_backend = "matplotlib" # or 'matplotlib_code'
def add_title(fig, ax):
ax.set_title("Test Title")
return fig, ax
.. code-block:: python
lux.config.plot_config = add_title
We now see that the displayed visualizations adopt these new imported settings.

.. image:: ../img/style-7.png
:width: 700
:align: center

If we click on the visualization for `Displacement` v.s. `Weight` and export it. We see that the exported chart now contains code with these additional plot settings at the every end.

.. code-block:: python
Expand Down
Binary file added doc/source/img/style-7.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
76 changes: 62 additions & 14 deletions doc/source/reference/config.rst
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,45 @@ If you try to set the default_display to anything other than 'lux' or 'pandas,'
:width: 700
:align: center

Change plotting backend for rendering visualizations in Lux
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

We can set the :code:`plotting_backend` config to change the plotting library used for rendering the visualizations in Lux.
This is often useful not just for stylizing plot aesthetics, but also to change the code generated when `exporting a visualization <https://lux-api.readthedocs.io/en/latest/source/guide/export.html>`__.
For example, if you are more familiar with `matplotlib <https://matplotlib.org/>`__ , you may want to use a matplotlib plotting backend so that you can make use of the exported visualization code. In the following code, we set the plotting backend to 'matplotlib', and Lux will display the Matplotlib rendered charts.

.. code-block:: python
lux.config.plotting_backend = "matplotlib"
df
.. image:: https://github.com/lux-org/lux-resources/blob/master/doc_img/vislib-1.png?raw=true
:width: 700
:align: center

We can set the vislib back to the default 'vegalite,' which uses Vega-Lite to render the displayed chart.

.. code-block:: python
lux.config.plotting_backend = "vegalite"
df
.. image:: https://github.com/lux-org/lux-resources/blob/master/doc_img/display-1.png?raw=true
:width: 700
:align: center

Lux currently only support Vega-Lite and matplotlib, and we plan to add support for other plotting libraries in the future. If you try to set the :code:`plotting_backend` to anything other than 'matplotlib' or 'vegalite', a warning will be shown, and the display will default to the previous setting.

.. code-block:: python
lux.config.plotting_backend = "notvegalite" # Throw an warning
df
.. image:: https://github.com/lux-org/lux-resources/blob/master/doc_img/vislib-2.png?raw=true

:width: 700
:align: center

Change the sampling parameters of Lux
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

Expand Down Expand Up @@ -91,20 +130,8 @@ We can disable this feature and revert back to using a scatter plot by running t
lux.config.heatmap = False
Default Renderer
~~~~~~~~~~~~~~~~~

Charts in Lux are rendered using `Altair <https://altair-viz.github.io/>`__. We are working on supporting plotting via `matplotlib <https://matplotlib.org/>`__ and other plotting libraries.

To change the default renderer, run the following code block:

.. code-block:: python
lux.config.renderer = "matplotlib"
Plot Configurations
~~~~~~~~~~~~~~~~~~~
Changing the plot styling
~~~~~~~~~~~~~~~~~~~~~~~~~~

Altair supports plot configurations to be applied on top of the generated graphs. To set a default plot configuration, first write a function that can take in a `chart` and returns a `chart`. For example:

Expand All @@ -129,6 +156,27 @@ The above results in the following changes:

See `this page <https://lux-api.readthedocs.io/en/latest/source/guide/style.html>`__ for more details.

Matplotlib also supports plot configurations to be applied on top of the generated graphs. To set a default plot configuration, first write a function that can take in a `fig` and 'ax' and returns a `fig` and 'ax. For example:

.. code-block:: python
def add_title(fig, ax):
ax.set_title("Test Title")
return fig, ax
.. code-block:: python
lux.config.plot_config = add_title
The above results in the following changes:

.. image:: https://github.com/lux-org/lux-resources/blob/master/doc_img/style-7.png?raw=true
:width: 600
:align: center

See `this page <https://lux-api.readthedocs.io/en/latest/source/guide/style.html>`__ for more details.


Modify Sorting and Ranking in Recommendations
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

Expand Down
1 change: 1 addition & 0 deletions doc/source/reference/gen/lux._config.config.Config.rst
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ lux.\_config.config.Config

~Config.default_display
~Config.heatmap
~Config.plotting_backend
~Config.sampling
~Config.sampling_cap
~Config.sampling_start
Expand Down
2 changes: 2 additions & 0 deletions doc/source/reference/gen/lux.vis.Vis.Vis.rst
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,8 @@ lux.vis.Vis.Vis
~Vis.to_Altair
~Vis.to_VegaLite
~Vis.to_code
~Vis.to_matplotlib
~Vis.to_matplotlib_code



Expand Down
70 changes: 70 additions & 0 deletions doc/source/reference/lux.vislib.matplotlib.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
lux.vislib.matplotlib package
=============================

Submodules
----------

lux.vislib.matplotlib.BarChart module
-------------------------------------

.. automodule:: lux.vislib.matplotlib.BarChart
:members:
:undoc-members:
:show-inheritance:

lux.vislib.matplotlib.Heatmap module
------------------------------------

.. automodule:: lux.vislib.matplotlib.Heatmap
:members:
:undoc-members:
:show-inheritance:

lux.vislib.matplotlib.Histogram module
--------------------------------------

.. automodule:: lux.vislib.matplotlib.Histogram
:members:
:undoc-members:
:show-inheritance:

lux.vislib.matplotlib.LineChart module
--------------------------------------

.. automodule:: lux.vislib.matplotlib.LineChart
:members:
:undoc-members:
:show-inheritance:

lux.vislib.matplotlib.MatplotlibChart module
--------------------------------------------

.. automodule:: lux.vislib.matplotlib.MatplotlibChart
:members:
:undoc-members:
:show-inheritance:

lux.vislib.matplotlib.MatplotlibRenderer module
-----------------------------------------------

.. automodule:: lux.vislib.matplotlib.MatplotlibRenderer
:members:
:undoc-members:
:show-inheritance:

lux.vislib.matplotlib.ScatterChart module
-----------------------------------------

.. automodule:: lux.vislib.matplotlib.ScatterChart
:members:
:undoc-members:
:show-inheritance:


Module contents
---------------

.. automodule:: lux.vislib.matplotlib
:members:
:undoc-members:
:show-inheritance:
25 changes: 24 additions & 1 deletion lux/_config/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@ class Config:

def __init__(self):
self._default_display = "pandas"
self.renderer = "altair"
self.plot_config = None
self.SQLconnection = ""
self.executor = None
Expand All @@ -30,6 +29,7 @@ def __init__(self):
self._sampling_cap = 30000
self._sampling_flag = True
self._heatmap_flag = True
self._plotting_backend = "vegalite"
self._topk = 15
self._sort = "descending"
self._pandas_fallback = True
Expand Down Expand Up @@ -261,6 +261,29 @@ def default_display(self, type: str) -> None:
stacklevel=2,
)

@property
def plotting_backend(self):
return self._plotting_backend

@plotting_backend.setter
def plotting_backend(self, type: str) -> None:
"""
Set the widget display to show Vegalite by default or Matplotlib by default
Parameters
----------
type : str
Default display type, can take either the string `vegalite` or `matplotlib` (regardless of capitalization)
"""
if type.lower() == "vegalite" or type.lower() == "altair":
self._plotting_backend = "vegalite"
elif type.lower() == "matplotlib":
self._plotting_backend = "matplotlib"
else:
warnings.warn(
"Unsupported plotting backend. Lux currently only support 'altair', 'vegalite', or 'matplotlib'",
stacklevel=2,
)

def _get_action(self, pat: str, silent: bool = False):
return lux.actions[pat]

Expand Down
4 changes: 2 additions & 2 deletions lux/core/frame.py
Original file line number Diff line number Diff line change
Expand Up @@ -710,7 +710,7 @@ def current_vis_to_JSON(vlist, input_current_vis=""):
current_vis_spec = {}
numVC = len(vlist) # number of visualizations in the vis list
if numVC == 1:
current_vis_spec = vlist[0].to_code(prettyOutput=False)
current_vis_spec = vlist[0].to_code(language=lux.config.plotting_backend, prettyOutput=False)
elif numVC > 1:
pass
return current_vis_spec
Expand All @@ -725,7 +725,7 @@ def rec_to_JSON(recs):
if len(rec["collection"]) > 0:
rec["vspec"] = []
for vis in rec["collection"]:
chart = vis.to_code(prettyOutput=False)
chart = vis.to_code(language=lux.config.plotting_backend, prettyOutput=False)
rec["vspec"].append(chart)
rec_lst.append(rec)
# delete since not JSON serializable
Expand Down
11 changes: 11 additions & 0 deletions lux/utils/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
# See the License for the specific language governing permissions and
# limitations under the License.
import pandas as pd
import matplotlib.pyplot as plt


def convert_to_list(x):
Expand Down Expand Up @@ -103,3 +104,13 @@ def like_nan(val):
import math

return math.isnan(val)


def matplotlib_setup(w, h):
plt.ioff()
fig, ax = plt.subplots(figsize=(w, h))
ax.set_axisbelow(True)
ax.grid(color="#dddddd")
ax.spines["right"].set_color("#dddddd")
ax.spines["top"].set_color("#dddddd")
return fig, ax
Loading

0 comments on commit 105942a

Please sign in to comment.