eshangj/stackoverflow_q_and_a_sample huggingface dataset:
Question: I'm trying to plot a value around the unit sphere using surface plot and facecolors in matplotlib, but my colorbar shows the normalized values instead of the real values. How can I fix this so the colorbar has the right range? import numpy as np import matplotlib.pyplot as plt import matplotlib as mpl from matplotlib import cm fig, ax = plt.subplots(subplot_kw={"projection": "3d"}, figsize = (10, 14)) # Make data n_points = 500 r = 1 u = np.linspace(0, 2 * np.pi, n_points) v = np.linspace(0, np.pi, n_points) x = r * np.outer(np.cos(u), np.sin(v)) y = r * np.outer(np.sin(u), np.sin(v)) z = r * np.outer(np.ones(np.size(u)), np.cos(v)) ax.plot_wireframe(x, y, z, color="grey", alpha = 0.1) data = np.random.uniform(0.2, 0.5, n_points) heatmap = np.array(np.meshgrid(data, data))[1] ax.set_aspect("equal") ax.set_xlabel('x') ax.set_ylabel('y') ax.set_zlabel('z') colormap = cm.viridis normaliser = mpl.colors.Normalize(vmin=np.min(heatmap), vmax=np.max(heatmap)) print(np.min(heatmap)) print(np.max(heatmap)) surf = ax.plot_surface( x, y, z, facecolors=colormap(normaliser(heatmap)), shade=False) fig.colorbar(surf, shrink=0.5, aspect=10, label="Singlet yield", pad = 0.05, norm = normaliser) plt.show()  This outputs 0.20009725794516225 and 0.49936395079063567 as min and max in the prints, but you can see the range of the colorbar is 0 to 1 in the following image.  How can I fix this issue and make it so the colorbar has the appropriate colors?

Answer: The colorbar function itself doesn't have a norm argument according to the documentation for this function. For minimal alteration, you can pass a matplotlib.cm.ScalarMappable as the first argument of the colorbar call and it works as expected (presuming you also pass the appropriate ax argument). Here is a fully runnable code demonstrating this: import numpy as np import matplotlib.pyplot as plt import matplotlib as mpl from matplotlib import cm fig, ax = plt.subplots(subplot_kw={"projection": "3d"}, figsize=(10, 14)) # Make data n_points = 500 r = 1 u = np.linspace(0, 2 * np.pi, n_points) v = np.linspace(0, np.pi, n_points) x = r * np.outer(np.cos(u), np.sin(v)) y = r * np.outer(np.sin(u), np.sin(v)) z = r * np.outer(np.ones(np.size(u)), np.cos(v)) ax.plot_wireframe(x, y, z, color="grey", alpha=0.1) data = np.random.uniform(0.2, 0.5, n_points) heatmap = np.array(np.meshgrid(data, data))[1] ax.set_aspect("equal") ax.set_xlabel('x') ax.set_ylabel('y') ax.set_zlabel('z') colormap = cm.viridis normaliser = mpl.colors.Normalize(vmin=np.min(heatmap), vmax=np.max(heatmap)) print(np.min(heatmap)) print(np.max(heatmap)) surf = ax.plot_surface( x, y, z, facecolors=colormap(normaliser(heatmap)), shade=False) mappable = cm.ScalarMappable(norm=normaliser, cmap=colormap) fig.colorbar(mappable, ax=ax, shrink=0.5, aspect=10, label="Singlet yield", pad=0.05) plt.show()  Here is the output it generates:
Question: I am trying to understand how and when return values from pytest fixtures are cached. In my understanding, the goal of fixtures (in particular session-scoped fixtures) is that they are called only once and that return values are cached for future calls. This does not seem to be the case for nested parameterized fixtures. The following code shows the issue: from collections import Counter import pytest state = Counter() @pytest.fixture(scope="session", autouse=True) def setup_session(): yield None print(state) @pytest.fixture(scope="session", params=["A1", "A2", "A3"]) def first(request): state[request.param] += 1 return request.param @pytest.fixture(scope="session", params=["B1", "B2"]) def second(first, request): return first + request.param @pytest.fixture(scope="session", params=["C1", "C2"]) def third(second, request): return second + request.param def test_length(third): assert len(third) == 6  The output is Counter({'A1': 3, 'A2': 2, 'A3': 1}). So first is called three times for parameter value A1, two times for A2, and once for A3. Why? I am expecting to get Counter({'A1': 1, 'A2': 1, 'A3': 1}) - one call for each parameter value. In case that's relevant, I am using Python 3.12.3, pytest-8.3.4 PS: All 12 tests pass - that's fine. PPS: When removing one level of nesting, the problem disappears and first is called exactly once per parameter value.

Answer: From the docs (Note box)  Pytest only caches one instance of a fixture at a time, which means that when using a parametrized fixture, pytest may invoke a fixture more than once in the given scope.  If you look closely at the console output you will see that you count the changes in the returned value from first test_length[C1-B1-A1] -> {'A1': 1, 'A2': 0, 'A3': 0} test_length[C1-B1-A2] -> {'A1': 1, 'A2': 1, 'A3': 0} test_length[C1-B2-A2] test_length[C2-B2-A2] test_length[C2-B2-A1] -> {'A1': 2, 'A2': 1, 'A3': 0} test_length[C2-B2-A3] -> {'A1': 2, 'A2': 1, 'A3': 1} test_length[C2-B1-A3] test_length[C1-B2-A3] test_length[C1-B1-A3] test_length[C2-B1-A2] -> {'A1': 2, 'A2': 2, 'A3': 1} test_length[C2-B1-A1] -> {'A1': 3, 'A2': 2, 'A3': 1} test_length[C1-B2-A1]  PPS: reducing the nesting might give you the expected results, but not necessarily def test_length(second):  output was Counter({'A1': 2, 'A2': 1, 'A3': 1})
Question: The mypy error is Need type annotation for "args" [var-annotated] Need type annotation for "kwargs" [var-annotated]  and here is the piece of code expected_args: Optional[Sequence[Tuple[Any, ...]]] expected_kwargs: Optional[Sequence[Dict[str, Any]]] ... expected_args_iter = iter(expected_args or ()) expected_kwargs_iter = iter(expected_kwargs or ()) ... for args, kwargs in itertools.zip_longest( expected_args_iter, expected_kwargs_iter, fillvalue={})  Where can I annotate "args" and "kwargs"?

Answer: This appears to be a bug in mypy v1.14.0. For some reason, any iteration over itertools.zip_longest() with an empty sequence* in fillvalue will cause a similar issue. This is the simplest example I could construct: import itertools seq_a: list seq_b: list for a, b in itertools.zip_longest(seq_a, seq_b, fillvalue=[]): ...  This is specific to both mypy and empty sequences. Non-empty sequences, non-sequences, and using Pyright are all fine. In your particular case, you can work around this issue by leaving fillvalue as its default (None) and hard-coding an exception into your comprehension. pretty_unused_args = [ ', '.join(itertools.chain( (repr(a) for a in args) if args is not None else [], ('%s=%r' % kwarg for kwarg in kwargs.items()) if kwargs is not None else [])) for args, kwargs in itertools.zip_longest( expected_args_iter, expected_kwargs_iter) ]   *Besides (), but including tuple()
Question: I have the following Custom dataset class for an image segmentation task. class LoadDataset(Dataset): def __init__(self, img_dir, mask_dir, apply_transforms = None): self.img_dir = img_dir self.mask_dir = mask_dir self.transforms = apply_transforms self.img_paths, self.mask_paths = self.__get_all_paths() self.__pil_to_tensor = transforms.PILToTensor() self.__float_tensor = transforms.ToDtype(torch.float32, scale = True) self.__grayscale = transforms.Grayscale() def __get_all_paths(self): img_paths = [os.path.join(self.img_dir, img_name.name) for img_name in os.scandir(self.img_dir) if os.path.isfile(img_name)] mask_paths = [os.path.join(self.mask_dir, mask_name.name) for mask_name in os.scandir(self.mask_dir) if os.path.isfile(mask_name)] img_paths = sorted(img_paths) mask_paths = sorted(mask_paths) return img_paths, mask_paths def __len__(self): return len(self.img_paths) def __getitem__(self, index): img_path, mask_path = self.img_paths[index], self.mask_paths[index] img_PIL = Image.open(img_path) mask_PIL = Image.open(mask_path) img_tensor = self.__pil_to_tensor(img_PIL) img_tensor = self.__float_tensor(img_tensor) mask_tensor = self.__pil_to_tensor(mask_PIL) mask_tensor = self.__float_tensor(mask_tensor) mask_tensor = self.__grayscale(mask_tensor) if self.transforms: img_tensor, mask_tensor = self.transforms(img_tensor, mask_tensor) return img_tensor, mask_tensor  When I am applying the following transformation: transforms.RandomHorizontalFlip()  either the image or the mask is being flipped. But if I change the order of the transformations in __getitem__ to the following, then it works fine. def __getitem__(self, index): img_path, mask_path = self.img_paths[index], self.mask_paths[index] img_PIL = Image.open(img_path) mask_PIL = Image.open(mask_path) if self.transforms: img_PIL, mask_PIL = self.transforms(img_PIL, mask_PIL) img_tensor = self.__pil_to_tensor(img_PIL) mask_tensor = self.__pil_to_tensor(mask_PIL) img_tensor = self.__float_tensor(img_tensor) mask_tensor = self.__float_tensor(mask_tensor) mask_tensor = self.__grayscale(mask_tensor) return img_tensor, mask_tensor  Does the order transformation matter? I'm using torchvision.transforms.v2 for all the transformations.

Answer: Yes, the order of transformations matters. In this case, the transform to tensors makes the difference. When v2.RandomHorizontalFlip is given two tensors, the flip will be applied independently. However, when two PIL images are given, the same transform will be applied to both images, thus keeping the image and mask aligned. For a more consistent handling, you can try using TVTensors for the data augmentation. Using these, you can specify the type of each data input before transforming them. For example: from torchvision import tv_tensors img_tensor = tv_tensors.Image(img_tensor) mask_tensor= tv_tensors.Mask(mask_tensor)
Question: My goal is to obtain a PNG file with a transparent background from a PDF file. The convert tool can do the job: $ convert test.pdf test.png $ file test.png test.png: PNG image data, 595 x 842, 8-bit gray+alpha, non-interlaced  But I would like to do it programmatically in python without relying on convert or any other tool. I came up with the pdf2image package but I could not figure out how to get transparency. Here is my attempt: import pdf2image with open("test.pdf", "rb") as fd: pdf = pdf2image.convert_from_bytes(fd.read(), transparent=True) pdf[0].save("test.png")  Unfortunately I lose transparency: $ python test.py $ file test.png test.png: PNG image data, 1654 x 2339, 8-bit/color RGB, non-interlaced  Is there any way to do this without relying on an external tool using pdf2image or any other package ?

Answer: With PyMuPDF, you can do this: import pymupdf doc=pymupdf.open("test.pdf") for page in doc: pix = page.get_pixmap(alpha=True, dpi=150) pix.save(f"{doc.name}-{page.number}.png")  Results in transparent PNG images named "test.pdf-0.png", etc. The images have a resolution of 150 DPI in above case. Note: I am a maintainer and the original creator of PyMuPDF.
Question: Tensorflow version is 2.17.1 Tensoflow probability version is 0.24.0 Example from the documentation https://www.tensorflow.org/probability/api_docs/python/tfp/layers/MixtureNormal?hl=en is the following: import numpy as np import tensorflow as tf import tensorflow_probability as tfp tfd = tfp.distributions tfpl = tfp.layers tfk = tf.keras tfkl = tf.keras.layers # Load data -- graph of a [cardioid](https://en.wikipedia.org/wiki/Cardioid). n = 2000 t = tfd.Uniform(low=-np.pi, high=np.pi).sample([n, 1]) r = 2 * (1 - tf.cos(t)) x = r * tf.sin(t) + tfd.Normal(loc=0., scale=0.1).sample([n, 1]) y = r * tf.cos(t) + tfd.Normal(loc=0., scale=0.1).sample([n, 1]) # Model the distribution of y given x with a Mixture Density Network. event_shape = [1] num_components = 5 params_size = tfpl.MixtureNormal.params_size(num_components, event_shape) model = tfk.Sequential([ tfkl.Dense(12, activation='relu'), tfkl.Dense(params_size, activation=None), tfpl.MixtureNormal(num_components, event_shape) ]) # Fit. batch_size = 100 model.compile(optimizer=tf.train.AdamOptimizer(learning_rate=0.02), loss=lambda y, model: -model.log_prob(y)) model.fit(x, y, batch_size=batch_size, epochs=20, steps_per_epoch=n // batch_size)  This ends up with the error ValueError: Only instances of `keras.Layer` can be added to a Sequential model. Received: <tensorflow_probability.python.layers.distribution_layer.MixtureNormal object at 0x7c9076269a50> (of type <class 'tensorflow_probability.python.layers.distribution_layer.MixtureNormal'>) 

Answer: Taking a look at the release notes of TensorFlow Probability: "NOTE: In TensorFlow 2.16+, tf.keras (and tf.initializers, tf.losses, and tf.optimizers) refers to Keras 3. TensorFlow Probability is not compatible with Keras 3 -- instead TFP is continuing to use Keras 2, which is now packaged as tf-keras and tf-keras-nightly and is imported as tf_keras. When using TensorFlow Probability with TensorFlow, you must explicitly install Keras 2 along with TensorFlow (or install tensorflow-probability[tf] or tfp-nightly[tf] to automatically install these dependencies.)" Try and follow the instructions above.
Question: I'm trying to calculate the weights for a finite-difference approximation of the first derivative f′(x)f'(x)f′(x) using the Taylor series expansion. I'm solving for weights a,b,c,d,ea, b, c, d, ea,b,c,d,e such that: af(x+2Δx)+bf(x+Δx)+cf(x)+df(x−Δx)+ef(x−2Δx)a f(x+2\Delta x) + b f(x+\Delta x) + c f(x) + d f(x-\Delta x) + e f(x-2\Delta x)af(x+2Δx)+bf(x+Δx)+cf(x)+df(x−Δx)+ef(x−2Δx) approximates f′(x)f'(x)f′(x). Here's what I did:  I used the Taylor series expansion for f(x±kΔx)f(x \pm k\Delta x)f(x±kΔx), where k=1,2k = 1, 2k=1,2.  I built a system of linear equations to enforce the following conditions:  Coefficients of f(x): a+b+c+d+e=0  Coefficients of f′(x): 2a+b−d−2e=1  Coefficients of f′′(x): 4a+b+d+4e=0  Coefficients of f(3)(x): 8a+b−d−8e=0  Coefficients of f(4)(x): 16a+b+d+16e=0    I implemented the matrix equation A⋅z=bA \cdot z = bA⋅z=b in Python: import numpy as np A = np.array([ [1, 1, 1, 1, 1], # Coefficients of f(x) [2, 1, 0, -1, -2], # Coefficients of f'(x) [4, 1, 0, 1, 4], # Coefficients of f''(x) [8, 1, 0, -1, 8], # Coefficients of f'''(x) [16, 1, 0, 1, 16] # Coefficients of f''''(x) ]) b = np.array([0, 1, 0, 0, 0]) # Targeting the first derivative z = np.linalg.solve(A, b) print(z)   The Issue: The output I'm getting is: [0.25,0,-0,0,-0.25]  However, the expected weights for the first derivative should be something like:   [-1/12,2/3,0,-2/3,1/12]   What I Tried:  Double-checked the coefficients in matrix A to ensure they match the Taylor series expansion.  Verified that the right-hand side vector b=[0,1,0,0,0] is correct for approximating f′(x).   Despite this, the weights are incorrect. Am I missing something in the matrix setup or the Python implementation?  Expected Behavior: I want the solution to match the theoretical weights for a central finite difference approximation of the first derivative f′(x) using five points.  

Answer: You have a typo in the 4th line of the matrix A, the last element should be -8 instead of 8: A = np.array([ [1, 1, 1, 1, 1], # Coefficients of f(x) [2, 1, 0, -1, -2], # Coefficients of f'(x) [4, 1, 0, 1, 4], # Coefficients of f''(x) [8, 1, 0, -1, -8], # Coefficients of f'''(x) [16, 1, 0, 1, 16] # Coefficients of f''''(x) ])
Question: #AttributeError: 'FigureCanvasInterAgg' object has no attribute 'tostring_rgb'. Did you mean: 'tostring_argb'? #import matplotlib.pyplot as plt #======================== # This can be work # import matplotlib # matplotlib.use('TkAgg') # import matplotlib.pyplot as plt #========================= with open('notebook.txt', encoding='utf-8') as file: # contents = file.read() # print(contents) # for line in file: # print('line:', line) contents = file.readlines() print(contents) newList = [] for content in contents: newContent = content.replace('\n', '') money = newContent.split(':')[-1] newList.append(int(money)) # 6月: 9000 # contents = content.replace('\n', '') print(newList) x = [1, 2, 3, 4, 5, 6] y = newList plt.plot(x, y, 'r') plt.xlabel('month') plt.ylabel('money') plt.legend() plt.show()  1月: 7000 2月: 10000 3月: 15000 4月: 12000 5月: 13000 6月: 9000  I am learning to draw graphs with matplotlib, but import matplolib.plylot as plt does not recognize the data. I have pip installed matplotlib, but I suspect it is not installed in the right path. Is there any way to solve this problem?

Answer: The following code runs successfully on my computer, and my maplotlib verson is 3.7.1 if you don't know your matplotlib verson,you can press "ctrl" and 'r',then input "cmd" to open the terminal,and input "pip list",then you can find your matlotlib version import matplotlib.pyplot as plt from matplotlib import rcParams # 设置支持中文字体 rcParams['font.sans-serif'] = ['SimHei'] # 使用黑体 rcParams['axes.unicode_minus'] = False # 正常显示负号 with open('notebook.txt', encoding='utf-8') as file: contents = file.readlines() # 按行读取文件内容 newList = [] for content in contents: newContent = content.replace('\n', '') # 去掉换行符 money = newContent.split(':')[-1].strip() # 提取“:”后面的金额部分并去掉空格 newList.append(int(money)) x = [1, 2, 3, 4, 5, 6] plt.plot(x, newList, 'r', label='收入') # 绘制红色折线图，并设置图例标签 plt.xlabel('月份') # 设置 x 轴标签 plt.ylabel('金额') # 设置 y 轴标签 plt.legend() # 显示图例 plt.show() # 展示图形
Question: Suppose we have the list: mylist = [ [ "Hello", [ "Hi" ] ] ]  How do I check that list containing "Hello" and "Hi" exists in mylist, in specifically this structure without flattening it? All the solutions are flattening the list, but I need to check for specific structure, like this Array |_ —-|_ “Hello” ———|_ “Hi” ——. . . 

Answer: You can just ask whether it's in there: ["Hello", ["Hi"]] in mylist  Attempt This Online!
Question: Is it possible with sympy Wild symbols and replace to match arbitrary function applications? What I would ideally like to do is the following: x = Symbol('x') expr1 = sin(x) expr2 = exp(x) F = Wild('F') #or maybe WildFunction('F')? result1 = expr1.replace(F(x), lambda F: F(tan(x))) #expected: sin(tan(x)) result2 = expr2.replace(F(x), lambda F: F(tan(x))) #expected: exp(tan(x))  Unfortunately this does not work: it throws a TypeError since Wild symbols are not callable. So is there a way to make this work? Note that I really don't want to match and replace specific functions, nor do I want to match and replace symbolic functions like Function('f'). I want to match and replace arbitrary (sympy?) functions like sin, exp, im, tan, re, conjguate and so on. What does work is F = WildFunction('F') result1 = expr1.replace(F, lambda F: F.func(*F.args))  But it feels a little unnatural and fragile.

Answer: Yes, but you're looking for Wild's properties argument and can use the type() of the match to nest a function call >>> F = Wild("F", properties=[lambda F: F.is_Function]) >>> expr1.replace(F, lambda F: type(F)(tan(x))) sin(tan(x))  Or to be more picky about which functions are replaced >>> F = Wild("F", properties=[lambda F: type(F) in [sin,tan,cos]]) >>> (sin(x) + cosh(x) + cos(x)).replace(F, lambda F: type(F)(csc(x))) sin(csc(x)) + cos(csc(x)) + cosh(x)  A somewhat shameless plug, but check out my other Answer about how .replace et al. behave (also links to more on Wild)!
Question: On a Windows machine, I'm developing a Python project that I manage using uv. I run the unit tests with uv run pytest, and uv automatically creates a virtual environment in .venv. So far, so good. But every now and then, I want to run the unit tests - or other commands - in Linux (from the same project source directory). In my case, this means WSL, but it could also be a VM using a shared folder or a network share. The problem is that the virtual environment is platform specific, so uv run pytest reports an error that the virtual environment is invalid. Is it possible to configure uv to use a different name for the project's virtual environment - e. g., .venv_linux? As a workaround, I could move the .venv folder out of the way and let uv on Linux create its own .venv. But I'd have to do that every time I switch between the two, and that would be cumbersome. I also couldn't do this while a command from the project is still running.

Answer: To support different virtual environments in a uv-managed project, the environment variable UV_PROJECT_ENVIRONMENT might be used.  UV_PROJECT_ENVIRONMENT: Specifies the path to the directory to use for a project virtual environment. See the project documentation for more details.  You could set it to .venv_windows or .venv_linux before running uv sync / uv run [...].
Question: I am trying to plot the response of a periodically-driven damped oscillator whose dynamics is governed by, x''+ 2Gx' + f0^2 x = F cos(ft) where the constants denote the following. G: Damping coefficient f0: Natural frequency f: Driving frequently F: Strength of the drive To do so, I solved the above differential equation for x(t). Next, I extracted the steady-state part from x(t), took its Fourier transform, and plotted its magnitude to visualize the response of the oscillator. Here is the code that attempts to achieve it. import numpy as np import matplotlib.pyplot as plt from scipy.fft import fft, fftfreq G=1.0 f0=2 f1=5 F=1 N=500000 T=50 dt=T/N t=np.linspace(0,T,N) u=np.zeros(N,dtype=float) # Position v=np.zeros(N,dtype=float) # Velocity u[0]=0 v[0]=0.5 for i in range(N-1): u[i+1] = u[i] + v[i]*dt v[i+1] = v[i] - 2*G*v[i]*dt - (f0*f0)*u[i]*dt + F*np.cos(f1*t[i])*dt slice_index=int(20/dt) U=u[slice_index:] X_f = fft(U) frequencies = fftfreq(len(U), dt) psd = np.abs(X_f) positive_freqs = frequencies[frequencies > 0] plt.plot(positive_freqs, psd[frequencies > 0], label="Simulated PSD") plt.plot(frequencies, psd)  Since the oscillator is forced and reaches a steady state, I expect the response to peak around the driving frequency. However, the above code gives a peak located nowhere near f. What am I doing wrong?

Answer: Your frequencies f0 and f1 are applied in the finite-difference model in rad/s. This may or may not have been your intention. However, your frequencies from the FFT are in cycles/s. Since you are using the symbol f, rather than omega, I would guess that you want them in cycles/s. In your finite-difference model then you would have to use 2.PI.f in both locations where you put an f before. Specifically in the line v[i+1] = v[i] - 2*G*v[i]*dt - (2 * np.pi * f0 ) ** 2 * u[i]*dt + F*np.cos( 2 * np.pi * f1*t[i] ) * dt  Then you get peak energy at a frequency of 5 Hz. (Trim the x-axis scale.) You are very heavily damped, BTW. Also, you aren't strictly plotting PSD.  import numpy as np import matplotlib.pyplot as plt from scipy.fft import fft, fftfreq G=1.0 f0=2 f1=5 F=1 N=500000 T=50 dt=T/N t=np.linspace(0,T,N) u=np.zeros(N,dtype=float) # Position v=np.zeros(N,dtype=float) # Velocity u[0]=0 v[0]=0.5 for i in range(N-1): u[i+1] = u[i] + v[i]*dt v[i+1] = v[i] - 2*G*v[i]*dt - (2 * np.pi * f0 ) ** 2 * u[i]*dt + F*np.cos(2 * np.pi * f1*t[i] ) * dt slice_index=int(20/dt) U=u[slice_index:] X_f = fft(U) frequencies = fftfreq(len(U), dt) psd = np.abs(X_f) positive_freqs = frequencies[frequencies > 0] plt.plot(positive_freqs, psd[frequencies > 0], label="Simulated PSD") plt.xlim(0,10) plt.show()