Wrapper libraries and idea of "built on top of"

  • Thread starter fog37
  • Start date
  • #1
fog37
1,568
108
TL;DR Summary
Wrapper libraries and the backend software they support
Hello,
I understand that a software library is called "wrapper library" if it provides a level of abstraction to another library: we can use the wrapper library instead of using the backend library directly to have an easier and more convenient access to the backend library functionalities.
One example from the Python world is the pair of viz libraries Matplotlib and Seaborn. It is standard to always imported both two libraries together but we could import only one of them and use it alone.

Seaborn is built on top of matplotlib and is said to be a wrapper library for matplotlib. That means that seaborn can do everything that matplotlib does. Seaborn is just an easier to use interface (API) to use all the functionalities of matplotlib under the hood.
  • That said, why do we need to also import matplotlib if seaborn can do everything anyway? Is it simply because seaborn can only get so far and does not really access ALL the capabilities of the more sophisticated matplotlib? Matplotlib has a slightly harder syntax but we can tweak more with it. Is that correct?
  • Importing seaborn does not automatically import matplotlib. However, in the case of the Python libraries Keras and Tensorflow, Keras is the API and wrapper library for TF and Keras does not work without the Tensorflow engine. TF is always imported implicitly or explicitly and needs to be there...That is not the case for the pair matplotlib and seaborn...Why?
  • Do you have any other example of wrapper library in Python? Many libraries are built on top of other libraries. For example, pandas is built on top of numpy and matplotib. But "built on top" simply means that the these libraries started out from the skeleton of these other two libraries. It does not mean that pandas is a wrapper library for numpy or matplotlib...Correct?
Thank you!
 
Technology news on Phys.org
  • #2
It depends on the how they were designed and what things from the library are exposed to the enduser.

As an example, the lower level library may have defined some constants that are needed in the calls to the wrapper library so rather than redefining the constants, the designer felt it was better to require the import of the lower level library.

Wrapper libraries do not necessarily support all features of the lower level library, only those things that users are most interested in using and that support their usecases.

I have seen this used with the curses library where wrapper libraries provide character based gui widgets like checkboxes, selectable list objects... that curses doesn't provide. This saves the enduser from developing those widgets each time they are needed in their code.
 
  • #3
I have never used Seaborn, but here are my two cents after looking at the Seaborn documentation.
fog37 said:
TL;DR Summary: Wrapper libraries and the backend software they support

Hello,
I understand that a software library is called "wrapper library" if it provides a level of abstraction to another library: we can use the wrapper library instead of using the backend library directly to have an easier and more convenient access to the backend library functionalities.
One example from the Python world is the pair of viz libraries Matplotlib and Seaborn. It is standard to always imported both two libraries together but we could import only one of them and use it alone.
The Seaborn documentation says that it is based on Matplotlib and just provides an interface to it. So I believe that it will not work unless Matplotlib is installed.
fog37 said:
Seaborn is built on top of matplotlib and is said to be a wrapper library for matplotlib. That means that seaborn can do everything that matplotlib does.
No. Seaborn can only do the tasks that it has been designed to do. There is no reason to believe that it provides all the functionality or flexibility of Matplotlib.
fog37 said:
Seaborn is just an easier to use interface (API) to use all the functionalities of matplotlib under the hood.
"all the functionalities of Matplotlib"? What makes you think that it provides "all" of the functionalities?
fog37 said:
  • That said, why do we need to also import matplotlib if seaborn can do everything anyway?
Because Seaborn uses Matplotlib to do the work. It makes using some parts of Matplotlib easier. It almost certainly does not allow you to do everything that Matplotlib can do unless it also provides a direct pass-through of Matplotlib commands.
fog37 said:
  • Is it simply because seaborn can only get so far and does not really access ALL the capabilities of the more sophisticated matplotlib? Matplotlib has a slightly harder syntax but we can tweak more with it. Is that correct?
That would be my assumption.
fog37 said:
  • Importing seaborn does not automatically import matplotlib. However, in the case of the Python libraries Keras and Tensorflow, Keras is the API and wrapper library for TF and Keras does not work without the Tensorflow engine. TF is always imported implicitly or explicitly and needs to be there...That is not the case for the pair matplotlib and seaborn...Why?
Because different designers developed their programs differently. There is no law that it has to be done a certain way.
fog37 said:
  • Do you have any other example of wrapper library in Python? Many libraries are built on top of other libraries. For example, pandas is built on top of numpy and matplotib. But "built on top" simply means that the these libraries started out from the skeleton of these other two libraries.
I's not sure what you mean by "the skeleton of these other two libraries". Do you mean a subset?
fog37 said:
  • It does not mean that pandas is a wrapper library for numpy or matplotlib...Correct?
numpy is imported when pandas is used. I don't know if there is an official definition of "wrapper library".
 
  • #4
Seaborn doesn't have the full functionality of matplotlib; you have to call matplotlib functions directly in order to, for example, actually export a plot to an image file: see here.
 
  • Like
Likes FactChecker
  • #5
fog37 said:
It is standard to always imported both two libraries together but we could import only one of them and use it alone.
According to the documentation, Seaborn cannot be used without numpy, pandas, and mathplotlib:
https://seaborn.pydata.org/installing.html said:
The basic invocation of pip will install seaborn and, if necessary, its mandatory dependencies.

[...]

Mandatory dependencies​

 
  • Like
Likes FactChecker and fog37
  • #6
jack action said:
According to the documentation, Seaborn cannot be used without numpy, pandas, and mathplotlib:
I see. Thank you.

So numpy, pandas, matplotlib are required dependencies. Without them installed, seaborn does not work, i.e. it cannot be installed alone. However, it can be imported alone: in the code, I can simply type import seaborn as sns and everything works just fine. Under the hood, matplotlib and numpy do not get imported but do they still get "contacted" (bad term) by seaborn?
 
  • #7
fog37 said:
I see. Thank you.

So numpy, pandas, matplotlib are required dependencies. Without them installed, seaborn does not work, i.e. it cannot be installed alone. However, it can be imported alone: in the code, I can simply type import seaborn as sns and everything works just fine. Under the hood, matplotlib and numpy do not get imported but do they still get "contacted" (bad term) by seaborn?

Check the source code. seaborn.utils imports matplotlib, numpy and pandas:

Python:
"""Utility functions, mostly for internal use."""
import os
import re
import inspect
import warnings
import colorsys
from contextlib import contextmanager
from urllib.request import urlopen, urlretrieve

import numpy as np
import pandas as pd
import matplotlib as mpl
from matplotlib.colors import to_rgb
import matplotlib.pyplot as plt
from matplotlib.cbook import normalize_kwargs

from .external.version import Version
from .external.appdirs import user_cache_dir
 
  • Like
Likes DrClaude, FactChecker and fog37
  • #8
pasmith said:
Check the source code. seaborn.utils imports matplotlib, numpy and pandas:

Python:
"""Utility functions, mostly for internal use."""
import os
import re
import inspect
import warnings
import colorsys
from contextlib import contextmanager
from urllib.request import urlopen, urlretrieve

import numpy as np
import pandas as pd
import matplotlib as mpl
from matplotlib.colors import to_rgb
import matplotlib.pyplot as plt
from matplotlib.cbook import normalize_kwargs

from .external.version import Version
from .external.appdirs import user_cache_dir
Thank you! Looking into.

So the line import seaborn as sns does automatically import, under the cover, the libraries matplotlib, pandas, and numpy... but that does not mean we can use those other libraries and their methods without explicitly importing them as well...
 
  • Like
Likes jedishrfu
  • #9
fog37 said:
So the line import seaborn as sns does automatically import, under the cover, the libraries matplotlib, pandas, and numpy
Not really. The full description of how the import system works is here: https://docs.python.org/3/reference/import.html but in summary:

The import statement combines two operations; it searches for the named module, then it binds the results of that search to a name in the local scope.​

So when you do import seaborn as sns, what you are doing is binding the seaborn module to the name sns in your code's scope so that you can use its public methods.

Whether seaborn imports any other modules into its scope is completely irrelevant to you.
 
  • #10
fog37 said:
Thank you! Looking into.

So the line import seaborn as sns does automatically import, under the cover, the libraries matplotlib, pandas, and numpy... but that does not mean we can use those other libraries and their methods without explicitly importing them as well...
You should be able to use them if they are in the scope where you need them.
 

What are wrapper libraries?

Wrapper libraries are collections of pre-written code that provide a simplified interface for using a complex programming language or API. They "wrap" the original code in a more user-friendly and streamlined package.

Why are wrapper libraries useful?

Wrapper libraries are useful because they allow developers to easily access and use complex code without needing to have an in-depth understanding of how it works. This saves time and effort and makes it easier to integrate different tools and technologies into a project.

What is the idea of "built on top of"?

The idea of "built on top of" refers to the concept of using a wrapper library to simplify and enhance the functionality of an existing code or tool. It allows developers to build upon the foundation of the original code without needing to start from scratch.

What are some examples of wrapper libraries?

Some popular examples of wrapper libraries include jQuery, which simplifies JavaScript code, and Requests, which simplifies making HTTP requests in Python. Other examples include Firebase for working with databases and Bootstrap for creating responsive web designs.

What are the potential drawbacks of using wrapper libraries?

While wrapper libraries can be very helpful, they may also have limitations and drawbacks. These can include compatibility issues with different platforms or versions, limitations on customization, and a lack of understanding of how the underlying code actually works. It's important for developers to carefully evaluate and test wrapper libraries before integrating them into a project.

Similar threads

  • Programming and Computer Science
Replies
1
Views
1K
  • Programming and Computer Science
Replies
8
Views
967
  • Programming and Computer Science
Replies
7
Views
480
  • Programming and Computer Science
Replies
1
Views
1K
  • STEM Academic Advising
Replies
2
Views
1K
  • Programming and Computer Science
Replies
15
Views
1K
  • Programming and Computer Science
Replies
7
Views
6K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
1
Views
7K
  • Feedback and Announcements
6
Replies
183
Views
76K
Back
Top