Masthash

#numpy

thetestspecimen
6 days ago

Julia is the new upstart in the data science world, but can it keep up with the tried and tested combination of Python + NumPy + Numba?

https://towardsdatascience.com/is-julia-faster-than-python-and-numba-897c53694621

#DataScience #deeplearning #MachineLearning #Python #NumPy #Numba #JuliaLang

AskUbuntu
2 weeks ago

Why low swappines (10) leads to system crashes? #ram #swap #memoryusage #numpy

https://askubuntu.com/q/1486228/612

Erik W. Bjønnes
3 weeks ago

Spent the day programming #python for the first time in ages, and on the one hand it’s great how large the ecosystem is and how quickly I can get something up. On the other hand, it didn’t take long before I wished I was back in #RustLang or even #CPlusus..!

The amount of foot guns, trip mines and quicksand was #%^*! Luckily #numpy helped keep me on track for the most part.

Perhaps it’s time to check out using python libraries from rust…

Jens Hannemann
3 weeks ago

#3GoodThings for today:

1. All my students got their #SDR up and running
2. Sitting on the front porch hacking #Python #DSP #NumPY #SciPy #MatplotLib with a glass of wine
3. Chicken Tikka Masala sitting in the slow cooker for dinner

@threegoodthings

Fantastic NumPy resource that @melissawm has shown me:

From Python to Numpy by Nicolas P. Rougier
https://www.labri.fr/perso/nrougier/from-python-to-numpy/

I'm surely going to learn a lot and tap into this as I try to write about #Python + #NumPy for "lay people" like myself, in Portuguese.

Dessas loucuras que me acometem de tempos em tempos, hoje eu fiquei com vontade de começar a escrever sobre #NumPy para leigos.

Começando com as minhas imbatíveis credenciais: eu sou totalmente leigo! Acredito que isso pode ajudar um pouco, mas eu tenho um trunfo que é ter pessoas amigas que realmente entendem do assunto, como a incrível @melissawm e que eu acredito que (dado tempo o suficiente) vão me impedir de falar besteiras muito grandes (as besteiras pequenas são inevitáveis).

Você vai poder me acompanhar aqui:
https://hackmd.io/@villares/numpy-para-leigos

captura de tela dos primeiros parágrafos da página em https://hackmd.io/@villares/numpy-para-leigos

**proto-sumário**
- Introdução
    - Quem é você e por qual motivo está escrevendo isto aqui?
    - O que é NumPy, pra quê serve?
    - Outros recursos para estudar
- Mostre-me exemplos de coisas que você fez com numpy
    - Manipulando imagens
    - Aplicando máscaras com elementos translúcidos (alpha channel)
    -Trabalhando com muitos números do ruído de Perlin rapidamente
    - Calculando muitos pontos de curvas Bezier de uma vez
    - Simulando partículas
- Agora, mostre-me coisas realmente impressionantes

**Introdução**

[continua com 4 parágrafos]
Dan Gohman
1 month ago

On the latest Bytecode Alliance community stream, a look at componentize-py, including a demo of #NumPy running in a #Wasm component!

https://www.youtube.com/watch?v=lbZ0ywLkBDM

pi
1 month ago

#Python in #Excel (in Beta) #Microsoft 🤝 #Anaconda

Default imported libraries:
#matplotlib
#numpy
#pandas
#seaborn
#statsmodels

only for Windows, needs internet access, code executed on MS servers without network or file access

see https://aka.ms/python-in-excel-getting-started & https://www.anaconda.com/excel

Changelog
1 month ago

A #Python library to write reliable programs that interact with generative models (more like #NumPy than #LangChain)

🔗 https://github.com/normal-computing/outlines

How decomposing #4D objects into lower-dimensional faces helps to determine intersections and containment: https://onkeypress.blogspot.com/2023/08/hypergeometry-intersections-and.html Part of an ongoing project to extend #raytracing to higher dimensions. #CGI #Python #numpy

Michał Górny
1 month ago

Myślicie, że jest wam dzisiaj gorąco?

Najpierw walczyłem z nowym segfaultem w #pydantic 2 z Pythonem 3.12. Nie udało mi się zajść daleko, co najwyżej ustalić, że to #heisenbug.

https://github.com/pydantic/pydantic/issues/7181

Potem testowałem świeży snapshot #LLVM — tylko po to, by odkryć, że testy znów padly na 32-bitowych platformach. Po bisect'cie, okazało się, że przyczyną była zmiana "NFCi" (niezamierzająca zmian funkcjonalnych) do logiki hashowania — wygląda na to, że LLVM drukuje teraz funkcje w kolejności zależnej od platformy.

https://reviews.llvm.org/D158217#4600956

Na koniec walczyłem z segfaultem w testach #trimesh. Najwyraźniej jest to regresja związana z betą #numpy 1.26.0, więc wypracowałem backtrace i zgłosiłem błąd.

https://github.com/numpy/numpy/issues/24459

#Gentoo #Python

Michał Górny
1 month ago

You think it's hot today?

First I've been trying to figure out a new segfault in #pydantic 2 with #Python 3.12. I haven't been able to get far, except for establishing that it's a #heisenbug.

https://github.com/pydantic/pydantic/issues/7181

Then I've been testing a fresh #LLVM snapshot — only to discover the test suite is broken on 32-bit platforms again. After a bisect, it turned out that it was caused by a NFCi change to the hash logic — I guess that the function ordering depends on the platform now.

https://reviews.llvm.org/D158217#4600956

Finally, I've been figuring out a #trimesh segfault in tests. Apparently it's specific to #numpy 1.26.0 beta, so I've gotten a backtrace and filed a bug there.

https://github.com/numpy/numpy/issues/24459

#Gentoo

Sergi
1 month ago

I really liked @t_redactyl's talk about #Python optimization with #Numpy. I thought it was going to be the typical the typical "arrays are quicker than loops and that's it", but I didn't know about broadcasting and really liked the trick with sorting.

I also liked a lot the time taken to explain how lists work on memory vs arrays.

Check it out at https://youtu.be/8Nwk-elxdEQ

Tom Larrow
2 months ago

Today's #CreativeCoding is some inset rectangle packing. Rather than storing all these as objects as in a traditional packing algorithm, it is evaluating the pixel array as a numpy array, and then comparing the values and making sure they are all the same value

This is really inefficient, and takes more than an hour to generate an image, but this was more my way of learning more #Numpy functions and deepening my #python understanding #py5

Code: https://codeberg.org/TomLarrow/creative-coding-experiments/src/branch/main/x_0095

A dense pattern of squares, which have other squares inside them. They are all different sizes, and not in an ordered pattern
A dense pattern of squares, which have other squares inside them. They are all different sizes, and not in an ordered pattern. This one is a lighter grey than the other one
Andreas Dutzler
2 months ago

Why are the "batch" axes always the leading axes in NumPy? I designed all my packages to use the trailing axes as batch axes because this seems more natural to me. Now I'm thinking about switching to NumPy's convention - just to make things more intuitive for NumPy users. Any ideas on that? #python #batch #numpy

py5coding
2 months ago

Just added two new methods `to_pil()` and `get_np_pixels()` to the Sketch, Py5Graphics, and Py5Image classes in #py5. The first will return a PIL Image object and the second will get pixels as a #numpy array. Both further the goal of further integrating py5 into the #python ecosystem.

@villares@pynews.com.br #numpy vectorization is so magic...

def quadratic_points(ax, ay, bx, by, cx, cy, num_points=None, first_point=False):
if num_points is None:
num_points = int(py5.dist(ax, ay, bx, by) + py5.dist(bx, by, cx, cy) + py5.dist(ax, ay, cx, cy)) // 10
if num_points <= 2:
return [(ax, ay), (cx, cy)] if first_point else [(cx, cy)]
t = np.arange(0 if first_point else 1, num_points + 1) / num_points
x = (1 - t) * (1 - t) * ax + 2 * (1 - t) * t * bx + t * t * cx
y = (1 - t) * (1 - t) * ay + 2 * (1 - t) * t * by + t * t * cy
return np.column_stack((x, y))

Antonio One
2 months ago

#python #numpy #sqrt

The numpy.lib.scimath module provides a workaround for some of the limitations of the math module in Python. It includes a sqrt function that can handle negative numbers and returns complex results for negative inputs. A simple use case would be to calculate the square root of a negative number without raising a ValueError.

Antonio One
2 months ago

`numpy.lib.index_tricks` provides a set of classes and functions to construct arrays with different indices. `set_module` is a function decorator that assigns a module name to a given class or function. #python #numpy #set_module

`

Stark
2 months ago

@pixeltracker @guetto

For research projects where I use #NumPy and #MatPlotLib, I actually like using #Jupyter! It is just easier to run code and view my plots.

With #PyQt, you can even have widgets like sliders and other #Qt stuff.

It just speeds up my prototyping and makes me more productive. Naturally, only my plotting code and math exist in the .ipynb, and the rest is just imported from normal .py files. Thus, it allows for quick conversions once the prototyping is done.

@Python #Python

Stark
2 months ago

@folkerschamel

Just checked it out!

Is a weird one. Don't think I encountered any place or problem to use it except for mathematical operations, where I would normally just lean towards #Numpy.

But I'll keep it in mind. I like functions like these, which force me to change my solutions to use optimized built-ins rather than my own solutions.

@Python #Python

Today I learned that #PyGEOS and #shapely merged in 2021 and that brought ufunc numpy capabilities to shapely.

https://shapely.readthedocs.io/en/stable/ #Python #GIS #GEOS #computationalGeometry #Python #numpy

This is probably useless but, with a bit of #monkeyPatching, one can make Py5Image objects picklable... https://gist.github.com/villares/3e00c5c4e3366b18ebadb9073e46c6d1

#Processing #Python #numpy #py5 cc @py5coding

Barrett
2 months ago

I know it's common in #python to import #pandas as `pd` and #numpy as `np`, but I just hate it.

For some reason it takes me extra brain power to decipher `pd` to pandas when I'm reading over modules. I'd rather improve readability and type a bit more when I'm writing the program.

Is it just me?

Daniel Thorngren
2 months ago

So at least with #python on my computer, log is faster than log10 by a factor of two. Similar results obtained from #Numpy and #numba . Wat?

#programming

gram
2 months ago

@lsmith You can save a bit of memory by using tuples instead of lists and NamepdTuple instead of dicts (if you know in advance the keys of dicts).

If you can define the data structure more clearly, #numpy or #pandas will save even more memory for you. They now use pyarrow [1] inside which is very good at packing data.

[1]: https://arrow.apache.org/docs/python/index.html

#python

Jakub Synowiec
2 months ago

There is another quirk, not to say, an issue, in AWS Glue. In Python shell jobs, when using Python 3.9, pip is not aware of the pre-built analytics libraries when installing an external wheel. As a result, it resolves and downloads from the package registry even if the pre-built versions of pandas or numpy satisfy the wheel’s requirements.

#Python #AWS #AWSGlue #bug #pandas #numpy

https://docs.aws.amazon.com/glue/latest/dg/add-job-python.html

Tom Larrow
2 months ago

Sometimes I go into a #CreativeCoding session wanting to make something specific. Other times I wonder what will happen if I try something weird

This is one of those. I use #Py5 to draw short lines on the screen, then capture the pixel array in a #NumPy array. Then I use the NumPy command roll to literally roll those pixels values around the array writing them back to the canvas when they are in different positions. This is the result after thousands of positions

Code https://codeberg.org/TomLarrow/creative-coding-experiments/src/branch/main/x_0084

Various blended colors forming many overlapping peaks and valleys, as if entire mountain ranges were flattened into a 2d image and you could see through them.

It's also just sort of a chaotic mess. This one has a lot of lighter tones towards the top, with darker ones in the middle center
Various blended colors forming many overlapping peaks and valleys, as if entire mountain ranges were flattened into a 2d image and you could see through them.

This one is a lot darker with many more deep blues and dark purples
Various blended colors forming many overlapping peaks and valleys, as if entire mountain ranges were flattened into a 2d image and you could see through them.

This one has a lot of fire like reds, yellows and oranges
Various blended colors forming many overlapping peaks and valleys, as if entire mountain ranges were flattened into a 2d image and you could see through them.

This one is a pastel mashup
Folker
2 months ago

@DanielaKEngert @sentientmortal @fell @_alen

As a more serious note: As discussed by various people in various other posts, the performance of a program depends on so many things, the language itself is only one of many factors. And in reality a non-trivial software is often a combination of multiple programming languages anyway, like #python for complex high-levell code, and #cpp for specific performance-critical code. As done with #numpy or #tensorflow.

Clément Robert
2 months ago

#Cython 3.0 is (almost) out !

(binaries are being deployed right now, so it should be available in a couple hours)

https://github.com/cython/cython/releases/tag/3.0.0

This is exiting news for our ecosystem because it's the first stable version able to *not* generate deprecated C-API #numpy code. When it's widely adopted, Numpy devs will finally be able to move forward with performance optimisations that were not possible without breaking everyone's favourite package !

nerdzmania
2 months ago

(Learning) Machine Learning

I have been studying machine learning recently and trying out different tools for it, here are some observations about IDEs in relation with ML (on Windows):

1. No IDE, just using the Command Prompt terminal and web browsers. That's the one I like less. The command prompt terminal keeps forgetting all the commands I used in the previous sessions. There must be some workarounds for that but I don't care enough to look for them. Also, the default UI for Jupyter Notebook is pretty crappy, gotta install and set up some themes.

2. VS Code. This one I like the most. It works well but I keep getting some bogus warnings about libraries not being imported properly, but it works anyways so... 🤷‍♂️ Also the UI in VS code is all right I find.

3. PyCharm. The Community Edition only allows to view the notebooks in Read-Only mode. There is no way I'm paying for the Pro version so that's it for that one.

What are your favourite tools?

#ml #pandas #numpy #python #jupyter #notebook

Jupyter Notebook logo
Folker
2 months ago

@josephgruber @fell

The point of these exercises is to point out that it's too simplistic to simply say that programming lanuage X is better or faster than programming language Y.

Last time I had such discussions was as teenager.😉

Typically you use and combine various programming languages, e.g. #python (for high-level code) with #cpp (for performance critical low-level code). Textbook sample: #numpy. Or #tensorflow.

Folker
3 months ago

@fell In fact, another run looks like this.

But as a serious note: In my experience, in most #python programs the bottleneck is usually not the #python code itself. I'm a big fan of combining #python for most of the application combined with #cpp for performance critical code, which is usually much less code than you might think at first, and which you usually don't have to program anyway. For example, #numpy is implemented in #c/#cpp.

dillonniederhut
3 months ago

If you use #NumPy, upper bound your dependencies to <2.0 now.

Also, as of 1.25, you no longer need to use oldest-supported-numpy in your builds.

@scipy2023

Łukasz Langa
3 months ago

It still feels like magic to me that #NumPy allows slices that aren't "rectangular". They can be arbitrary index lists, which in the simplest form allows you to "draw" polygons by first selecting the pixels and then making a single assignment. That's what ski.draw.polygon does.

But it goes further, you can have really arbitrary lists of multi-dimensional indexes so they don't have to be continuous or anything. My simple high-school math mind can barely accept it 🤯

#Python

Clément Robert
3 months ago

This year is very intensive in terms of migrations for scientific Python maintainers:
- a new #CPython (3.12) in October
- a major version of #Cython (3.0) announced for this summer
- a major version of #Numpy (2.0) scheduled for the end of the year

All of which will require careful testing and probably take some time to propagate through the giant mess that are dependency trees in scientific #Python 😵‍💫

Steven Pestana
3 months ago

So what's the easiest way to handle time and date data with timezone information in #python (#pandas, #datetime, #numpy, or #xarray). I find myself switching back and forth between datetime64, Timestamp, adding timedelta or tzinfo haphazardly and have never really settled on what's the best way to handle these data. I'm primarily working with pandas dataframes or xarray datasets. #programmingHelp

Anyone using #Python for data analysis that is not using #pandas and/or #numpy?
What would you substitute them for?

Pekka Väänänen
3 months ago

Hello I'm Pekka and I do experiments in computer graphics, video art, and machine learning. I also read a lot. I mostly post about my hobby projects (or #books I'm reading).

At the moment I'm working on #homebrew #N64 stuff and making some mashups tracks. Occasionally also #demoscene coding with or without #shaders :)

#introduction #python #numpy #lowpoly

Cornell box on the Nintendo 64
The Mona Lisa converted to a low poly model.
A diagram of a machine learning system that uses tiny neural cellular automata (NCA) to combine a gbuffer+depth to a painterly color image.
An illustration showing how tree sprites are spawned in our demoscene production A New World Awaits (2021).

If you want fast numeric calculations in #Python, you'll often end up using #NumPy either directly or indirectly. But sometimes NumPy isn't fast enough—here's what to try next:

https://pythonspeed.com/articles/numpy-is-slow/

@TomLarrow, this is what I wanted to do and show you yesterday but didn't have the time:

https://github.com/villares/sketch-a-day/blob/main/2023/sketch_2023_06_24/sketch_2023_06_24.py #py5 #Python

It is a clever #NumPy masking strategy I learned from @hx2A that allows one to benefit from a transparent background (or even translucent objects) in the offscreen buffer image you draw before the clip/mask. In this case it just saves you from drawing a "visible circle region" but if you have lots of objects, or translucent objects, it can be very handy.

some clipped slices from translucent blue circle with concentric green stripes over a blue and green noisy-striped background.

Cheers @yorik thanks for you work on @FreeCAD !

Dear @hx2A thanks for your work on @py5coding !

Oi @melissawm thank you for your work on #numpy !

Hi @doctormo thank you for your work on @inkscape !

#FLOSS #FOSS #LibreSoftware #FreeSoftware #OpenSource

Temas que vocês iriam curtir para os meus próximos #fanzines #desenhoSemArgumentos desenho()
#py5 #numpy #shapely #trimesh #Arduino #FreeCAD

The extraction seems to be working really well, but can't tell how well yet

Has two repeating sections. One is very clearly good data that looks as expected

The other looks like noise, but is very repeatable and (partially) points in a way that true noise wouldn't point. Also partially larger than the real effect could be

What I really need is more example data. Fortunately bosses/partners are listening enough to make that happen (maybe)

#numerical #math #space #python #numpy #matplotlib

I haven't logged in to #StackOverflow in years, but I had to upvote this amazingly elegant answer

https://stackoverflow.com/a/33920320/678832

And it isn't just a good idea--it actually works!

#math #vector #python #numpy

gostei mais da mistura de hoje...
#Processing #Python #py5 #numpy #CreativeCoding (atualização: animação comprimiu mal, vou por uma imagem estática que dá pra ter uma ideia)

import py5
import numpy as np

def setup():
global npa, R, G, B
py5.size(600, 600)
npa = np.empty((py5.width, py5.height)).T
npa.fill(100)
R = dist_to_pos(py5.width, py5.height, 300, 300)
G = dist_to_pos(py5.width, py5.height, 200, 300)
B = dist_to_pos(py5.width, py5.height, 300, 200)

def draw():
rnd_r = np.random.randint(0, 300, size=(py5.width, py5.height)).T
rnd_g = np.random.randint(0, 300, size=(py5.width, py5.height)).T
rnd_b = np.random.randint(0, 300, size=(py5.width, py5.height)).T
img = np.dstack([R < rnd_r, G < rnd_g, B < rnd_b])
py5.set_np_pixels(img * 150, 'RGB')

def dist_to_pos(width, height, cx, cy):
""" reeturns a 2D array filled with distances """
x = np.arange(width)
y = np.arange(height)
xx, yy = np.meshgrid(x, y)
return np.linalg.norm(np.array([xx - cx, yy - cy]), axis=0)

py5.run_sketch()

imagem com núvem circular de pixels coloridos em um fundo preto, gerada pelo código no toot

Today I updated https://abav.lugaralgum.com/sketch-a-day that was stuck a few days behind... today's sketch...

#Processing #Python #py5 #numpy

# Animation made with the code below, random white dots on a gray background concentrated near the center.

import py5
import numpy as np

def setup():
    global npa, dst
    py5.size(600, 600)
    npa = np.empty((py5.width, py5.height)).T
    npa.fill(100)
    dst = dist_to_pos(py5.width, py5.height, 300, 300)
    
def draw():
    rnd = np.random.randint(0, 300, size=(py5.width, py5.height)).T
    img = dst < rnd
    py5.set_np_pixels(npa + img * 150, 'L')

def dist_to_pos(width, height, cx, cy):
    """ reeturns a 2D array filled with distances """
    x = np.arange(width)
    y = np.arange(height)
    xx, yy = np.meshgrid(x, y)
    return np.linalg.norm(np.array([xx - cx, yy - cy]), axis=0)

py5.run_sketch()

sketch_2022_05_30 #Processing #Python #py5 #numpy, same bitwise pattern strategy as yesterday's, learned from Naoki Tsutae.

#black and white patter that is reminiscent of a PCB, based on the code below
# https://twitter.com/ntsutae/status/1521190629769826304?s=20

import numpy as np
import py5

order = 500
power = 59
img = None

def setup():
    global x, y
    py5.size(1000, 1000)
    py5.no_smooth()
    x, y = np.meshgrid(np.arange(0, order), np.arange(0, order))

def draw():
    global img
    py5.background(0)
    pattern = func(x, y, py5.frame_count / 2)
    #img = py5.create_image_from_numpy(color_map[pattern], 'RGB')
    img = py5.create_image_from_numpy(pattern * 255, 'L', dst=img)
    py5.image(img, 0, 0, py5.width, py5.height)
    py5.window_title(f'{py5.get_frame_rate():.2f}')


@np.vectorize
def func(x, y, t):
    return int((t + ((x ^ y) ** (power / 10))) % 256)  > 32

def key_pressed():
    global power
    if py5.key_code == py5.UP:
        power += 1
    elif py5.key_code == py5.DOWN:
        power = max(power - 1, 1)
    elif py5.key == 's':
        py5.save_frame(f'out{order}-{power}.png')
    print(power)

py5.run_sketch(block=False)

#CreativeCoding #Processing #Python # This pattern strategy I learned from Naoki Tsutae
# https://openprocessing.org/user/154720?view=sketches&o=48#sk

import numpy as np #numpy
import py5 #py5

order = 500
power = 59

def setup():
global color_map, x, y
py5.size(1000, 1000)
py5.no_smooth()

color_map = np.array([
[py5.red(hsb(i)), py5.green(hsb(i)), py5.blue(hsb(i))]
for i in range(256)])
x, y = np.meshgrid(np.arange(0, order), np.arange(0, order))

def draw():
py5.background(0)
pattern = func(x, y)
img = py5.create_image_from_numpy(color_map[pattern], 'RGB')
py5.image(img, 0, 0, py5.width, py5.height)

def hsb(h, sat=255, bri=255):
py5.color_mode(py5.HSB)
return py5.color(h, sat, bri)

@np.vectorize
def func(x, y):
return int((x ^ y) ** (power / 10)) % 256

def key_pressed():
global power
if py5.key_code == py5.UP:
power += 1
elif py5.key_code == py5.DOWN:
power = max(power - 1, 1)
elif py5.key == 's':
py5.save_frame(f'out{order}-{power}.png')
print(power)

py5.run_sketch(block=False)

A colorful symmetric pattern of pixels generated by the code in the toot. Reminds me of a Persian rug, but the colors are more saturated. One can see echoes of squares and diagonals.

I really like @bitartbot an I always wanted to try some of the patterns with #py5 (#Processing + #Python), also #numpy vectorization seemed cool to try...

"""
one-bit pattern that is produced by the code below (an the code is shown in the image on the right inside Thonny IDE
"""

# based on https://botsin.space/@bitartbot/110448213578682186

import numpy as np
import py5

order = 500

def setup():
    py5.size(1000, 1000)
    py5.no_smooth()
    x, y = np.meshgrid(np.arange(0, order), np.arange(0, order))

    pattern = func(x, y)
    #print(pattern)
    img = py5.create_image_from_numpy(pattern * 255, 'L')
    py5.image(img, 0, 0, py5.width, py5.height)
    py5.save('out.png')

@np.vectorize
def func(x, y):
    try:
        return (~(int((x & y) / (x ^ 8)) | ((y * 10) % (~x)))) % 3 > 0
    except ZeroDivisionError:
        return 0

py5.run_sketch(block=False)

sketch_2022_05_23 #Processing #Python #py5 #numpy

"""
O código abaixo a esquerda no Thonny IDE e a imagem que ele produz de círculos concêntricos com degradê de cinza no anel mais interno até cores com um ruído azul, mais azuis no canto superior esquerdo, mais magenta no canto superior direito, amarelo no inferior direito e verde no inferior esquerdo.
"""

import py5
import numpy as np

def setup():
    py5.size(900, 600)
    w, h = py5.width, py5.height
    R = np.linspace(0, 255, py5.width).reshape(1, -1)
    G = np.linspace(0, 255, py5.height).reshape(-1, 1)
    B = np.random.uniform(128, 255, (py5.height, py5.width))
    A = np.array([[128 + py5.dist(w /2, h / 2, x, y) % 128
                   for x in range(py5.width)]
                   for y in range(py5.height)])
    rgba = np.dstack(np.broadcast_arrays(R, G, B, A))
    img = py5.create_image_from_numpy(rgba, 'RGBA');
    py5.image(img, 0, 0)
    
py5.run_sketch()

sketch_2022_05_22 #Processing #Python #py5 #numpy https://abav.lugaralgum.com/sketch-a-day

Water is maybe a tiny bit faster, but still very slow. I added text for the current material and I can pickle the numpy array and load it back.

screenshot of the sandbox simulator with some pixels at the bottom of a window with black backgorund. Some yellow formations resemble piles of sand. Some blue "water" and red vertical formations of "rock" (that doesn't flow).

Will I ever get a good #numpy intuition?

I was making some clumsy left-right comparisons and swaps with grid.T[1:] vs. grid.T[:-1] and it worked. Now I tried grid vs. np.roll(grid, ...) and I'm struggling to make the masks for the swap work :(

Feynman 🔴
4 months ago

I made an object recognition program in #python and the dataset seemed more suitable to recognize stuffs on the street so that’s where I end up. Great for educational purpose but definitely makes me think about the ethical abuses of #ai today and in a near future. #computervision #opencv #numpy #tensorflow #classification

sketch_2023_05_21 #Processing #Python #py5 #numpy #creativeCoding

My naive sandbox now has, beside sand, concrete, water and rock. Water is very slow... but OK. All under 100 lines of code!

https://github.com/villares/sketch-a-day/blob/main/2023/sketch_2023_05_21/sketch_2023_05_21.py

Animated screen capture with Thonny IDE with code on the left and the sandbox simulation on the right.

A U shaped concrete recipient gets water, sand is added all around. Holes are made to the recipient, water and sand fall. More water added. Water takes a long time to settle.

sketch_2022_05_20 update!

I couldn't resist fiddling a bit more, and it came out more realistic!
Code at https://abav.lugaralgum.com/sketch-a-day

#Processing #Python #numpy #py5

"""
Animated capture of the sand simulator, a square window with black background where the mouse creates gray grains or square chuncks of "sand" (pixels) when dragged, those fall and accumulate at the bottom forming triangular piles.
"""

import numpy as np
import py5

def setup():
 global grid
 py5.size(500, 500)
 grid = np.zeros((py5.width, py5.height), dtype=bool)

def draw():
 py5.set_np_pixels(grid * 128, 'L')
 update_grid()

def update_grid():
 for i in reversed(range(grid.shape[0]-1)):
  row, next_row = grid[i], grid[i+1]
  sand = row == 1
  next_row_empty = next_row == 0
  falling = sand & next_row_empty
  row[falling] = 0
  next_row[falling] = 1
 for i in reversed(range(grid.shape[0]-1)):
  row, next_row = grid[i], grid[i+1]  
  sand_right = row[1:] == 1
  next_left_empty = next_row[:-1] == 0
  falling_left = sand_right & next_left_empty
  row[1:][falling_left] = 0  
  next_row[:-1][falling_left] = 1
 for i in reversed(range(grid.shape[0]-1)):
  row, next_row = grid[i], grid[i+1]    
  sand_left = row[:-1] == 1
  next_right_empty = next_row[1:] == 0
  falling_right = sand_left & next_right_empty
  row[:-1][falling_right] = 0  
  next_row[1:][falling_right] = 1

def key_pressed():
 if py5.key == ' ':
  grid[:] = np.zeros((py5.width, py5.height), dtype=bool)

def mouse_dragged():
 x = py5.mouse_x % py5.width
 y = py5.mouse_y % py5.height
 if py5.is_key_pressed:
  grid[y-4:y+5,x-4:x+5] = 1
 else:
  grid[y][x] = 1

py5.run_sketch()

#Python #numpy #imageProcessing I have done this before but I can't remember how and I'm lazy:

Say you have a table/dict of ints to colors like this palette = {
1: rgb(100, 0, 200),
2: rgb(200, 100, 0),
3: rgb(0, 200, 100),
...}
# it could be just an array of tuples or a 2D array maybe... [[100, 0, 200)], [200, 100, 0], ...]

And I have another 2D array of ints that I want to convert to a stack of RGB arrays so as to make an image of them (with #Pillow, or in my case #py5)

Which would be an elegant way of doing it?

(Writing this I had an idea, but I have to get out to my weekend shift at Sesc... maybe I'll try it later)

cc @TomLarrow