eshangj/stackoverflow_q_and_a_sample huggingface dataset:
Question: I need to record an audio from the microphone and convert it into text. I have tried this conversion process using several audio clips that I downloaded from the web and it works fine. But when I try to convert the audio clip I recorded from the microphone it gives the following error. Traceback (most recent call last): File "C:\Users\HP\AppData\Local\Programs\Python\Python37\lib\site-packages\speech_recognition__init__.py", line 203, in enter self.audio_reader = wave.open(self.filename_or_fileobject, "rb") File "C:\Users\HP\AppData\Local\Programs\Python\Python37\lib\wave.py", line 510, in open return Wave_read(f) File "C:\Users\HP\AppData\Local\Programs\Python\Python37\lib\wave.py", line 164, in init self.initfp(f) File "C:\Users\HP\AppData\Local\Programs\Python\Python37\lib\wave.py", line 144, in initfp self._read_fmt_chunk(chunk) File "C:\Users\HP\AppData\Local\Programs\Python\Python37\lib\wave.py", line 269, in _read_fmt_chunk raise Error('unknown format: %r' % (wFormatTag,)) wave.Error: unknown format: 3 The code I am trying is as follows. import speech_recognition as sr import sounddevice as sd from scipy.io.wavfile import write # recording from the microphone fs = 44100 # Sample rate seconds = 3 # Duration of recording myrecording = sd.rec(int(seconds * fs), samplerate=fs, channels=2) sd.wait() # Wait until recording is finished write('output.wav', fs, myrecording) # Save as WAV file sound = "output.wav" recognizer = sr.Recognizer() with sr.AudioFile(sound) as source: recognizer.adjust_for_ambient_noise(source) print("Converting audio file to text...") audio = recognizer.listen(source) try: text = recognizer.recognize_google(audio) print("The converted text:" + text) except Exception as e: print(e)  I looked at the similar questions that were answered, and they say that we need to convert it into a different wav format. Can someone provide me a code or a library that I can use for this conversion? Thank you in advance.

Answer: You wrote the file in float format: soxi output.wav Input File : 'output.wav' Channels : 2 Sample Rate : 44100 Precision : 25-bit Duration : 00:00:03.00 = 132300 samples = 225 CDDA sectors File Size : 1.06M Bit Rate : 2.82M Sample Encoding: 32-bit Floating Point PCM  and wave module can't read it. To store int16 format do like this: import numpy as np myrecording = sd.rec(int(seconds * fs), samplerate=fs, channels=2) sd.wait() # Wait until recording is finished write('output.wav', fs, myrecording.astype(np.int16)) # Save as WAV file in 16-bit format
Question: I'm trying to create in Altair a Vega-Lite specification of a plot of a time series whose time range spans a few days. Since in my case, it will be clear which day is which, I want to reduce noise in my axis labels by letting labels be of the form '%H:%M', even if this causes labels to be non-distinct. Here's some example data; my actual data has a five minute resolution, but I imagine that won't matter too much here: import altair as alt import numpy as np import pandas as pd # Create data spanning 30 hours, or just over one full day df = pd.DataFrame({'time': pd.date_range('2018-01-01', periods=30, freq='H'), 'data': np.arange(30)**.5})  By using the otherwise trivial yearmonthdatehoursminutes transform, I get the following: alt.Chart(df).mark_line().encode(x='yearmonthdatehoursminutes(time):T', y='data:Q')   Now, my goal is to get rid of the dates in the labels on the horizontal axis, so they become something like ['00:00', '03:00', ..., '21:00', '00:00', '03:00'], or whatever spacing works best. The naive approach of just using hoursminutes as a transform won't work, as that bins the actual data: alt.Chart(df).mark_line().encode(x='hoursminutes(time):T', y='data:Q')   So, is there a declarative way of doing this? Ultimately, the visualization will be making use of selections to define the horizontal axis limits, so specifying the labels explicitly using Axis does not seem appealing.

Answer: To expand on @fuglede's answer, there are two distinct concepts at play with dates and times in Altair. Time formats let you specify how times are displayed on an axis; they look like this: chart.encode( x=alt.X('time:T', axis=alt.Axis(format='%H:%M')) )  Altair uses format codes from d3-time-format. Time units let you specify how data will be grouped, and they also adjust the default time format to match. They look something like this: chart.encode( x=alt.X('time:T', timeUnit='hoursminutes') )  or via the shorthand: chart.encode( x='hoursminutes(time):T' )  Available time units are listed here. If you want to adjust axis formats only, use time formats. If you want to group based on timespans (i.e. group data by year, by month, by hour, etc.) then use a time unit. Examples of this appear in the Altair documentation, e.g. the Seattle Weather Heatmap in Altair's example gallery.
Question: I created a Student model with field choices. However, when I save it, it doesn't validate whether the choice is in the choices I specified in the model field.  Why doesn't it prevent me from saving a new object with a choice I didn't specify in my model? Here is the model: class Student(models.Model): year_in_school = models.CharField( max_length=4, choices= [ ('FRES', 'Freshman'), ('SOPH', 'Sophomore'), ], )  And here is the code I wrote in the shell: >>> from app.models import Student >>> new_student = Student.objects.create(year_in_school='HACK') >>> new_student.year_in_school 'HA' 

Answer: You might want to read more about choices here. The relevant part copied below:  If choices are given, they’re enforced by model validation  Choices are not enforced at the database level. You need to perform model validation (by calling full_clean()) in order to check it. full_clean() will not be called automatically when you call your model’s save() method. You’ll need to call it manually.
Question: So, from what I see from most sources, they say if youre trying to make a python program call azure devops api calls, it uses a python import statement such as : from azure.devops.connection import Connection from msrest.authentication import BasicAuthentication ...  Is there any way to use requests or other built in import statements so I dont have to install these devops specific modules? I'm coding in putty so I dont have a way to install these modules. If anyone's got any solutions or ideas I'd be happy to hear it!

Answer: Surely, it is supported to use requests to call Azure DevOps REST API Firstly, you need to create a personal access token (PAT) Then you can use the PAT to create the basic auth header, and make the request: import requests import base64 pat = 'tcd******************************tnq' authorization = str(base64.b64encode(bytes(':'+pat, 'ascii')), 'ascii') headers = { 'Accept': 'application/json', 'Authorization': 'Basic '+authorization } response = requests.get( url="https://dev.azure.com/jack0503/_apis/projects?api-version=5.1", headers=headers) print(response.text)
Question: Having a weird issue here. Following the docs, I am attaching the PaymentMethod to an existing customer, but it's not working. Roughly, I:  create a customer create a payment intent with the customer create a card element with the payment intent customer enters card info confirm payment succeeded and sent intent back to backend if the intent has succeeded and the customer chose to save their card, create the payment method with the intent method and customer get error  The code:  python: stripe.Customer.create(email=user.email, name=user.full_name) python: stripe.PaymentIntent.create(amount=amount, currency="aud", customer=user.stripe_customer_id) js: Stripe('{{ stripe_publishable_key }}').elements().create("card"); user: enters card info js:  stripe.confirmCardPayment('{{ clientSecret }}', { payment_method: { card: card, billing_details: { // name: 'Jenny Rosen' }, } }).then(function (result) { if (result.error) { // Show error to your customer (e.g., insufficient funds) console.log(result.error.message); var displayError = document.getElementById('card-errors'); displayError.textContent = result.error.message; } else { // The payment has been processed! if (result.paymentIntent.status === 'succeeded') { // Show a success message to your customer // There's a risk of the customer closing the window before callback // execution. Set up a webhook or plugin to listen for the // payment_intent.succeeded event that handles any business critical // post-payment actions. $('#fake-submit').click(); } } }); python: stripe.PaymentMethod.attach(stripe.PaymentIntent.retrieve(intent_id).payment_method, customer=user.stripe_customer_id) error: Request req_request_id: This PaymentMethod was previously used without being attached to a Customer or was detached from a Customer, and may not be used again. 

Answer: It looks like there is an issue with the Stripe documentation.  On https://stripe.com/docs/payments/save-after-payment#web-collect-card-details they have:  setup_future_usage: 'off_session'  But on https://stripe.com/docs/payments/save-and-reuse#web-collect-card-details they are missing this critical line. But in your case, does the user select if they want to save their card on the frontend? Then you don't need to save the card on the backend and can save it in the confirmCardPayment call: https://stripe.com/docs/js/payment_intents/confirm_card_payment#stripe_confirm_card_payment-data-save_payment_method :  save_payment_method boolean If the PaymentIntent is associated with a customer and this parameter is set to true, the provided payment method will be attached to the customer. Default is false.
Question: I am searching for an alternative way for segmenting the grains in the following image of soil grains other than watershed segmentation in python as it may mislead the right detection for the grains furthermore , I am working on the edge detection image ( using HED algorithm ) as attached .. I hope to find a better way to segment the grains for further processing as I would like to get the area of each polygon in the image in my project .. Thanks in advance I am asking also about random walker segmentation or any other available method.   

Answer: You could try using Connected Components with Stats already implemented as cv2.connectedComponentsWithStats to perform component labeling. Using your binary image as input, here's the false-color image:  The centroid of each object can be found in centroid parameter and other information such as area can be found in the status variable returned from cv2.connectedComponentsWithStats. Here's the image labeled with the area of each polygon. You could filter using a minimum threshold area to only keep larger polygons  Code import cv2 import numpy as np # Load image, Gaussian blur, grayscale, Otsu's threshold image = cv2.imread('2.jpg') blur = cv2.GaussianBlur(image, (3,3), 0) gray = cv2.cvtColor(blur, cv2.COLOR_BGR2GRAY) thresh = cv2.threshold(gray, 0, 255, cv2.THRESH_BINARY_INV + cv2.THRESH_OTSU)[1] # Perform connected component labeling n_labels, labels, stats, centroids = cv2.connectedComponentsWithStats(thresh, connectivity=4) # Create false color image and color background black colors = np.random.randint(0, 255, size=(n_labels, 3), dtype=np.uint8) colors[0] = [0, 0, 0] # for cosmetic reason we want the background black false_colors = colors[labels] # Label area of each polygon false_colors_area = false_colors.copy() for i, centroid in enumerate(centroids[1:], start=1): area = stats[i, 4] cv2.putText(false_colors_area, str(area), (int(centroid[0]), int(centroid[1])), cv2.FONT_HERSHEY_SIMPLEX, 0.5, (255,255,255), 1) cv2.imshow('thresh', thresh) cv2.imshow('false_colors', false_colors) cv2.imshow('false_colors_area', false_colors_area) cv2.waitKey()
Question: I am trying to use protobuf enum as a type for values in a dictionary but it does not work for some reason. My enum definition in proto is: enum Device { UNSPECIFIED = 0; ON = 1; OFF = 2; }  After successful compilation and importing, the following code results in error. from devices_pb2 import Device def foo(device: Device) -> Dict[str, Device]: pass  Error message:  def foo(device: Device) -> Dict[str, Device]: File "/home/ivan/anaconda3/envs/py37/lib/python3.7/typing.py", line 254, in inner return func(*args, **kwds) File "/home/ivan/anaconda3/envs/py37/lib/python3.7/typing.py", line 629, in __getitem__ params = tuple(_type_check(p, msg) for p in params) File "/home/ivan/anaconda3/envs/py37/lib/python3.7/typing.py", line 629, in <genexpr> params = tuple(_type_check(p, msg) for p in params) File "/home/ivan/anaconda3/envs/py37/lib/python3.7/typing.py", line 142, in _type_check raise TypeError(f"{msg} Got {arg!r:.100}.") TypeError: Parameters to generic types must be types. Got <google.protobuf.internal.enum_type_wrapper.EnumTypeWrapper object at 0x7f4df6d81850>.  However, if I do not use dictionary then it works just fine: def foo(device: Device) -> Device: pass  I wonder if there is a solution to this problem?

Answer: Adding the following solved the problem: from __future__ import annotations  For more details, please check here.
Question: I have a library that I need to import on my code. However, whenever it is imported it outputs several lines of data to the console. How can I suppress the output? Thanks

Answer: import os import sys # silence command-line output temporarily sys.stdout, sys.stderr = os.devnull, os.devnull # import the desired library import library # unsilence command-line output sys.stdout, sys.stderr = sys.__stdout__, sys.__stderr__
Question: I imagine this is a classic floating point precision question, but I am trying to wrap my head around this result, running 1//0.01 in Python 3.7.5 yields 99.  I imagine it is an expected result, but is there any way to decide when it is safer to use int(1/f) rather than 1//f ?

Answer: If this were division with real numbers, 1//0.01 would be exactly 100. Since they are floating-point approximations, though, 0.01 is slightly larger than 1/100, meaning the quotient is slightly smaller than 100. It's this 99.something value that is then floored to 99.
Question: I am trying to plot a Receiver Operating Characteristics (ROC) curve with cross validation, following the example provided in sklearn's documentation. However, the following import gives an ImportError, in both python2 and python3. from sklearn.metrics import plot_roc_curve  Error: Traceback (most recent call last): File "<stdin>", line 1, in <module> ImportError: cannot import name plot_roc_curve  python-2.7 sklearn version: 0.20.2. python-3.6 sklearn version: 0.21.3. I found that the following import works fine, but it's not quite the same as plot_roc_curve. from sklearn.metrics import roc_curve  Is plot_roc_curve deprecated? Could somebody try the code and let me know the sklearn version if it works?

Answer: Plotting API was introduced in the version 0.22. As mentioned here, Scikit-learn 0.20 was the last version to support Python 2.7 and Python 3.4. Scikit-learn now requires Python 3.5 or newer.
Question: This question has been asked and answered for Jupyter Notebooks here. There is one suggestion regarding JupyterLab there as well on how to hide cell output, but not to clear it. This is easy enough using the menu under Edit > Clear Outputs. But how do you do it using a keyboard shortcut? Many other commands under Edit already have their own assigned shortcuts, but not this one: 

Answer: The answer: You'll have to assign a custom shortcut key under Settings > Advanced Settings Editor by inserting the following under User Preferences: {// List of Keyboard Shortcuts "shortcuts": [ { "command": "notebook:clear-cell-output", "keys": [ "F10" ], "selector": ".jp-Notebook.jp-mod-editMode" }, ] }  I went for F10, but most other keys or combination of keys should work too. I've also used Ctrl Shift Enter. Where to put it:  Some details: If you've assigned other shortcuts, make sure to add it in the correct place in the list of other shortcuts. {// List of Keyboard Shortcuts "shortcuts": [ { "command": "notebook:run-in-console", "keys": [ "F9" ], "selector": ".jp-Notebook.jp-mod-editMode" }, { "command": "notebook:clear-cell-output", "keys": [ "F10" ], "selector": ".jp-Notebook.jp-mod-editMode" }, ] }  And a little mystery: If you insert the exact same thing as in the second box, you'll see that the item Run > Run Selected Text or Current Line in Console has gotten a nice F9 right next to it:  This will not be the case for the item Edit > Clear Outputs, and I'll have to say that I don't know why.  To my knowledge the "command": "notebook:clear-cell-output" that you're assigning to your chosen keyboard shortcut should be that exact functionality. But the good thing is that it works perfectly all the same. At least it does for me. Please note that this approach works best for newer versions of JupyterLab. The correct way will be a bit different for older versions. Here's a python snippet to test it out right away: import pandas as pd import numpy as np df = pd.DataFrame(np.random.randint(0,100,size=(100, 4)), columns=list('ABCD')) print(df)
Question: I'm trying to write a simple server in python. So after watching tutorial, I'm trying to import a few modules. from http.server import HTTPServer from http.server import SimpleHTTPServer  As the doc says, it has been moved, that's why i'm doing so. But it gives me this error : from http.server import SimpleHTTPServer ImportError: cannot import name 'SimpleHTTPServer' And without SimpleHTTPServer I can't use SimpleHTTPRequestHandler, as it is defined in SimpleHTTPServer.SimpleHTTPRequestHandler. How can I resolve this ?

Answer: The SimpleHTTPServer module was moved to be the module http.server. So the command is: python3 -m http.server  Also, the new SimpleHTTPRequestHandler object is BaseHTTPRequestHandler.
Question: I'm using Google Colab for deep learning and I'm aware that they randomly allocate GPU's to users. I'd like to be able to see which GPU I've been allocated in any given session. Is there a way to do this in Google Colab notebooks? Note that I am using Tensorflow if that helps.

Answer: Since you can run bash command in colab, just run !nvidia-smi:
Question: I want to select the first row when there are multiple rows with repeated values in a column. For example: import pandas as pd df = pd.DataFrame({'col1':['one', 'one', 'one', 'one', 'one', 'one', 'one', 'one'], 'col2':['ID=ABCD1234', 'ID=ABCD1234', 'ID=ABCD1234', 'ID=ABCD5678', 'ID=ABCD5678', 'ID=ABCD5678', 'ID=ABCD9102', 'ID=ABCD9102']})  The pandas dataframe looks like this: print(df) col1 col2 0 one ID=ABCD1234 1 one ID=ABCD1234 2 one ID=ABCD1234 3 one ID=ABCD5678 4 one ID=ABCD5678 5 one ID=ABCD5678 6 one ID=ABCD9102 7 one ID=ABCD9102  I want the row 0, row 3, and row 6 to be selected and output as a new dataframe. Expected output:  col1 col2 0 one ID=ABCD1234 3 one ID=ABCD5678 6 one ID=ABCD9102 

Answer: You can use: df.drop_duplicates(subset = ['col2'], keep = 'first', inplace = True)
Question:  import requests, socket, socks ModuleNotFoundError: No module named 'socks'  I have tried pip install socks, and followed instructions of other stackoverflow posts but none of them worked. I'm working on pycharm right now and I have also installed socks and socket on there, in fact, it does show I have installed it. And yes, I do have it on the right interpreter. How can I solve this?

Answer: I think you mean to install PySocks, so do pip install PySocks. pypi docs for PySocks pip install socks installs something different. pypi docs for socks
Question: I am ssh’d into a development environment (vagrant Ubuntu box) and my project directory is mapped to another filesystem (via vbox) so symlinks are not supported. I am attempting to create a new venv, but the --copies flag isn’t being respected. $sudo python -m venv --copies venv Error: [Errno 71] Protocol error: 'lib' -> '/home/vagrant/vagrant_projects/rurp/venv/lib64'  If I use python 2.7 ($virtualenv venv --always-copy) it works, but not with the python3 venv --copies implementation. The --always-copy argument was a workaround for similar issues with python2.x.  I could not find anything online indicating a bug in venv and am at a bit of a loss. Has anyone else had this issue?  $ python -V Python 3.6.9  Thank you in advance. Edit: Also tested in python 3.8.1.

Answer: Per @chepner's comment above, it looks like the --copies argument is ignored on non-Windows systems (no mention of this in the documentation). I was able to workaround the issue by creating the venv in a local directory, manually copying the symlinked lib64 to a real directory, moving the venv to my project folder and manually updating the activation scripts. Ugly, but it works. $cd ~ $python3 -m venv --copies --clear venv $cp -r --remove-destination `readlink lib64` lib64 $cp -r venv vagrant_project/rurp/  I would be happy to accept a more elegant answer.
Question: I know that the stub files for built-in Python library for type checking and static analysis come with mypy or PyCharm installation. How can I get stub files for matplotlib, numpy, scipy, pandas, etc.?

Answer: Type stubs are sometimes packaged directly with the library. Otherwise there can be some external libraries to provide them. Numpy Starting with numpy 1.20 type stubs will be included in numpy. See this changelog and this PR adding them Before that they could added with the library https://github.com/numpy/numpy-stubs Pandas and Matplotlib For pandas, official stub support can be found in the pandas-stubs repository, which is the official pandas stubs project. There is no official support for stubs in Matplotlib, but you can check for community-contributed stubs or create your own if needed. In some cases, you might also find unofficial stubs in projects like data-science-types, but for the most up-to-date and reliable stubs, it's recommended to use the official sources mentioned above.
Question: How to start a new Django project using poetry? With virtualenv it is simple: virtualenv -p python3 env_name --no-site-packages source env_name/bin/activate pip install django django-admin.py startproject demo pip freeze > requirements.txt  What will be equivalent to this using Poetry?

Answer: Create a new project folder and step in: $ mkdir djangodemo $ cd djangodemo  Create a basic pyproject.toml with django as dependency: $ poetry init --no-interaction --dependency django  Create venv with all dependencies needed: $ poetry install  Init your demo-project: For Django versions after 4: $ poetry run django-admin startproject djangodemo  For Django version less than 4: $ poetry run django-admin.py startproject djangodemo
Question: I am currently trying to save a list of numpy arrays into a single file, an example of such a list can be of the form below import numpy as np np_list = [] for i in range(10): if i % 2 == 0: np_list.append(np.random.randn(64)) else: np_list.append(np.random.randn(32, 64))  I can combine all of them using into a single file using savez by iterating through list but is there any other way? I am trying to save weights returned by the function model.get_weights(), which is a list of ndarray and after retrieving the weights from the saved file I intend to load those weights into another model using model.set_weights(np_list). Therefore the format of the list must remain the same. Let me know if anyone has an elegant way of doing this.

Answer: I would go with np.save and np.load because it's platform-independent, faster than savetxt and works with lists of arrays, for example: import numpy as np a = [ np.arange(100), np.arange(200) ] np.save('a.npy', np.array(a, dtype=object), allow_pickle=True) b = np.load('a.npy', allow_pickle=True)  This is the documentation for np.save and np.load. And in this answer you can find a better discussion How to save and load numpy.array() data properly? Edit Like @AlexP mentioned numpy >= v1.24.2 does not support arrays with different sizes and types, so that's why the casting is necessary.