I am porting some Matlab code to Python. In this code there are many instances of a large matrix being sliced in many different ways, eg M(2:45,13:18)
, M(:12,:)
, V(4)
, V(:27)
, etc. It is technically possible to convert this to numpy notation manually, by replacing the parentheses with square brackets and substracting 1 to all indices (except after :
) but it is incredibly tedious and there is a very high probability that I will make a typo at some point.
I tried automating this but my grep/awk/parsing skills are not good enough. Is there a script out there that will do the job?
CodePudding user response:
Assuming you have your matlab code in a string, you can format it in Python. You could do this with regex match replacement:
import re
def format_indexers(match):
# Format any "(x:y:z)" into python format
ret = match[0].replace("(", "[").replace(")", "]") # replace brackets
ret = re.sub(r"(\d ):", lambda x: str(int(x[1]) - 1) ":", ret) # replaces numbers by decremented ones
return ret
s = "M(2:45,13:18), M(:12,:), V(4), V(:27)"
# Searches expressions between parenthesis and apply format_indexers to found matches
re.sub(r"\(.*\)", format_indexers, s)
outputs:
'M[1:45,12:18], M[:12,:], V[3], V[:27]'
CodePudding user response:
Here is another take based on PlainRavioli's original answer:
INPUT = 'M(2:45,13:18) M(:12,:) V(4) V(:27)'
import re
def my_replace(m):
m = m.group()
a, m = m[0], m[2:-1]
m = m.split(',')
for k in range(len(m)):
if ':' in m[k]:
m[k] = m[k].split(':')
if m[k][0]:
m[k][0] = str(int(m[k][0])-1)
m[k] = ':'.join(m[k])
else:
m[k] = str(int(m[k])-1)
m = ','.join(m)
m = a '[' m ']'
return m
OUT = re.sub('\\w\\(\d*:?\d*(,\d*:?\d*)?\\)', my_replace, INPUT)
print(OUT)
# yields: M[1:45,12:18] M[:12,:] V[3] V[:27]