Linear function with live data - function

I am an absolute newbie on the programming thing and really desperate. I picked up a high end task to solve as it seems to me...
I know there are tons of explanations for solving y = mx + b with python but they're all for the situation with "solid" data. I am trying to realize it with live data.
So far, i have two data streams, which I successful directed into two lists - please see code below.
for graph in basis_graph:
high_1 = float(graph.high)
low_1 = float(graph.low)
if high_1 > 0:
graph_high.append([high_1])
if low_1 > 0:
graph_low.append([low_1])
Now comes the tricky part and I DON`T GET IT. I need a function that calculates me "m". Something like that:
def function_signal():
if graph_high[-1] < graph_high[-2]:
please, mr. computer, calculate me "m"
I tried something like
def signal():
if graph_low[-1] < graph_low[-2]:
print("a")
ay1 = graph_low[-1]
by1 = graph_low[-2]
m = ay1 - by1
return m
print(m(ay1, ay2))
Two days I tried EVERYTHING from what I know so far but the only thing I earned was a cascade of Tracebacks. From "I can't divide two list objects" to " "m" is not defined" and so on and so on...
In the case above for example NOTHING happens. Sometimes he says "m is not defined"...
Please, if there's someone out there who is willing to help me than I would really appreciate it.
Thanks in advance.

Related

Detect inquiry sentence in Wav2Vec 2.0 result

I am studying ASR(Automatic Speech Recognition) using Wav2Vec2.0.
When I run Wav2Vec2.0, I get the result without a comma("."), question mark("?") etc. Therefore, the result came out as one whole sentence.
I know that I removed regex while making the tokenizer.
Is there any way to convert to the perfect sentence which contains regex?
Original Text from wav file = "So what which one is better?"
Wav2Vec 2.0 Result = "SO WHAT WHICH ONE IS BETTER" (Question mark missing)
Expected Result = "SO WHAT WHICH ONE IS BETTER?"
Most of the ASR are trained on open source datasets and all them has remove all kinf punctuation from it. If you like to have punctuation in the final output. Try to pass ASR output into following code.
from transformers import T5Tokenizer, T5ForConditionalGeneration
model_name = "flexudy/t5-small-wav2vec2-grammar-fixer"
tokenizer = T5Tokenizer.from_pretrained(model_name)
model = T5ForConditionalGeneration.from_pretrained(model_name)
sent = """WHEN ARE YOU COMING TOMORROW I AM ASKING BECAUSE OF THE MONEY YOU OWE ME PLEASE GIVE IT TO ME I AM WAITING YOU HAVE BEEN AVOIDING ME SINCE TWO THOUSAND AND THREE"""
input_text = "fix: { " + sent + " } </s>"
input_ids = tokenizer.encode(input_text, return_tensors="pt", max_length=256, truncation=True, add_special_tokens=True)
outputs = model.generate(
input_ids=input_ids,
max_length=256,
num_beams=4,
repetition_penalty=1.0,
length_penalty=1.0,
early_stopping=True
)
sentence = tokenizer.decode(outputs[0], skip_special_tokens=True, clean_up_tokenization_spaces=True)
print(f"{sentence}")
You can see following results as a output.
When are you coming tomorrow? I am asking because of the money you owe me, please give it to me. I am waiting. You have been avoiding me since 2003.
For better understanding check this model on HuggingFace.
https://huggingface.co/flexudy/t5-small-wav2vec2-grammar-fixer

Loading json file using torchtext

I'm working on the dailydialog dataset, which I've converted into a
JSON file which looks something like this:
[{"response": "You know that is tempting but is really not good for our fitness.", "message": "Say, Jim, how about going for a few beers after dinner?"}, {"response": "Do you really think so? I don't. It will just make us fat and act silly. Remember last time?", "message": "What do you mean? It will help us to relax."}, {"response": "I suggest a walk over to the gym where we can play singsong and meet some of our friends.", "message": "I guess you are right. But what shall we do? I don't feel like sitting at home."}, {"response": "Sounds great to me! If they are willing, we could ask them to go dancing with us.That is excellent exercise and fun, too.", "message": "That's a good idea. I hear Mary and Sally often go there to play pingpong.Perhaps we can make a foursome with them."}, {"response": "All right.", "message": "Please lie down over there."}]
So, each item has two keys - response and message.
This is my first time using PyTorch, so I was following a few online available resources. These are the relevant snippets of my code:
def tokenize_en(text):
return [tok.text for tok in spacy_en.tokenizer(text)]
src = Field(tokenize = tokenize_en,
init_token = '<sos>',
eos_token = '<eos>',
lower = True)
fields = {'response': ('r', src)}
train_data, test_data, validation_data = TabularDataset.splits(
path = 'FilePath',
train = 'trainset.json',
test = 'testset.json',
validation = 'validationset.json',
format = 'json',
fields = fields
)
Although no errors are raised, despite having many items in my JSON file, the train, test and validation datasets strangely have only 1 example each, as seen in this image:
Image Showing the length of train_data, test_data and validation_data
I'd be really grateful if someone could point out the error to me.
Edit: I found out that the whole file is being treated as a single text string due to lack of indents in the file. But if I indent the JSON file, the TabularDataset function throws a JSONDecodeError to me, suggesting it can no more decode the file. How can I get rid of this problem?
I think the code is alright, but the issue is with your JSON file. Can you try removing the square brackets("[]") at the beginning and the end of the file?
Probably that is the reason that Your Python file is reading it as one single object.

Rails quickly find record where has_many associations exactly match array of association ids

I've got a Product model which has_many OptionValue records, which describe color, size, etc.
Within my code, I need to query the Product model where the product.option_values.pluck(:id) array exactly matches an array of (e.g.) options = [1, 6, 4].
Running something like Product.includes(:option_values).where(option_values: { id: options_array }) returns all values that match at least one element of the options array, rather than all of them.
I've developed an inefficient way of getting the record I need, as follows:
Product.all.each { |v| return v if v.option_values.pluck(:id).sort == options_array.sort }
Obviously the above is way ott and I'm sure there's a simpler way to handle this, and I'm happy to use ActiveRecord or a straight SQL query (though I'm not too hot on the latter, so haven't come up with anything yet).
Any advice on the best way of achieving this greatly appreciated. Not sure I've explained this perfectly, so please comment if you've any questions.
Thanks in advance, Steve.
Clocked this in my old questions and threw together a quick and easy solution, so dropping it here for anyone else that might come this way:
product_options = product.option_values.pluck(:id)
unless options_array.length < product_options.length
(product_options & options_array).length == product_options.length
end
This:
checks the options_array is long enough to contain the necessary matches
looks for the elements in common across the two arrays ((product_options & options_array))
measures their length
checks this is the same length as the desired array product_options, meaning all the required options were found.
Alternatively, you can subtract one from the other and check there's nothing leftover (i.e. missing):
(product_options - options_array).empty?
Hope this helps someone at some point.

how to fix this easy digital communication excercise using MATLAB

I have a problem in writing in MATLAB a program that modulates data bits, adds noise then demodulates it and calculate the bit error and the symbol error rate. The modulation used first should be QPSK. I have done the following,
N=100;
databits = randi([0 3],N,1);
hModulator = comm.QPSKModulator;
hModulator.PhaseOffset = pi/4;
txsig = step(hModulator, databits);
scatterplot(txsig)
grid on
SNRdB=-10
rxsig = awgn(txsig,SNRdB);
H = comm.QPSKDemodulator
hH.PhaseOffset = pi/4;
symdecoded=step(H,rxsig)
symerr(symdecoded,databits)
biterr(symdecoded, databits)
My first question is I don't think I am doing the bit error rate and symbol error rate correctly, can someone help me spot where the problem is? Where am I missing out?
I am then asked to repeat the same problem but make the changes needed to make it work with 16-QAM and 64-QAM by changing a parameter called CONSTELLATION.
I have tried using demod and ddemodce but these two functions are removed from matlab? Does anyone know how to proceed?
I dont know why you are using SNRdB=-10 dB?
Try for example positive numbers 0 10 20 30, and you may save symerr and biterr as the function proposes [number,ratio] = symerr(x,y)
number= numb of error and ratio = bitsTx/bitsRx
For m-qam better use modem.qammod try the help in matlab
Best regards and good luck

Strange output of a very simple code?

I'm very new to python, but I found this strange:
Code: I was typing in Sagemathcloud's sageworksheet file:
x=y+1
def f(x):
return y
x=-1
print(x)
print(f(x))
As an output, I kept getting 7 or sometimes 49, something quite randomnumbers no matter what value of x I input. Any insights please?
P.S. I'm trying to make it look like a code, but I'm, not sure how to do that, for example, def was in the next lmine after I typed: x=y+1.
If that's the only code you have, then y isn't defined before it is used. y will take the value of what was previously in the memory space it's using - and that may not even be an int which causes the randomness of the numbers you're experiencing.