VSCode + Python on a mac

As my experimentation continues, I wanted to get Visual Studio Code installed on a mac, and wanted to use python as the language of choice – main reason for the mac is to understand and explore the #ML libraries, runtimes, and their support on a mac (both natively and in containers – docker).

Now, Microsoft has a very nice tutorial to get VSCode setup and running on a mac, including some basic configuration (e.g. touchbar support). But when it comes to getting python setup, and running, that is a different matter. Whilst the tutorial is good, it doesn’t actually work and errors out.

Below is the code that Microsoft outlines in the tutorial for python. It essentially is the HelloWorld using packages and is quite simple; but this will fail and won’t work.

import matplotlib.pyplot as plt
import numpy as np

x = np.linspace(0, 20, 100)  # Create a list of evenly-spaced numbers over the range
plt.plot(x, np.sin(x))       # Plot the sine of each x point
plt.show()                   # Display the plot

When you run this, you will see an error that is something like the one outlined below.

2019-01-18 14:23:34.648 python[38527:919087] -[NSApplication _setup:]: unrecognized selector sent to instance 0x7fbafa49bf10
2019-01-18 14:23:34.654 python[38527:919087] *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[NSApplication _setup:]: unrecognized selector sent to instance 0x7fbafa49bf10'
*** First throw call stack:
(
	0   CoreFoundation                      0x00007fff521a1ecd __exceptionPreprocess + 256
	1   libobjc.A.dylib                     0x00007fff7e25d720 objc_exception_throw + 48
	2   CoreFoundation                      0x00007fff5221f275 -[NSObject(NSObject) __retain_OA] + 0
	3   CoreFoundation                      0x00007fff52143b40 ___forwarding___ + 1486
	4   CoreFoundation                      0x00007fff521434e8 _CF_forwarding_prep_0 + 120
	5   libtk8.6.dylib                      0x000000011523031d TkpInit + 413
	6   libtk8.6.dylib                      0x000000011518817e Initialize + 2622
	7   _tkinter.cpython-37m-darwin.so      0x0000000114fb2a0f _tkinter_create + 1183
	8   python                              0x0000000101836ba6 _PyMethodDef_RawFastCallKeywords + 230
	9   python                              0x00000001019772b1 call_function + 257
	10  python                              0x0000000101974daf _PyEval_EvalFrameDefault + 45215
	11  python                              0x0000000101968a42 _PyEval_EvalCodeWithName + 418
	12  python                              0x0000000101835867 _PyFunction_FastCallDict + 231
	13  python                              0x00000001018b9481 slot_tp_init + 193
	14  python                              0x00000001018c3441 type_call + 241
	15  python                              0x0000000101836573 _PyObject_FastCallKeywords + 179
	16  python                              0x000000010197733f call_function + 399
	17  python                              0x0000000101975052 _PyEval_EvalFrameDefault + 45890
	18  python                              0x0000000101836368 function_code_fastcall + 120
	19  python                              0x0000000101977265 call_function + 181
	20  python                              0x0000000101974daf _PyEval_EvalFrameDefault + 45215
	21  python                              0x0000000101968a42 _PyEval_EvalCodeWithName + 418
	22  python                              0x0000000101835867 _PyFunction_FastCallDict + 231
	23  python                              0x0000000101839782 method_call + 130
	24  python                              0x00000001018371e2 PyObject_Call + 130
	25  python                              0x00000001019751c6 _PyEval_EvalFrameDefault + 46262
	26  python                              0x0000000101968a42 _PyEval_EvalCodeWithName + 418
	27  python                              0x0000000101836a73 _PyFunction_FastCallKeywords + 195
	28  python                              0x0000000101977265 call_function + 181
	29  python                              0x0000000101974f99 _PyEval_EvalFrameDefault + 45705
	30  python                              0x0000000101836368 function_code_fastcall + 120
	31  python                              0x0000000101977265 call_function + 181
	32  python                              0x0000000101974f99 _PyEval_EvalFrameDefault + 45705
	33  python                              0x0000000101968a42 _PyEval_EvalCodeWithName + 418
	34  python                              0x0000000101836a73 _PyFunction_FastCallKeywords + 195
	35  python                              0x0000000101977265 call_function + 181
	36  python                              0x0000000101974f99 _PyEval_EvalFrameDefault + 45705
	37  python                              0x0000000101968a42 _PyEval_EvalCodeWithName + 418
	38  python                              0x0000000101836a73 _PyFunction_FastCallKeywords + 195
	39  python                              0x0000000101977265 call_function + 181
	40  python                              0x0000000101974daf _PyEval_EvalFrameDefault + 45215
	41  python                              0x0000000101968a42 _PyEval_EvalCodeWithName + 418
	42  python                              0x00000001019cc9a0 PyRun_FileExFlags + 256
	43  python                              0x00000001019cc104 PyRun_SimpleFileExFlags + 388
	44  python                              0x00000001019f7edc pymain_main + 9148
	45  python                              0x0000000101808ece main + 142
	46  libdyld.dylib                       0x00007fff7f32bed9 start + 1
	47  ???                                 0x0000000000000003 0x0 + 3
)
libc++abi.dylib: terminating with uncaught exception of type NSException

[Done] exited with code=null in 1.017 seconds

The main reason this fails is that one has to be a little more explicit with matplot (the library that we are trying to use). Matplot has this concept of backends, which essentially is the runtime dependencies needed to support various execution environments – including both interactive and non-interactive environments.

For matplot to work on a mac, the raster graphics c++ library that it uses is based on something called Anti-Grain Geometry (AGG). And for the library to render, we need to be explicit on which agg to use (there are multiple raster libraries).

In addition on a mac OS X there is a limitation when rendering in OSX windows (presently lacks blocking show() behavior when matplotlib is in non-interactive mode).

To get around this, we explicitly tell matplot to use the specific agg (“TkAgg in our case) and then it will all work. I have a updated code sample below, which adds more points, and also waits for the console input, so one can see what the output looks like.

import matplotlib
matplotlib.use("TkAgg")
from matplotlib import pyplot as plt
import numpy as np

def waitforuser():
    input("Press enter to continue ...")
    return

x = np.linspace(0, 50, 200)  # Create a list of evenly-spaced numbers over the range
y = np.sin(x)

print(x)
waitforuser()

print(y)
waitforuser()

plt.plot(x,y)
plt.show()

And incase you are wondering what it looks like, below are a few screenshots showing the output.

To get everything working, make sure you setup the Linting, debugger, and the python environment properly. And of course, you can go nuts with containers! Happy coding!

Azure Cognitive Services in containers is the smart way to go

{Cross posted from my post on Avanade}

Containers just got smarter.

That’s the news from Microsoft, which announced recently that Azure Cognitive Services now supports containers. The marriage of AI and containers is a technology story, of course, but it’s a potentially even bigger business story, one that affects where and how you can do business and gain competitive advantage.

First, the technology story
Containers aren’t new, of course. They’re an increasingly popular technology with a big impact on business. That’s because they boost the agility and flexibility with which a business can roll out new tools to employees and new products and services to customers.

With containers, a business can get software releases and changes out faster and more frequently, increasing its competitive advantage. Because containers abstract applications from their underlying operating systems and other services—like virtual machines abstracted from hardware—those applications can run anywhere: in the cloud, on a laptop, in a kiosk or in an intelligent Internet-of-Things (IoT) edge device in the field.

In many respects this frees up the application’s developer, who can focus on creating the best, most useful software for the business. With Microsoft’s announcement, that software can now more easily include object detection, vision recognition, text and language understanding.

At Avanade, we take containers a step further by including support for them in our modern engineering platform, a key part of our overall approach to intelligent IT. So, you can automate your creation and management of containers—including AI-enabled containers—for a faster, easier, more seamless DevOps process. You can take greater advantage of IoT capabilities and move technologies such as AI closer to the edge, where they can reduce latency and boost performance.

What AI containers do for business
And you can do much more, which is where the business story gets interesting. With the greater agility and adaptability that comes with container-based AI services, you can respond more quickly to new competition, regulatory environments and business models. That contrasts with the more limited responses that have been possible with traditional, cloud-based AI. 

For example, data sovereignty laws and GDPR requirements generally restrict the transfer of data to the cloud, where cloud-based cognitive services can interact with it. Now, with containers that support cognitive services, you can avoid those restrictions by running your services locally.

A retail bank might use containerized AI to identify customers, address their needs, process payments and offer additional services, boosting customer satisfaction and bank revenue—all without sending private financial data outside the region (or even outside the bank) in accordance with GDPR.

Similarly, regional medical centers and clinics subject to HIPAA privacy laws in the US can process protected information on site with containerized AI to cut patient wait times and deliver better health outcomes.

Or, think about limited-connectivity or disconnected environments—such as manufacturing shop floors, remote customer sites or oil rigs or tankers—that can’t count on accessing AI that resides in the always-on cloud. Previously, these sites might have had to batch their data to process it during narrow periods of cloud connectivity, with the delays greatly limiting the timeliness and usefulness of AI.

Now, these sites can combine IoT and AI to anticipate and respond to manufacturing disruptions before they occur, increasing safety, productivity and product quality while reducing errors and costs.

If you can’t bring your data to your AI, now you can bring your AI to your data. That’s the message of container-hosted AI and the modern engineering platform. Together, they optimize your ability to bring AI into environments where you can’t count on the cloud. Using AI where you couldn’t before makes innovative solutions possible—and innovative solutions deliver competitive advantage. 

Boost ROI and scale
If you’re already using Azure Cognitive Services, you’ve invested time and money to train the models that support your use cases. Because those models are now portable, you can take advantage of them in regulated, limited-connectivity and disconnected environments, increasing your return on that investment. 

You can also scale your use of AI with a combination of cloud- and container-based architectures. That enables you to apply the most appropriate architectural form for any given environment or use. At the same time, you’re deploying consistent AI technology across the enterprise, increasing reliability while decreasing your operating cost.

Keep in mind…

Here are three things to keep in mind as you think about taking advantage of this important news:

  1. Break the barriers between your data scientists and business creatives. Containerized cognitive services is about far more than putting AI where you couldn’t before. It’s about using it in exciting new ways to advance the business. Unless you have heterogeneous teams bringing diverse perspectives to the table, you may miss some of the most important innovation possibilities for your business.
  2. You need a cloud strategy that’s not just about the cloud. If you don’t yet have a cloud strategy, you’re behind the curve. But if your cloud strategy is limited to the cloud, you may be about to fall behind the next curve. Microsoft’s announcement is further proof that the cloud is crucial to the enterprise—and also part of a larger environment, including both legacy and edge platforms, with which it must integrate.
  3. Be prepared for the ethics issues. Putting cognitive services in places you couldn’t before could raise new ethics issues. After all, we’re talking about the ability to read people’s expressions and even their emotions. This shouldn’t put you off—but it should put you on alert. Plug your ethics committee into these discussions when appropriate. If you don’t already have an ethics committee, create one. But that’s another post. 🙂

Want to learn more?

Microsoft’s announcement furthers the democratization of AI: the use of AI in more places and in more ways throughout the enterprise and beyond. Whether you turn to us for your AI solutions or look to us to assist you in developing your own, we’re ready to help with the greatest concentration of Microsoft expertise outside of Microsoft itself.