eshangj/stackoverflow_q_and_a_sample huggingface dataset:
Question: I am working with images that I need to process. However, only specific areas of these images are of interest so for each image I have a corresponding mask (that can be of any shape, not a bounding box or anything specific). I would like to do Histogram Equalization but only on the "masked surface" as I am not interested in the rest of the image. A similar question has been asked here, however the solutions proposed rely on bounding boxes or equalizing the whole image which is not my goal. Any solution using Histogram Equalization or CLAHE, RGB or grey scale images would be interesting

Answer: This is possible. Here is a simple approach: Flow: You can perform histogram equalization for a given region with the help of the mask.  Using the mask, store coordinates where pixels are in white. Store pixel intensities from these coordinates present in the grayscale image Perform histogram equalization on these stored pixels. You will now get a new set of values. Replace the old pixel values with the new ones based on the coordinate positions.  Code: The following is an illustration using a grayscale image. img = cv2.imread('flower.jpg') gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)   The following is the mask: mask = cv2.imread('mask.jpg', 0)   I want to perform histogram equalization only on the flower. Store the coordinates where pixels are white (255) in mask: coord = np.where(mask == 255)  Store all pixel intensities on these coordinates present in gray: pixels = gray[coord]  Perform histogram equalization on these pixel intensities: equalized_pixels = cv2.equalizeHist(pixels)  Create a copy of gray named gray2. Place the equalized intensities in the same coordinates: gray2 = gray.copy() for i, C in enumerate(zip(coord[0], coord[1])): gray2[C[0], C[1]] = equalized_pixels[i][0] cv2.imshow('Selective equalization', gray2)   Comparison:   Note: This process can be extended for Histogram Equalization or CLAHE, ON RGB or grayscale images.
Question: I'm following a Django tutorial and trying to update a urls.py file with the following code: from django.contrib import admin from django.urls import path from django.conf.urls import re_path, include urlpatterns=[ path('admin/', admin.site.urls), re_path(r'^',include('EmployeeApp.urls')) ]  When I run the server with python manage.py runserver I get the following error: ImportError: cannot import name 're_path' from 'django.conf.urls' (C:\Users\User\AppData\Local\Programs\Python\Python310\lib\site-packages\django\conf\urls\__init__.py)  I'm running version 4.0.4 of Django: py -m django --version # Prints: 4.0.4 

Answer: I needed to change from django.conf.urls import re_path, include  to: from django.conf.urls import include from django.urls import re_path  Now the error has stopped. (Documentation)
Question: I'm currently making a custom render engine for Blender 3.0+. I'm programming in C++(core engine) and Python(Blender API). In the past I have made two other render engines, where the data stored for meshes are polygons, edges and vertices, which is quite usual. Blender uses a different structure, where a polygon references multiple "MeshLoops". A "MeshLoop" references one edge and one vertex. What I don't quite understand is the reason behind this. Simple referencing a list of vertices would absolutely work (and that's how my render engine works for now). I'm certain there is a reason for this, but I'm unable to find any more information about this. The API documentation about it simply states that is keeps the index of one vertex and one edge. So my question is : what exactly is a MeshLoop, and what purpose does it serve ?  Usefull links :  Blender API : Mesh Blender API : MeshLoops Blender API : MeshPolygon 

Answer: What the MeshLoop is in Blender API You're undoubtedly right about official documentation, even in 2024 it sucks. But the answer to your question is obvious. MeshLoop objects are tiny "utilities" in Blender that help you not only describe a 3D geometry but also store a color data for each vertex. There are four main arrays to define mesh geometry in Blender (unlike Maya, for instance, where vertices, edges and polygons are used for a commonly accepted mesh description, as you previously said):  Mesh.vertices : (3 points in space) Mesh.edges : (reference 2 vertices) Mesh.loops : (reference a single vertex and edge) Mesh.polygons : (reference a range of loops)  As stated in the documentation: "Each polygon references a slice in the loop array, this way, polygons do not store vertices or corner data such as UVs directly, only a reference to loops that the polygon uses." Thus, using the simplest example of a plane with 4 different colors assigned, we'll see how the central vertex, thanks to MeshLoop, retrieves the information about each of the four colors (without MeshLoop you can store only one color per vertex). import bpy plane = bpy.context.object mesh = plane.data len(mesh.vertices) len(mesh.edges) len(mesh.loops) len(mesh.polygons) len(mesh.vertex_colors)
Question: I am trying to install a library from the source that makes use of Poetry, but I get this error error: Multiple top-level packages discovered in a flat-layout: ['tulips', 'fixtures']. To avoid accidental inclusion of unwanted files or directories, setuptools will not proceed with this build. If you are trying to create a single distribution with multiple packages on purpose, you should not rely on automatic discovery. Instead, consider the following options: 1. set up custom discovery (`find` directive with `include` or `exclude`) 2. use a `src-layout` 3. explicitly set `py_modules` or `packages` with a list of names To find more information, look for "package discovery" on setuptools docs  What do I need to do to fix it?

Answer: Based on this comment on a GitHub issue, adding the following lines to your pyproject.toml might solve your problem: [tool.setuptools] py-modules = []  (For my case, the other workaround provided in that comment, i.e. adding py_modules=[] as a keyword argument to the setup() function in setup.py worked) See Package Discovery and Namespace Packages for additional information.
Question: In the following MWE, I have two files/modules:  main.py which is and should be checked with mypy and importedmodule.py which should not be type checked because it is autogenerated. This file is autogenerated, I don't want to add type:ignore.  MyPy Command $ mypy main.py --exclude '.*importedmodule.*'  $ mypy --version mypy 0.931  main.py """ This should be type checked """ from importedmodule import say_hello greeting = say_hello("Joe") print(greeting)  importedmodule.py """ This module should not be checked in mypy, because it is excluded """ def say_hello(name: str) -> str: # This function is imported and called from my type checked code return f"Hello {name}!" def return_an_int() -> int: # ok, things are obviously wrong here but mypy should ignore them # also, I never expclitly imported this function return "this is a str, not an int" # <-- this is line 14 referenced in the mypy error message  But MyPy complains about the function that is not even imported in main.py:  importedmodule.py:14: error: Incompatible return value type (got "str", expected "int") Found 1 error in 1 file (checked 1 source file)  What is wrong about my exclude?

Answer: Here is SUTerliakov's comment on your question written as an answer. In the pyproject.toml file you can insert the following below your other mypy config [[tool.mypy.overrides]] module = "importedmodule" ignore_errors = true  With this config you will ignore all errors coming from the mentioned module. By using a wildcard, you can also ignore all modules in a directory: [[tool.mypy.overrides]] module = "importedpackage.*" ignore_errors = true  Related Documentation:  https://mypy.readthedocs.io/en/stable/config_file.html#using-a-pyproject-toml-file
Question: I came across a weird issue while using the new match/case syntax in Python3.10. The following example seems like it should work, but throws an error: values = [ 1, "hello", True ] for v in values: match type(v): case str: print("It is a string!") case int: print("It is an integer!") case bool: print("It is a boolean!") case _: print(f"It is a {type(v)}!")  $ python example.py File "/.../example.py", line 9 case str: ^^^ SyntaxError: name capture 'str' makes remaining patterns unreachable   It is mentioning that the first case (the value str) will always result in True.   Wondering if there is an alternative to this other than converting the type to a string.

Answer: Rather than match type(v), match v directly: values = [ 1, "hello", True, ] for v in values: match v: case str(): print("It is a string!") case bool(): print("It is a boolean!") case int(): print("It is an integer!") case _: print(f"It is a {type(v)}!")  Note that I've swapped the order of bool() and int() here, so that True being an instance of int doesn't cause issues. This is a class pattern match.
Question: I have the following model class Window(BaseModel): size: tuple[int, int]  and I would like to instantiate it like this: fields = {'size': '1920x1080'} window = Window(**fields)  Of course this fails since the value of 'size' is not of the correct type. However, I would like to add logic so that the value is split at x, i.e.: def transform(raw: str) -> tuple[int, int]: x, y = raw.split('x') return int(x), int(y)  Does Pydantic support this?

Answer: Pydantic 2.x (edit) Pydantic 2.0 introduced the field_validator decorator which lets you implement such a behaviour in a very simple way. Given the original parsing function: from pydantic import BaseModel, field_validator class Window(BaseModel): size: tuple[int, int] @field_validator("size", mode="before") @classmethod def transform(cls, raw: str) -> tuple[int, int]: x, y = raw.split("x") return int(x), int(y)  Note:  The validator method is a class method, as denoted by the cls first argument. Implementing it as an instance method (with self) will raise an error. The mode="before" in the decorator is critical here, as expected this is what makes the method run before checking "size" is a tuple.   Pydantic 1.x (original answer) You can implement such a behaviour with pydantic's validator. Given your predefined function: def transform(raw: str) -> tuple[int, int]: x, y = raw.split('x') return int(x), int(y)  You can implement it in your class like this: from pydantic import BaseModel, validator class Window(BaseModel): size: tuple[int, int] _extract_size = validator('size', pre=True, allow_reuse=True)(transform)  Note the pre=True argument passed to the validator. It means that it will be run before the default validator that checks if size is a tuple.  Now: fields = {'size': '1920x1080'} window = Window(**fields) print(window) # output: size=(1920, 1080)  Note that after that, you won't be able to instantiate your Window with a tuple for size. fields2 = {'size': (800, 600)} window2 = Window(**fields2) # AttributeError: 'tuple' object has no attribute 'split'  In order to overcome that, you could simply bypass the function if a tuple is passed by altering slightly your code: Pydantic 2.x class Window(BaseModel): size: tuple[int, int] @field_validator("size", mode="before") def transform(cls, raw: str | tuple[int, int]) -> tuple[int, int]: if isinstance(raw, tuple): return raw x, y = raw.split("x") return int(x), int(y)  Pydantic 1.x def transform(raw: str | tuple[int, int]) -> tuple[int, int]: if isinstance(raw, tuple): return raw x, y = raw.split('x') return int(x), int(y) class Window(BaseModel): size: tuple[int, int] _extract_size = validator('size', pre=True, allow_reuse=True)(transform)  Which should give: fields2 = {'size': (800, 600)} window2 = Window(**fields2) print(window2) # output: size:(800, 600)
Question: I installed poetry using the following command:- (Invoke-WebRequest -Uri https://raw.githubusercontent.com/python-poetry/poetry/master/get-poetry.py -UseBasicParsing).Content | python -  To know more about it refer this. Now I wanted to create a virtual environment, that I created it using the following command:- poetry config virtualenvs.in-project true To know more about it refer this. But after doing this, I can`t see any .venv(virtual environment) folder. To check if virtual environment is there, we use the following command:- poetry config virtualenvs.in-project and if the above command return true, implies you have it. I'm getting true, also the location mentioned on the docs I cant see it there. Could anyone tell me how to locate the virtual environment now?

Answer: There are 2 commands that can find where the virtual environment is located.  poetry show -v The first line of this command will tell you where the virtual environment is located. And the rest will tell you which packages are installed in it.  poetry env info -p The above command will give you just the location of the virtual environment.
Question: When i tried pip install confluent-kafka got the following error  #include <librdkafka/rdkafka.h> ^~~~~~~~~~~~~~~~~~~~~~ 1 error generated. error: command '/usr/bin/gcc' failed with exit code 1  I'm using python version 3.9 and macOs Monterey

Answer: Install the librdkafka library brew install librdkafka Set the environment variables export C_INCLUDE_PATH=/usr/local/Cellar/librdkafka/2.2.0/include export LIBRARY_PATH=/usr/local/Cellar/librdkafka/2.2.0/lib  Then you can install it through pip install
Question: I'm facing this error while installing the setup for Tensorflow Object Detection API. How to fix this error? Error: Could not install packages due to an OSError: [WinError 206] The filename or extension is too long: ``` 

Answer: Error: Could not install packages due to an OSError: [WinError 206] The filename or extension is too long: To fix this error on your Windows machine on regedit and navigate to Computer\HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Control\FileSystem and edit LongPathsEnabled and set value from 0 to 1
Question: currently I'm working with FastAPI and pydantic as serializer. Problem is, we're using snowflake id on the server side, which means we need to convert those ids to string before sending to client (javascript) because the id is larger than JS's MAX SAFE INTEGER. So I tried to create a new class which extends python's int type and customize how it will be serialized and deserialized. Here's my code: class SnowflakeId(int): @classmethod def __get_validators__(cls): yield cls.validate @classmethod def validate(cls, v: str): return int(v) @classmethod def __modify_schema__(cls, field_schema: dict) -> None: field_schema['type'] = 'string'  And here is the model: class BaseModel(pydantic.BaseModel): __abstract__ = True id: SnowflakeId class Config: orm_mode = True arbitrary_types_allowed = True json_encoders = { SnowflakeId: lambda v: str(v) } alias_generator = camelize allow_population_by_field_name = True  It works fine when deserializing from json string into int id, however, when it comes to the serialization, the output still is integer. I want it to serialize the id into string also, is it possible?

Answer: Yes it is! json_encoders is a good try, however under the hood pydantic calls json.dumps. So for serializable types (like your SnowflakeId) it won't care about additional json_encoders. What you can do is to override dumps method: def my_dumps(v, *, default): for key, value in v.items(): if isinstance(value, SnowflakeId): v[key] = str(value) else: v[key] = value return json.dumps(v) class BaseModel(pydantic.BaseModel): id: SnowflakeId class Config: json_dumps = my_dumps  And let validate return SnowflakeId: class SnowflakeId(int): ... @classmethod def validate(cls, v: str): return cls(v)  m = BaseModel(id="123") print(m.json()) # {"id": "123"}
Question: I have a simple dag that uses a branch operator to check if y is False. If it is, the dag is supposed to move on to the say_goodbye task group. If True, it skips and goes to finish_dag_step. Here's the dag: def which_step() -> str: y = False if not y: return 'say_goodbye' else: return 'finish_dag_step' with DAG( 'my_test_dag', start_date = datetime(2022, 5, 14), schedule_interval = '0 0 * * *', catchup = True) as dag: say_hello = BashOperator( task_id = 'say_hello', retries = 3, bash_command = 'echo "hello world"' ) run_which_step = BranchPythonOperator( task_id = 'run_which_step', python_callable = which_step, retries = 3, retry_exponential_backoff = True, retry_delay = timedelta(seconds = 5) ) with TaskGroup('say_goodbye') as say_goodbye: for i in range(0,2): step = BashOperator( task_id = 'step_' + str(i), retries = 3, bash_command = 'echo "goodbye world"' ) step finish_dag_step = BashOperator( task_id = 'finish_dag_step', retries = 3, bash_command = 'echo "dag is finished"' ) say_hello >> run_which_step run_which_step >> say_goodbye >> finish_dag_step run_which_step >> finish_dag_step finish_dag_step  I get the following errors when the dag hits run_which_step:   I don't understand what's causing this. What is going on?

Answer: You can't create task dependencies to a TaskGroup. Therefore, you have to refer to the tasks by task_id, which is the TaskGroup's name and the task's id joined by a dot (task_group.task_id). Your branching function should return something like def branch(): if condition: return [f'task_group.task_{i}' for i in range(0,2)] return 'default'  But instead of returning a list of task ids in such way, probably the easiest is to just put a DummyOperator upstream of the TaskGroup. It'd effectively act as an entrypoint to the whole group.
Question: I would like to visualize the amount of laps a certain go-kart has driven within a pie chart. To achive this i would like to count the amount of laptime groupedby kartnumber. I found there are two ways to create such a pie chart: 1# df.groupby('KartNumber')['Laptime'].count().plot.pie()  2# df.groupby(['KartNumber']).count().plot(kind='pie', y='Laptime')  print(df) print(df) HeatNumber NumberOfKarts KartNumber DriverName Laptime 0 334 11 5 Monique 53.862 1 334 11 5 Monique 59.070 2 334 11 5 Monique 47.832 3 334 11 5 Monique 47.213 4 334 11 5 Monique 51.975 ... ... ... ... ... ... 4053 437 2 20 luuk 39.678 4054 437 2 20 luuk 39.872 4055 437 2 20 luuk 39.454 4056 437 2 20 luuk 39.575 4057 437 2 20 luuk 39.648  Output not with plot: KartNumber 1 203 10 277 11 133 12 244 13 194 14 172 15 203 16 134 17 253 18 247 19 240 2 218 20 288 21 14 4 190 5 314 6 54 60 55 61 9 62 70 63 65 64 29 65 53 66 76 67 42 68 28 69 32 8 49 9 159 None 13  As you can see i have the kartnumbers and count of laptimes. But i would like to show the count of laptimes within the pie chart(or legend). I tried using autopct but couldnt get it working properly. Does anyone knows how to achive my desired situation? Edit: For more information on this dataset please see: How to get distinct rows from pandas dataframe?

Answer: Complete awswer:  autopct=lambda x: '{:.0f}'.format(x * (df['Laptime'].count()) / 100))
Question: I have the following dataframe: data = {'Value':[6.25, 4.55, 4.74, 1.36, 2.56, 1.4, 3.55, 3.21, 3.2, 3.65, 3.45, 3.86, 13.9, 10.3, 15], 'Name':['Peter', 'Anna', 'Luke', 'Peter', 'Anna', 'Luke', 'Peter', 'Anna', 'Luke', 'Peter', 'Anna', 'Luke', 'Peter', 'Anna', 'Luke'], 'Param': ['Param1', 'Param1', 'Param1', 'Param2', 'Param2', 'Param2', 'Param3', 'Param3', 'Param3', 'Param4', 'Param4', 'Param4', 'Param5', 'Param5', 'Param5'], 'error': [2.55, 1.24, 0, 0.04, 0.97, 0, 0.87, 0.7, 0, 0.73, 0.62, 0, 0, 0, 0]} df = pd.DataFrame(data)  I'd like to add errorbars (pre-defined in the error column) to the bar plot, but I can't seem to get the x-coordinates right? It shows errorbars for Param5 but there are no errors for Param5? Also for Luke, there are no errors, but in Param1 an errorbar is plotted. plt.figure() ax = sns.barplot(x = 'Param', y = 'Value', data = df, hue = 'Name', palette = sns.color_palette('CMRmap_r', n_colors = 3)) x_coords = [p.get_x() + 0.5*p.get_width() for p in ax.patches] y_coords = [p.get_height() for p in ax.patches] plt.errorbar(x=x_coords, y=y_coords, yerr=df["error"], fmt="none", c= "k")  

Answer: The bars in ax.patches come ordered by hue value. To get the bars and the dataframe in the same order, the dataframe could be sorted first by Name and then by Param: from matplotlib import pyplot as plt import seaborn as sns import pandas as pd data = {'Value': [6.25, 4.55, 4.74, 1.36, 2.56, 1.4, 3.55, 3.21, 3.2, 3.65, 3.45, 3.86, 13.9, 10.3, 15], 'Name': ['Peter', 'Anna', 'Luke', 'Peter', 'Anna', 'Luke', 'Peter', 'Anna', 'Luke', 'Peter', 'Anna', 'Luke', 'Peter', 'Anna', 'Luke'], 'Param': ['Param1', 'Param1', 'Param1', 'Param2', 'Param2', 'Param2', 'Param3', 'Param3', 'Param3', 'Param4', 'Param4', 'Param4', 'Param5', 'Param5', 'Param5'], 'error': [2.55, 1.24, 0, 0.04, 0.97, 0, 0.87, 0.7, 0, 0.73, 0.62, 0, 0, 0, 0]} df = pd.DataFrame(data) df = df.sort_values(['Name', 'Param']) plt.figure(figsize=(8, 5)) ax = sns.barplot(x='Param', y='Value', data=df, hue='Name', palette='CMRmap_r') x_coords = [p.get_x() + 0.5 * p.get_width() for p in ax.patches] y_coords = [p.get_height() for p in ax.patches] ax.errorbar(x=x_coords, y=y_coords, yerr=df["error"], fmt="none", c="k") plt.show()   PS: Note that by default, the columns are sorted alphabetically. If you want to maintain the original order, you can make the column categorical via pd.Categorical(df['Name'], df['Name'].unique()). df = pd.DataFrame(data) df['Name'] = pd.Categorical(df['Name'], df['Name'].unique()) df['Param'] = pd.Categorical(df['Param'], df['Param'].unique()) df = df.sort_values(['Name', 'Param'])
Question: I'm currently trying to move our internal projects away from setup.py to pyproject.toml (PEP-518). I'd like to not use build backend specific configuration if possible, even though I do specify the backend in the [build-system] section by require'ing it. The pyproject.toml files are more or less straight-forward translations of the setup.py files, with the metadata set according to PEP-621, including the dependencies. We are using setuptools_scm for the determination of the version, therefore the version field ends up in the dynamic section. We used to set the packages parameter to setup in our setup.py files, but I couldn't find any corresponding field in pyproject.toml, so I simply omitted it. When building the project using python3 -m build ., I end up with a package named UNKNOWN, even though I have the name field set in the [project] section. It seems that this breaks very early in the build: $ python -m build . * Creating virtualenv isolated environment... * Installing packages in isolated environment... (setuptools, setuptools_scm[toml]>=6.2, wheel) * Getting dependencies for sdist... running egg_info writing UNKNOWN.egg-info/PKG-INFO ....  I'm using python 3.8.11 and the following packages: build==0.8.0 distlib==0.3.4 filelock==3.4.1 packaging==21.3 pep517==0.12.0 pip==22.0.4 platformdirs==2.4.0 pyparsing==3.0.9 setuptools==62.1.0 six==1.16.0 tomli==1.2.3 virtualenv==20.14.1 wheel==0.37.1  My (abbreviated) pyproject.toml looks like this: [project] name = "coolproject" dependencies = [ 'pyyaml==5.3', 'anytree==2.8.0', 'pytest' ] dynamic = [ "version" ] [build-system] requires = ["setuptools", "wheel", "setuptools_scm[toml]>=6.2"] [tool.setuptools_scm]  Any ideas?

Answer: Turning @AKX's comments into an answer so that other people can find it more easily. The problem may be an outdated pip/setuptools on the system. Apparently, version 19.3.1 which I have on my system cannot install a version of setuptools that can handle PEP621 metadata correctly. You cannot require a new pip from within pyproject.toml using the build-system.requires directive. In case you cannot update the system pip, you can always install on a per-user basis: pip install --user pip  and you're good to go.