strip and filter strings in ragged tensor - tensorflow

I would like learn if there is a decent and tensorflownic way to do follow conversion. Basically each string(row) as several words, and each word has a suffix like "%1", and goal is to strip string and only leave words with suffix value <= certain target value.
It is not hard to achieve using regular python programming. But I am thinking of adding the step to a tf computational graph, so a more tensorflownic way is preferred.
#From
text = tf.constant(['a1%0,a2%0,a3%1,a4%2,a5%3,a6%4','a7%3,a8%4',...]) #in shape of (n,) and n is large
#if target = 1, result will be
res = tf.ragged.constant([["a1", "a2", "a3"], [],...])
#if target = 3, result will be
res = tf.ragged.constant([["a1", "a2", "a3", "a4", "a5"], ["a7"],...])

You can do the following (tested in tensorflow 2.9)
text = tf.constant(['a1%0,a2%0,a3%1,a4%2,a5%3,a6%4','a7%3,a8%4',...])
target = 1
a = tf.strings.split(tf.strings.regex_replace(text, "%\d", ""), ",")
b = tf.strings.split(tf.strings.regex_replace(text, "[^,]*%", ""), ",")
b = tf.strings.to_number(b)
c = tf.ragged.boolean_mask(a, (b<=target))

Related

Is there something like an in-place version of numpy concatenate?

I'd like to be able to concatenate multiple arrays and have changes in the original arrays reflected in the concatenated version. So the code below should not raise an AssertionError.
arr1 = np.zeros(3)
arr2 = np.zeros(3)
arr3 = np.in_place_concatenate([arr1, arr2])
arr1[0] = 10
assert arr3[0] == 10
Does this concept exist in numpy?

ETABS 2016 VBA SetOConcrete_1 assigns only the last Fc value for all definitions

I'm working on an Excel VBA module that supplies certain data to ETABS 2016, part of the code is to define multiple material properties through using API methods SetMaterial and SetOConcrete_1.
The effective part of the code is this
'Values:-
'Val(0)=label, Val(1)=Fc', Val(2)=E, Vals(3)=StrainAtFc, Val(4)=UltimateStrain, Val(5)=PoisonRatio, Val(6)=Thermal Coef
'Val(7) = UnitWeight
For i = 0 To iCount - 1
.
.
SOME LONG CODE TO RETRIEVE DATA TO A DYNAMIC ARRAY OF VARIANTS CALLED "VAL"
.
.
With myModel.PropMaterial
MsgBox Val(0) & " " & Val(1) & " " & i 'debug line to display input values for label and FC'
ret = .SetMaterial(Val(0), eMatType_Concrete)
ret = .SetOConcrete_1(Val(0), Val(1), False, 0, 2, 4, Val(3), Val(4), -0.1)
ret = .SetWeightAndMass(Val(0), 1, Val(7))
ret = .SetMPIsotropic(Val(0), Val(2), Val(5), Val(6))
End With
Next i
I've even added a debugging part to display the values of interest, per one definition, two definitions, and so on, this part is
'Debugging part
If i >= 0 Then
On Error GoTo 0
Err.Raise (2)
End If
'End of Debugging part
and then, I change i >= 0 to i >= 1 and to i >= 2 to display the needed number of iterations .
The debugging part always displays the correct and needed values of Val(0)(the material's Name) and Val(1)(the material's Fc) for every iteration,
for example, the values for every iteration are:-
1- Iteration no.1: Val(0) = "C35", Val(1) = 35000, i = 0
2- Iteration no.2: Val(0) = "C37", Val(1) = 37000, i = 1
3- Iteration no.3: Val(0) = "C38", Val(1) = 38000, i = 2
and Then, what happens in ETABS is the following:-
1- With i >= 0 I get one iteration and one material definition of "C35", "C35" has Fc = 35MPa [Correct Result]
2- With i >= 1 I get two iterations and two material definitions of "C35" and "C37", but all of them have Fc = 37 MPa [Wrong Result]
3- With i >= 2 I get three iterations and three material definitions of "C35", "C37" and "C38", but all of them have Fc = 38 MPa [Wrong Result]
So, How Can the supplied values of the material's name and Fc to SetOConcrete_1 be different at every iteration and ETABS just ignores all that work and just supplies only the last Fc value to all materials?!
Is the ETABS API taking the value you supply it as a reference? That's what it seems like based on your explanation.
Public Sub Temp()
'In order to use early-binding Dictionary you need to set a
'reference in Tools>References>"Microsoft Scripting Runtime"
Dim foo As Dictionary
Set foo = New Dictionary
Dim bar As Range
Set bar = ActiveSheet.Range("A1")
bar.Value2 = "Original"
foo.Add "1", bar.Value2
foo.Add "2", bar
bar.Value2 = "Changed"
Debug.Print foo("1")
Debug.Print foo("2")
End Sub
If you go through this example you'll notice that when an item is added with .Value2, value type, it doesn't change. When it's fed in as a Range, reference type, it will change.
You may have a different outcome if you extract each value in the Val variable into their respective type before feeding them in.
Dim label As String
label = Val(0)
Dim Fc As Long
Fc = Val(1)
Dim strainAtFc As Long
strainAtFc = Val(3)
Dim ultimateStrain As Long
ultimateStrain = Val(4)
Dim poisonRatio As Long
poisonRatio = Val(5)
Dim thermalCoefficient As Long
thermalCoefficient = Val(6)
Dim unitWeight As Long
unitWeight = Val(7)
***Note that Val is also a member of VBA.Conversion and could cause issues if it isn't Dimd.

Is it possible to use metric "k" notations in excel?

Is it possible to work in Excel with some metric suffix notation:
If I write 1000, the cell shows 1k. if I write 1000000 the cell shows 1M.
I made two functions to make a workaround but maybe there's a more suitable solution.
Function lecI(cadena) As Double
u = Right(cadena, 1)
If u = "k" Then
mult = 1000
ElseIf u = "M" Then
mult = 1000000
ElseIf u = "m" Then
mult = 0.001
End If
lecI = Val(Left(cadena, Len(cadena) - 1)) * mult
End Function
Function wriI(num) As String
If num > 1000000 Then 'M
wriI = Str(Round(num / 1000000, 2)) & "M"
ElseIf num > 1000 Then 'k
wriI = Str(Round(num / 1000, 1)) & "k"
ElseIf num < 0.01 Then 'm
wriI = Str(Round(num * 1000, 1)) & "m"
Else: wriI = Str(num)
End If
Based on the link by #Vasily, you can get the desired outcome using only Conditional Formatting. This is nice because it means that all of your values are stored as Numbers and not Text and math works like normal.
Overall steps:
Create a new conditional formatting for each block of 1000 that applies the number format for that block
Add the largest condition at the top so it formats first
Rinse and repeat to get all the ones you want
Conditional formatting used to style column C which is just random data at different powers of ten. It is the same number as column D just styled differently.
Number formats, are pretty easy since they are the same as that link, see Large Numbers section.
ones = 0 " "
thousands = 0, " k"
millions = 0,, " M"
and so on for however many you want
Automation, if you don't want to click and type all day, here is some VBA that will create all the conditional formatting for you (for current Selection). This example goes out to billions. Keep adding powers of 3 by extending the Array with more entries.
Sub CreateConditionalsForFormatting()
'add these in as powers of 3, starting at 1 = 10^0
Dim arr_markers As Variant
arr_markers = Array("", "k", "M", "B")
For i = UBound(arr_markers) To 0 Step -1
With Selection.FormatConditions.Add(xlCellValue, xlGreaterEqual, 10 ^ (3 * i))
.NumberFormat = "0" & Application.WorksheetFunction.Rept(",", i) & " "" " & arr_markers(i) & """"
.StopIfTrue = False
End With
Next
End Sub
I change the StopIfTrue value so that this does not break other conditional formatting that might exist. If the largest condition is at the top (added first) then the NumberFormat from that one holds. By default, these are created with StopIfTrue = True. This is a moot point if you do not have any other conditional formatting on these cells.

Rhino: Not All Arguments Converted During String Formatting

I am attempting to execute a code using Rhino Python and I am having some issues with the following TypeError:
Message: not all arguments converted during string formatting
The code I have written is meant to read point coordinates from a file "newpoints.csv" and use them as arguments for Rhino Python's 'AddLine' function.
#!/usr/bin/env python
import rhinoscriptsyntax as rs
file = open("C:\\Users\\Seshane Mahlo\\Documents\\MSc Thesis\\newpoints.csv", "r")
lines = file.readlines()
file.close()
ab = len(lines)
seq = range(0, ab-1, 2)
coordinates = []
startvals = []
stopvals = []
for line in lines:
coords = line.split(',')
xcoord = float(coords[0])
ycoord = float(coords[1])
point = (xcoord, ycoord)
coordinates.append(point)
starts = range(0, ab-2, 2)
ends = range(1, ab+1, 2)
for i,j in zip(starts, ends):
strt = coordinates[i]
stp = coordinates[j]
rs.AddLine(start=strt,end=stp)
I think there is a small mistake in your code here:
starts = range(0, ab-2, 2)
ends = range(1, ab-1, 2)
which should be
starts = range(0, ab-1, 2)
ends = range(1, ab, 2)
because the last element you get from the range function is one less than the stop argument.
But what is causing the error is that you are trying to add a line, which is composed of two 3d points using a 2-tuple (x,y)
To fix this change:
point = (xcoord, ycoord)
to
point = (xcoord, ycoord, 0)
or whatever you want your z-coordinate to be.

Lua Script Pattern Matching Problem

First of all, I have been using this site as a reference through the entire scripting process and it has been wonderful. I appreciate how useful and knowledgeable everyone is here. With that in mind, I have a question regarding matching (pattern matching) in Lua. I am writing a script that essentially takes input from a file and imports it into a table. I am checking for specific MAC addresses in the file as the host I am querying.
if macFile then
local file = io.open(macFile)
if file then
for line in file:lines() do
local f = line
i, j = string.find ( f, "%x+" )
m = string.sub(f, i, j)
table.insert( macTable, m )
end
file:close()
end
This parses the file into a format I will use to query later. Once the table is built, I run a pattern matching sequence to try and match the MAC from the table by iterating the table and matching the pattern against the current iteration:
local output = {}
t = "00:00:00:00:00:00"
s = string.gsub( t, ":", "")
for key,value in next,macTable,nil do
a, p = string.find ( s, value )
matchFound = string.sub(s, a, p)
table.insert( output, matchFound )
end
This doesn't return any output although when I enter it line by line in a Lua prompt, it seems to work. The variables are being passed correctly I believe. Any suggestions?
If your macFile uses a structure like this:
012345678900
008967452301
000000000000
ffffffffffff
The following script should work:
macFile = "./macFile.txt"
macTable = {}
if macFile then
local hFile = io.open(macFile, "r")
if hFile then
for line in hFile:lines() do
local _,_, sMac = line:find("^(%x+)")
if sMac then
print("Mac address matched: "..sMac)
table.insert(macTable, sMac)
end
end
hFile:close()
end
end
local output = {}
t = "00:00:00:00:00:00"
s = string.gsub( t, ":", "")
for k,v in ipairs(macTable) do
if s == v then
print("Matched macTable address: "..v)
table.insert(output, v)
end
end
I am just leaving from work and can't have a deeper look at your problem right now, but the next two lines seem quite odd.
t = "00:00:00:00:00:00"
s = string.gsub( t, ":", "")
Basically you are removing all the ':' characters in the string t. Afterwards you end up with s being "000000000000". This is probably not what you want?