eshangj/stackoverflow_q_and_a_sample huggingface dataset:
Question: I set up a new Python Poetry project with poetry init. I'm not creating a package, so I added this to my pyproject.toml: [tool.poetry] package-mode = false  The Operating Modes section of the Poetry documentation says that when operating in non-package mode, the name and version fields in pyproject.toml are optional, so I removed them. Now when I run a Poetry command, such as poetry env list, I get this error: The Poetry configuration is invalid: - project must contain ['name'] properties  Why is this happening?

Answer: This is a known issue: Poetry Issue 10032. More context is available in Issues 10031 and 10033. It does not matter if poetry.tools.package-mode = false, the project.name field is still required, because per the official standardized specification of the [project] table:  A [project] table must include a name key.  This behavior was initially unclear in Poetry's documentation, but updates clarify it, as discussed in Poetry Issue 10033. The solution is to use the [tool.poetry] table instead of the [project] table. The [project] table is meant to be used only for projects intended to be packaged, built, and distributed as sdist and/or wheel, for example on a Python package index such as PyPI. So using the [project] table for a "non-package" project is nonsensical and you should use the [tool.poetry] table instead.
Question: I previously asked about modeling a 2-finger gripper as a 1-DOF system in Drake: How to make a gripper with two fingers (2DOF) have 1 DOF making one finger symmetric to the other. My gripper had two fingers (each with a prismatic joint) and I wanted to represent it as a 1-DOF system in Drake. The answer provided indicated to use the <mimic> tag in an SDF file to achieve this. However, this approach does not reduce the num_positions() in the plant, meaning both finger joints are still considered in the configuration space. Now, I am working with C-IRIS to generate convex polytopes and need to ensure that the gripper fingers are treated as having only 1 DOF in configuration space while still considering collisions correctly. The right finger is not an independent variable and should move symmetrically to the left finger. C-IRIS should account for collisions involving the right finger, even though it is not explicitly controlled. How can I make C-IRIS ignore the redundant DOF when computing convex polytopes? At the same time: How can I ensure that collisions involving the right finger are still considered, even though it follows the left finger's motion? Any guidance on handling this in Drake would be greatly appreciated! I am using the python bindings with Pydrake version 1.35.0

Answer: It is doable inside C-IRIS (well, not out-of-the-box). What C-IRIS does is that it certifies the following problem ∀s ∈ {s | C*s <= d}, ∃ a(s), b(s), such that the separating plane a(s) * x + b(s) = 0 separates the geometries A and B in the Cartesian space.  Normally we search for the parameter C and d in the polytope {s | C*s <= d}. In your case, you want to say that your s variable also live in a subspace (specifically, to enforce symmetry of the joints, you probably want s_left + s_right = 0, which are two additional rows in the constraint C * s <= d). And then you can ask whether this polytope {s | C*s <= d} (which includes the constraint s_left + s_right = 0 ) is collision free (for example, using FindSeparationCertificateGivenPolytope ). So given a polytope C * s <= d (which lives in the subspace s_left+s_right=0 ), you can query the no-collision certificate. Searching for C and d is harder, as now you need to impose the additional constraint on C and d, such that it includes s_left + s_right <= 0 and -s_left - s_right <= 0. You could call InitializePolytopeSearchProgram and then impose additional constraints on C and d (for example, some entries in C needs to be 1 or -1, some corresponding entries in d need to be 0). And then solve the returned optimization problem with the right objective function. You can refer to FindPolytopeGivenLagrangian function in cspace_free_polytope.cc for an implementation.
Question: I've created a Python 3.6 environment in my WSL Ubuntu 22.04 operating system using virtualenv -p python3.6 <my_env>. I need to download some packages, particularly matplotlib, but I'm having some problems. When I run pip install matplotlib from terminal, it returns me ERROR: Command errored out with exit status 1, as it fails to build wheel for matplotlib, kiwisolver and pillow. I can't upgrade the Python version. I don't know what to do, I've tried everything I found online, but it's not working. Output Building wheel for matplotlib (setup.py) ... error ERROR: Command errored out with exit status 1: command: /home/andrea/Andrea/env/pyaneti_env/bin/python -u -c 'import io, os, sys, setuptools, tokenize; sys.argv[0] = '"'"'/tmp/pip-install-3byxbnl0/matplotlib_5a7e3488d8ed4fa586ccee5566cd5543/setup.py'"'"'; __file__='"'"'/tmp/pip-install-3byxbnl0/matplotlib_5a7e3488d8ed4fa586ccee5566cd5543/setup.py'"'"';f = getattr(tokenize, '"'"'open'"'"', open)(__file__) if os.path.exists(__file__) else io.StringIO('"'"'from setuptools import setup; setup()'"'"');code = f.read().replace('"'"'\r\n'"'"', '"'"'\n'"'"');f.close();exec(compile(code, __file__, '"'"'exec'"'"'))' bdist_wheel -d /tmp/pip-wheel-e23pjohb cwd: /tmp/pip-install-3byxbnl0/matplotlib_5a7e3488d8ed4fa586ccee5566cd5543/ Complete output (1714 lines): [...] AttributeError: install_layout ERROR: Failed building wheel for matplotlib  Analogue results for kiwisolver and pillow

Answer: Here I am answering my own question. After trying everything you and the internet suggested, I gave up on using virtualenv. As Matt suggested in the comments, I downloaded miniconda and created an environment there. I was able to download all the packages and the code is running nicely. Thanks for your help.
Question: I want to create a method on a base Pydantic model to instantiate child models with dummy data. from __future__ import annotations from pydantic import BaseModel class BaseModelWrapper(BaseModel): @classmethod def make_dummy(cls) -> BaseModelWrapper: for name, field in cls.model_fields.items(): if not field.is_required(): continue # How can I create values based on the field type? print(field.annotation) return cls() class XXX(BaseModelWrapper): a: int | None b: str c: int d: int | None = None e: list[str] # These should be equivalent XXX.make_dummy() XXX(a=None, b="", c=0, e=[])  The part I'm struggling with is how to programmatically map type annotations to values. Let's say field.annotation is int | None. I could just create a dictionary to map that to None, but there are tons of possible combinations of types, so this doesn't scale. There must be a cleaner way to create a value for each field.

Answer: I ultimately used @kamilcuk's answer to create the following method from __future__ import annotations from pydantic import BaseModel from types import UnionType from typing import Any, Union, get_args, get_origin def is_union_type(t: type | UnionType) -> bool: return get_origin(t) in [Union, UnionType] class BaseModelWrapper(BaseModel): @classmethod def make_dummy(cls) -> Self: kwargs = {} for name, field in cls.model_fields.items(): if not field.is_required(): continue if field.annotation is None: kwargs[name] = None continue t = field.annotation if is_union_type(t): types: tuple[type[Any]] = get_args(t) if type(None) in types: kwargs[name] = None continue t = types[0] try: # Will error if given `list[str]` / `dict[str]` if issubclass(t, BaseValidator): kwargs[name] = t.make_dummy() else: kwargs[name] = t() except TypeError: kwargs[name] = t() return cls(**kwargs)
Question: Can someone help me understand why the Signature.bind(...).arguments are different when I call directly for a class method vs. when used in a decorator? I have the decorator below. def decorator(func): @functools.wraps(func) def inner(*args, **kwargs): print(inspect.signature(func).bind(*args, **kwargs).arguments) return func(*args, **kwargs) return inner  When this decorator runs on a class method, I get the cls parameter in the signature like below. class MyClass: @classmethod @decorator def mymethod(cls, a, b): return a + b MyClass.mymethod(1, 2) # returns: # {'cls': <class '__main__.MyClass'>, 'a': 1, 'b': 2} # 3  If I call inspect.signature on the class method directly, I get a different result or an error. This first block runs successfully but does not return the class parameter. print(inspect.signature(MyClass.mymethod).bind(1, 2).arguments) # returns {'a': 1, 'b': 2}  This errors out. print(inspect.signature(MyClass.mymethod).bind(MyClass, 1, 2).arguments) # TypeError: too many positional arguments 

Answer: The classmethod decorator changes the signature of a function. When classmethod is applied to a function that is stored on a class, it automatically fills the first parameter of the function as the class. This is much the same as how self parameter is filled as the object a method is called on. If you print out the two signatures you can see this ie. >>> inspect.signature(MyClass.mymethod) # bound method, cls need not be supplied manually <Signature (a, b)> >>> inspect.signature(MyClass.mymethod.__func__) # gets unbound, underlying function <Signature (cls, a, b)>  That is, your decorator is working with a pure function. This is because your decorator is applied before the classmethod decorator. Decorators are applied bottom to top (in the order they appear closest to the function def). ie. @applied_third @applied_second @applied_first def foo(): pass  Your decorator must be applied first. This is due to how classmethods work, and something called non-data descriptors. As such, you MUST supply the cls argument as it was supplied to it. When you do MyClass.mymethod, you get a bound method, whose first parameter has been bound to MyClass. As such you MUST NOT supply the cls parameter manually (as it is has already been done automatically for you). Try the following instead: >>> inspect.signature(MyClass.mymethod).bind(1, 2) {'a': 1, 'b': 2}
Question: I'm wondering what the best approach for finding the closest match to a given value from a collection of items is. The most important part is the lookup time relative to the input size, the data can be shuffled and moved around as much as needed, as long as the lookup is therefore faster. Here the initial script: MAX = 1_000_000 MIN = 0 target: float = 333_333.0 collection: set[float] = {random.uniform(MIN, MAX) for _ in range(100_000)} # --- Code here to find closest as fast as possible, now and for future lookups --- assert closest in collection and all(abs(target - v) >= delta for v in collection)  Approach 1 Iterate through all values and update closest accordingly. Very simple but very slow for big collections. closest: float = next(iter(collection)) # get any element delta: float = abs(closest - target) for v in collection: tmp_delta = abs(v - target) if tmp_delta < delta: closest = v delta = tmp_delta  Approach 2 Sort data and then find closest via binary search. Sort time is O(n log n), but future lookups will only take O(log n) time to find! sorted_collection: list[float] = sorted(collection) idx = bisect.bisect_right(sorted_collection, target) if idx == 0: return sorted_collection[0] if idx == len(sorted_collection): return sorted_collection[-1] before, after = sorted_collection[idx - 1], sorted_collection[idx] return before if target - before <= after - target else after  Approach 3 Using dict with some custom hashing? I thought about implemenenting a __hash__ method for a custom class that could then be used to find values with (armored) O(1) lookup time, but couldn't get anything quite working without making some previous assumptions about the data involved and wonder if it is even possible. And that is where I got to in a nutshell. I am wondering if there is a faster way than the simple sorting + binary search approach and if my idea with dict + custom hash function is somehow possible.

Answer: With floats, the binary search scales like the best known. However it requires looking at locations in memory at some distance from each other. A B-tree also scales like O(log(n)), but by locating sets of decisions close to each other, it gets an order of magnitude constant improvement. The hash idea doesn't work because a good hash function will scatter nearby values in a pseudorandom way. And therefore you have no better way to find those nearby values than to search the whole hash.
Question: Can I customize the compile options for GCC in Nuitka (e.g. -O2, -static)? Update: I read the user manual but found nothing. I also checked the command line options of nuitka and found --generate-c-only and --show-scons. Maybe these can help me build the executables, but I don't know how to complete the rest of the process.

Answer: From the user manual:  Providing extra Options to Nuitka C compilation Nuitka will apply values from the environment variables CCFLAGS, LDFLAGS during the compilation on top of what it determines to be necessary. Beware, of course, that this is only useful if you know what you are doing, so should this pose issues, raise them only with perfect information.  Maybe this could be helpful!
Question: I'm trying to calculate a 2d variable z = x + y where x and y are 1d arrays of unequal dimensions (say, x- and y-coordinate points on a spatial grid). I'd like to display the result row-by-row in which the values of x and y are in the first two columns and the corresponding value of z calculated from these x and y values are in the third, something like the following for x = [1, 2] and y = [3, 4, 5]: x y z 1 3 4 1 4 5 1 5 6 2 3 5 2 4 6 2 5 7  The code below works (using lists here, but will probably need numpy arrays later): import pandas as pd x = [1, 2] y = [3, 4, 5] col1 = [] col2 = [] z = [] for i in range(len(x)): for j in range(len(y)): col1.append(x[i]) col2.append(y[j]) z.append(x[i]+y[j]) df = pd.DataFrame(zip(col1, col2, z), columns=["x", "y", "z"]) print(df)  Just wondering, is there a better way of doing this without using the loop by some combination of meshgrid, indices, flatten, v/hstack, and reshape? The size of x and y will typically be around 100.

Answer: Here is one way: import numpy as np import pandas as pd x = np.asarray([1, 2])[:, np.newaxis] y = np.asarray([3, 4, 5]) x, y = np.broadcast_arrays(x, y) z = x + y df = pd.DataFrame(zip(x.ravel(), y.ravel(), z.ravel()), columns=["x", "y", "z"]) print(df) # x y z # 0 1 3 4 # 1 1 4 5 # 2 1 5 6 # 3 2 3 5 # 4 2 4 6 # 5 2 5 7  But yes, you can also use meshgrid instead of orthogonal arrays + explicit broadcasting. You can also use NumPy instead of Pandas. x = np.asarray([1, 2]) y = np.asarray([3, 4, 5]) x, y = np.meshgrid(x, y, indexing='ij') z = x + y print(np.stack((x.ravel(), y.ravel(), z.ravel())).T) # array([[1, 3, 4], # [1, 4, 5], # [1, 5, 6], # [2, 3, 5], # [2, 4, 6], # [2, 5, 7]])
Question: I have a package with a pyproject.toml that has a core package & an optional GUI package with its own extra dependencies. This is the project structure: . └── my_project/ ├── src/ │ └── my_package/ │ ├── core/ │ │ └── __init__.py │ ├── gui/ │ │ └── __init__.py │ └── __init__.py ├── poetry.lock └── pyproject.toml  PEP 771 specifies how extras can be installed using square brackets. I have found plenty of resources on how to install extras, but frustratingly none on how to define them in your own project. How do I set up my project so that a user installing with poetry add my_package installs just the core package, but poetry add my_package[gui] installs both core and gui?

Answer: Extras don't work like that, they only apply to your packages dependencies. If you put the code in same package it will get bundled in when the package is built. Extras only apply to the dependencies of your package. In your case regardless of whether the user specifies [gui]or not they will get the gui code bundled into the package. what you can do is use extras to prevent all the gui dependencies from being installed when the user isn't using the gui. To make the error pretty clear for the user i've put something like this in optional modules __init__.py try: import some_gui_depedency ... except ModuleNotFoundError: raise Exception("Gui dependencies not installed, use `poetry add <package> -e gui` to use Gui  generally python code is tiny so it doesn't really matter that your shipping code that won't work, its usually the dependencies that have lots of binaries etc. thats really what makes things take a while to install. If your trying to hide the gui code from the user (maybe for commercial reasons?) you will need to make them seperate packages.
Question: Suppose I have a DataFrame with the following format of strings separated by commas:    Index ColumnName     0 apple,peach,orange,pear,   1 orange, pear,apple   2 pear   3 peach,apple   4 orange    The actual number of rows will be greater than 10,000. I want to expand the DataFrame and sort the DataFrame by row 0. My expected output is below, where None is of type NoneType:    Index 0 1 2 3     0 apple peach orange pear   1 apple None orange pear   2 None None None pear   3 apple peach None None   4 None None orange None    I have expanded the data using the following code: df = df['ColumnName'].str.split(',', expand=True) # Expand initial DataFrame  However, I am unable to sort or reorder the data as desired despite trying various combinations of df.sort_values().

Answer: Here is another way: s = df['columnName'].str.strip(',').str.split(', ?').explode() s.set_axis(pd.MultiIndex.from_frame(s.groupby(s).ngroup().reset_index())).unstack()  Output: 0 0 1 2 3 index 0 apple orange peach pear 1 apple orange NaN pear 2 NaN NaN NaN pear 3 apple NaN peach NaN 4 NaN orange NaN NaN
Question: You have been given the input data (a sequence of items), for example: [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16]  and your task is to randomly select M non-overlapping samples (subsequences) of the same size N. For example, if the task was to select 3 samples of size 3, one of the solutions would be: [3, 4, 5] [8, 9, 10] [11, 12, 13]  The samples are unordered so [8, 9, 10], [3, 4, 5], [11, 12, 13] is the same solution. All solutions are expected to have an equal probability of being selected.  My algorithm:  Select randomly first sample: [11, 12, 13] Remaining input data are [1, 2, 3, 4, 5, 6, 7, 8, 9, 10] and [14, 15, 16]. Select randomly second sample from the remaining input data: [3, 4, 5]. Remaining input data (big enough) are [6, 7, 8, 9, 10] and [14, 15, 16]. Select randomly third sample from the remaining input data: [8, 9, 10].  Sadly, this algorithm does not work when the samples are too big. If the task was to select 3 samples of size 5, there exists a solution, but if you use my algorithm and select randomly the first sample as [3, 4, 5, 6, 7], the algorithm will fail.  Of course there is also an obvious brute-force algorithm: find all possible solutions and select randomly one of them. But I was hoping for something more "clever" (time and space efficient).

Answer: import random seq = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16] M = 3 N = 3 dividers = sorted(random.sample(range(len(seq) - M*N + M), M)) starts = [d + i*(N-1) for i, d in enumerate(dividers)] samples = [seq[start : start+N] for start in starts] print(samples)  Example output (Attempt This Online!): [[3, 4, 5], [10, 11, 12], [13, 14, 15]]  Idea is that if you want 3 subsequences of size 3 from overall 16 items, then 7 elements are not in the subsequences. They're instead in the 4 gaps before/after the subsequences. Each gap can be empty. So find 4 nonnegative integers with sum 7 and use them as gap sizes. How to do that was inspired by this answer. Alternative explanation: If we had N=1, this would simply be sampling M single items. But in general, each of them is accompanied by the next N-1 items to make up a subsequence. So we basically do sample(range(len(seq)), M) for the N=1 case, but for the general case shrink the range by M*(N-1) to allow room for the M times N-1 accompaniers, which we then add. I tried choosing samples 120000 times and counting how often each starts tuple occurred. Each of the 120 possibilities occurred about 1000 times as expected: 120 ((0, 3, 6), 1030) ((0, 3, 7), 1007) ((0, 3, 8), 956) ... ((6, 9, 13), 1010) ((6, 10, 13), 1006) ((7, 10, 13), 986)  Code: import random from collections import Counter seq = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16] M = 3 N = 3 ctr = Counter() for _ in range(120_000): dividers = sorted(random.sample(range(len(seq) - M*N + M), M)) starts = [d + i*(N-1) for i, d in enumerate(dividers)] ctr[*starts] += 1 print(len(ctr)) for x in sorted(ctr.items()): print(x)  Attempt This Online!
Question: I have Gamma-Spectra and I am doing Gauss fit to the peaks using Python with scipy. Works well, but trying to get a number on fit quality (for the intent on some automation) returns very odd numbers. The scipy command is: response = scipy.optimize.curve_fit(gauss, e, c, param0, full_output=True, absolute_sigma=True, method=FitMethod, bounds=bounds)  I get the fit quality from: fitQuality = np.linalg.cond(response[1]) # PCOV is response[1]  FitQuality is a value from zero (excellent fit) up to infinity (beyond lousy). As an example, here is a fit (shaded green) to the K-40 Gamma line in a CsI detector.  Given the jittery nature of the data, I am pleased with the result. And so is scipy, giving it a FitQuality rating of 18. Next picture shows a fit (shaded red) to a Na-22 line in a High-Resolution Ge detector.  Again, I am very pleased with the result. But scipy is giving it a FitQuality rating of 56,982,136; this means very, very poor. This does not make sense. The fit is nearly perfect! Is my FitQuality formula inappropriate? Does it need additions? Am I completely off the mark? Please, help.

Answer: The formula you have is useful, but it is not a direct measure of the quality of the fit. It is related to "uncertainty" of the fit, and the condition number specifically is useful for diagnosing problems that may have occurred during fit. For instance, I've seen in other SO posts that `curve_fit` returns wild -looking parameter values. We find that the condition number is astronomical, so the covariance matrix is essentially singular. We find that this is because the callable is over-parameterized - that is, the parameters are not independent - and some can be il either removed or combined. Even in these cases, the fit may look perfect. See, for example, good r2 score but huge parameter uncertainty. The fit looks great, but they would find that the condition number of pcov is huge. This is described in the curve_fit documentation. Something that correlates better with visual "quality" is the sum of squared errors - that is, the objective function of least squares curve fitting. The covariance, on the other hand, is related to the Hessian - sort of like the curvature - of the objective function with respect to fitting parameters. So it's just not a measure of the "quality" of the fit, but how the quality changes with respect to the parameters.