Reading TDMS files in python_ how to use tdmsinfo command? - python
I would like to know what is the content of a tdms file, which is produced by Labview.
Following this site, I write in Python:
import numpy as np
from nptdms import TdmsFile
from nptdms import tdms
#read a tdms file
filenameS = "RESULTS.tdms"
tdms_file = TdmsFile(filenameS)
tdmsinfo [--properties] tdms_file
I receive the following error:
tdmsinfo [--properties] tdms_file
^
SyntaxError: invalid syntax
I do not how to fix it.
Thank you for your help :)
What you are looking for is:
First create a TMDS objet from file:
tdms_file = TdmsFile("C:\\Users\\XXXX\\Desktop\\xx Python\\XXXX.tdms")
then get the group names with:
tdms_groups = tdms_file.groups()
after you can figure out which group names you have into the file, just write
tdms_groups
It will print the following:
['Variables_1', 'Variables_2', 'Variables_3', 'Variables_4', etc..]
With group names now u will be able to get channels with the following:
tdms_Variables_1 = tdms_file.group_channels("Variables_1")
Next print your channels contain into that group:
tdms_Variables_1
It will show:
[ TdmsObject with path /'Variables_1'/'Channel_1', TdmsObject with path /'Variables_1'/'Channel_2', etc..]
At the end get the vectors and its data:
MessageData_channel_1 = tdms_file.object('Variables_1', 'Channel_1')
MessageData_data_1 = MessageData_channel_1.data
Check your data
MessageData_data_1
do stuff with your data!
cheers!
To loop over all properties from the root object try this:
#read a tdms file
filenameS = "RESULTS.tdms"
tdms_file = TdmsFile(filenameS)
root_object = tdms_file.object()
# Iterate over all items in the properties dictionary and print them
for name, value in root_object.properties.items():
print("{0}: {1}".format(name, value))
That should give you all properties names.
Your problem seems to be that tdmsinfo will not work inside a Python script as it is not a python command: it's "a command line program".
The solution is to either use 'tdmsinfo' from a windows shell, or make a wrapper in python so that it runs the command in a subprocess for you. For instance in Python3 with the subprocess package
import subprocess
tdmsfile='my_file.tdms'
# startup info to hide the windows shell
si = subprocess.STARTUPINFO()
si.dwFlags |= subprocess.STARTF_USESHOWWINDOW
#si.wShowWindow = subprocess.SW_HIDE # default
# run tdmsinfo in a subprocess and capture the output in a
a = subprocess.run(['tdmsinfo',tdmsfile],
stdout=subprocess.PIPE,
startupinfo=si).stdout
a = a.decode('utf-8')
print(a)
the code above should give you only the channels and groups, but you can also run with the -p flag to include all the TDMS object properties
a = subprocess.run(['tdmsinfo','-p',tdmsfile],
stdout=subprocess.PIPE,
startupinfo=si).stdout
Related
Python file path in Windows
I am using Windows 10. I have this code, script_dir = os.path.dirname(__file__) temp = cs(os.path.join(script_dir+"first.txt"), os.path.join(script_dir+"second.text"), os.path.join(script_dir+"third.txt")) It executes in git bash, but it throws an error in powershell and cmd. How can I fix this code, so that I can execute this code in anywhere? ============================================================ Edit: it says, it cannot find .first.txt and following files. It also throws this error, DLL load failed: The specified module could not be found. ============================================================ Edit2: cs is a class I created. class cs: info = {} result = {} def __init__(self, first, second, third, output=None): self.output = "" self.first = first self.second = second self.third = third def decrypt(self): pass I don't know why it works in git bash, but not in powershell and cmd
The correct code is script_dir = os.path.dirname(__file__) temp = cs(os.path.join(script_dir, "first.txt"), os.path.join(script_dir, "second.text"), os.path.join(script_dir, "third.txt")) What you are doing wrong, is adding "first.txt" etc to script_dir, and passing that to os.path.join. os.path.join, however, takes multiple arguments (any number of arguments) and joins them together in the correct way. What your code did, is add those strings together, making: script_dirfirst.txt, which would explain why it couldn't find the file.
How to know which file is calling which file, filesystem
How to know which file is calling which file in filesystem, like file1.exe is calling file2.exe so file2.exe is modified, and file1.exe is entered in log file. winos I have searched INTERNET but not able to find any samples.
In order know which file is calling which file you can use the Trace module exp: if you have 2 files ***file1.py*** import file2 def call1(): file2.call2() ***file2.py*** def call2(): print "---------" u can use it using console: $ python -m trace --trackcalls path/to/file1.py or within a program using a Trace object ****tracefile.py*** import trace,sys from file1 import call1 #specify what to trace here tracer = trace.Trace(ignoredirs=[sys.prefix, sys.exec_prefix], trace=0, count=1) tracer.runfunc(call1) #call the function call1 in fille1 results = tracer.results() results.write_results(summary=True, coverdir='.')
Create Ipython magic command for saving last console input to file
Remark now I found a solution of doing it. I want to implement my own magic command in ipython which saves the last input to a python file in order to produce executable python code interactively: I thought about saving it as own magicfile.py in the ipython startup directory: #Save this file in the ipython profile startup directory which can be found via: #import IPython #IPython.utils.path.locate_profile() from IPython.core.magic import (Magics, magics_class, line_magic, cell_magic, line_cell_magic) # The class MUST call this class decorator at creation time #magics_class class MyMagics(Magics): #line_magic def s(self, line): import os import datetime today = datetime.date.today() get_ipython().magic('%history -l 1 -t -f history.txt /') with open('history.txt', 'r') as history: lastinput = history.readline() with open('ilog_'+str(today)+'.py', 'a') as log: log.write(lastinput) os.remove('history.txt') print 'Successfully logged to ilog_'+str(today)+'.py!' # In order to actually use these magics, you must register them with a # running IPython. This code must be placed in a file that is loaded once # IPython is up and running: ip = get_ipython() # You can register the class itself without instantiating it. IPython will # call the default constructor on it. ip.register_magics(MyMagics) So right now i type in a command in ipython, then s; and it appends it to the logfile of today.
Use the append argument, -a, with %save. If this is the line you wish to save: In [10]: print 'airspeed velocity of an unladen swallow: ' Then save it like this: In [11]: %save -a IPy_session.py 10 The following commands were written to file `IPy_session.py`: print 'airspeed velocity of an unladen swallow: ' See the Ipython %save documentation
It works by using the IPython Magic history. In the history the old inputs are saved and you just pick the last one and append it to a file with the date of today, so that you can save all inputs from one day in one log-file. The important lines are get_ipython().magic('%history -l 1 -t -f history.txt /') with open('history.txt', 'r') as history: lastinput = history.readline() with open('ilog_'+str(today)+'.py', 'a') as log: log.write(lastinput) os.remove('history.txt')
How to implement MVC like this in Python?
For example, I have a config file named rule1.conf like this: [Basis] user = "sunhf" time = "2012-12-31" [Bowtie] path = "/usr/bin/bowtie" index = "/mnt/Storage/sync/hg19" And a models.py like this(using a package named magic.py..): from magic import Section class Conf: __confname__ = None basis = Section(["user", "time"]) bowtie = Section(["path", "index"]) At last, a viewer.py like this: from models import Conf as my_conf my_conf.__confname__ = "rule1.conf" // bind to the config file, I have no ideas how to do this print my_conf.basis.user // output: `sunhf` print my_conf.bowtie.index // output: `/mnt/Storage/sync/hg19` When I run viewer.py in command line: $ python viewer.py sunhf /mnt/Storage/sync/hg19 Does anyone have any ideas about how to implement the magic.py? Thanks!
I put my solution here: https://github.com/hanfeisun/meta_model Use python test.py to see the result
How to add file extensions based on file type on Linux/Unix?
This is a question regarding Unix shell scripting (any shell), but any other "standard" scripting language solution would also be appreciated: I have a directory full of files where the filenames are hash values like this: fd73d0cf8ee68073dce270cf7e770b97 fec8047a9186fdcc98fdbfc0ea6075ee These files have different original file types such as png, zip, doc, pdf etc. Can anybody provide a script that would rename the files so they get their appropriate file extension, probably based on the output of the file command? Answer: J.F. Sebastian's script will work for both ouput of the filenames as well as the actual renaming.
Here's mimetypes' version: #!/usr/bin/env python """It is a `filename -> filename.ext` filter. `ext` is mime-based. """ import fileinput import mimetypes import os import sys from subprocess import Popen, PIPE if len(sys.argv) > 1 and sys.argv[1] == '--rename': do_rename = True del sys.argv[1] else: do_rename = False for filename in (line.rstrip() for line in fileinput.input()): output, _ = Popen(['file', '-bi', filename], stdout=PIPE).communicate() mime = output.split(';', 1)[0].lower().strip() ext = mimetypes.guess_extension(mime, strict=False) if ext is None: ext = os.path.extsep + 'undefined' filename_ext = filename + ext print filename_ext if do_rename: os.rename(filename, filename_ext) Example: $ ls *.file? | python add-ext.py --rename avi.file.avi djvu.file.undefined doc.file.dot gif.file.gif html.file.html ico.file.obj jpg.file.jpe m3u.file.ksh mp3.file.mp3 mpg.file.m1v pdf.file.pdf pdf.file2.pdf pdf.file3.pdf png.file.png tar.bz2.file.undefined Following #Phil H's response that follows #csl' response: #!/usr/bin/env python """It is a `filename -> filename.ext` filter. `ext` is mime-based. """ # Mapping of mime-types to extensions is taken form here: # http://as3corelib.googlecode.com/svn/trunk/src/com/adobe/net/MimeTypeMap.as mime2exts_list = [ ["application/andrew-inset","ez"], ["application/atom+xml","atom"], ["application/mac-binhex40","hqx"], ["application/mac-compactpro","cpt"], ["application/mathml+xml","mathml"], ["application/msword","doc"], ["application/octet-stream","bin","dms","lha","lzh","exe","class","so","dll","dmg"], ["application/oda","oda"], ["application/ogg","ogg"], ["application/pdf","pdf"], ["application/postscript","ai","eps","ps"], ["application/rdf+xml","rdf"], ["application/smil","smi","smil"], ["application/srgs","gram"], ["application/srgs+xml","grxml"], ["application/vnd.adobe.apollo-application-installer-package+zip","air"], ["application/vnd.mif","mif"], ["application/vnd.mozilla.xul+xml","xul"], ["application/vnd.ms-excel","xls"], ["application/vnd.ms-powerpoint","ppt"], ["application/vnd.rn-realmedia","rm"], ["application/vnd.wap.wbxml","wbxml"], ["application/vnd.wap.wmlc","wmlc"], ["application/vnd.wap.wmlscriptc","wmlsc"], ["application/voicexml+xml","vxml"], ["application/x-bcpio","bcpio"], ["application/x-cdlink","vcd"], ["application/x-chess-pgn","pgn"], ["application/x-cpio","cpio"], ["application/x-csh","csh"], ["application/x-director","dcr","dir","dxr"], ["application/x-dvi","dvi"], ["application/x-futuresplash","spl"], ["application/x-gtar","gtar"], ["application/x-hdf","hdf"], ["application/x-javascript","js"], ["application/x-koan","skp","skd","skt","skm"], ["application/x-latex","latex"], ["application/x-netcdf","nc","cdf"], ["application/x-sh","sh"], ["application/x-shar","shar"], ["application/x-shockwave-flash","swf"], ["application/x-stuffit","sit"], ["application/x-sv4cpio","sv4cpio"], ["application/x-sv4crc","sv4crc"], ["application/x-tar","tar"], ["application/x-tcl","tcl"], ["application/x-tex","tex"], ["application/x-texinfo","texinfo","texi"], ["application/x-troff","t","tr","roff"], ["application/x-troff-man","man"], ["application/x-troff-me","me"], ["application/x-troff-ms","ms"], ["application/x-ustar","ustar"], ["application/x-wais-source","src"], ["application/xhtml+xml","xhtml","xht"], ["application/xml","xml","xsl"], ["application/xml-dtd","dtd"], ["application/xslt+xml","xslt"], ["application/zip","zip"], ["audio/basic","au","snd"], ["audio/midi","mid","midi","kar"], ["audio/mpeg","mp3","mpga","mp2"], ["audio/x-aiff","aif","aiff","aifc"], ["audio/x-mpegurl","m3u"], ["audio/x-pn-realaudio","ram","ra"], ["audio/x-wav","wav"], ["chemical/x-pdb","pdb"], ["chemical/x-xyz","xyz"], ["image/bmp","bmp"], ["image/cgm","cgm"], ["image/gif","gif"], ["image/ief","ief"], ["image/jpeg","jpg","jpeg","jpe"], ["image/png","png"], ["image/svg+xml","svg"], ["image/tiff","tiff","tif"], ["image/vnd.djvu","djvu","djv"], ["image/vnd.wap.wbmp","wbmp"], ["image/x-cmu-raster","ras"], ["image/x-icon","ico"], ["image/x-portable-anymap","pnm"], ["image/x-portable-bitmap","pbm"], ["image/x-portable-graymap","pgm"], ["image/x-portable-pixmap","ppm"], ["image/x-rgb","rgb"], ["image/x-xbitmap","xbm"], ["image/x-xpixmap","xpm"], ["image/x-xwindowdump","xwd"], ["model/iges","igs","iges"], ["model/mesh","msh","mesh","silo"], ["model/vrml","wrl","vrml"], ["text/calendar","ics","ifb"], ["text/css","css"], ["text/html","html","htm"], ["text/plain","txt","asc"], ["text/richtext","rtx"], ["text/rtf","rtf"], ["text/sgml","sgml","sgm"], ["text/tab-separated-values","tsv"], ["text/vnd.wap.wml","wml"], ["text/vnd.wap.wmlscript","wmls"], ["text/x-setext","etx"], ["video/mpeg","mpg","mpeg","mpe"], ["video/quicktime","mov","qt"], ["video/vnd.mpegurl","m4u","mxu"], ["video/x-flv","flv"], ["video/x-msvideo","avi"], ["video/x-sgi-movie","movie"], ["x-conference/x-cooltalk","ice"]] #NOTE: take only the first extension mime2ext = dict(x[:2] for x in mime2exts_list) if __name__ == '__main__': import fileinput, os.path from subprocess import Popen, PIPE for filename in (line.rstrip() for line in fileinput.input()): output, _ = Popen(['file', '-bi', filename], stdout=PIPE).communicate() mime = output.split(';', 1)[0].lower().strip() print filename + os.path.extsep + mime2ext.get(mime, 'undefined') Here's a snippet for old python's versions (not tested): #NOTE: take only the first extension mime2ext = {} for x in mime2exts_list: mime2ext[x[0]] = x[1] if __name__ == '__main__': import os import sys # this version supports only stdin (part of fileinput.input() functionality) lines = sys.stdin.read().split('\n') for line in lines: filename = line.rstrip() output = os.popen('file -bi ' + filename).read() mime = output.split(';')[0].lower().strip() try: ext = mime2ext[mime] except KeyError: ext = 'undefined' print filename + '.' + ext It should work on Python 2.3.5 (I guess).
You can use file -i filename to get a MIME-type. You could potentially lookup the type in a list and then append an extension. You can find a list of MIME-types and example file extensions on the net.
Following csl's response: You can use file -i filename to get a MIME-type. You could potentially lookup the type in a list and then append an extension. You can find list of MIME-types and suggested file extensions on the net. I'd suggest you write a script that takes the output of file -i filename, and returns an extension (split on spaces, find the '/', look up that term in a table file) in your language of choice - a few lines at most. Then you can do something like: ls | while read f; do mv "$f" "$f".`file -i "$f" | get_extension.py`; done in bash, or throw that in a bash script. Or make the get_extension script bigger, but that makes it less useful next time you want the relevant extension. Edit: change from for f in * to ls | while read f because the latter handles filenames with spaces in (a particular nightmare on Windows).
Of course, it should be added that deciding on a MIME type just based on file(1) output can be very inaccurate/vague (what's "data" ?) or even completely incorrect...
Agreeing with Keltia, and elaborating some on his answer: Take care -- some filetypes may be problematic. JPEG2000, for example. And others might return too much info given the "file" command without any option tags. The way to avoid this is to use "file -b" for a brief return of information.BZT