def show_result(self, prediction, msg): new_line(50) print(msg) new_line(50) real = self.train_labels print "Confusion Matrix: " print str(confusion_matrix(real, prediction)) homo_score = homogeneity_score(real, prediction) complete_score = completeness_score(real, prediction) v_score = v_measure_score(real, prediction) rand_score = adjusted_rand_score(real, prediction) mutual_info = adjusted_mutual_info_score(real, prediction) print("Homogeneity Score: %0.3f" % homo_score) print("Completeness Score: %0.3f" % complete_score) print("V-measure: %0.3f" % v_score) print("Adjusted Rand Score: %0.3f" % rand_score) print("Adjusted Mutual Info Score: %0.3f\n" % mutual_info) return { 'Homogeneity': homo_score, 'Completeness': complete_score, 'V-measure': v_score, 'RAND': rand_score, 'Mutual': mutual_info }
def __init__(self): new_line(50) print 'started to build all training and testing data...' self.tfidf_transformer = TfidfTransformer() self.vectorizer = CountVectorizer(analyzer='word', stop_words=stop_words, min_df=5, tokenizer=stemTokenizer) self.svd = TruncatedSVD(n_components=50, random_state=42) self.nmf = NMF(n_components=50, random_state=42) self.mm = MinMaxScaler() # build training data self.train_data = fetch_data(categories, 'train') self.train_labels = build_labels(self.train_data) self.vectors = self.to_vec(self.train_data.data) self.tfidf = self.to_tfidf(self.vectors) self.tfidf_SVD = self.to_SVD(self.tfidf) self.tfidf_NMF = self.to_NMF(self.tfidf) self.tfidf_mm = self.mm.fit_transform(self.tfidf_SVD) # build testing data self.test_data = fetch_data(categories, 'test') self.test_labels = build_labels(self.test_data) self.test_vectors = self.vectorizer.transform(self.test_data.data) self.test_tfidf = self.tfidf_transformer.transform(self.test_vectors) self.test_tfidf_SVD = self.svd.transform(self.test_tfidf) self.test_tfidf_NMF = self.nmf.transform(self.test_tfidf) self.test_tfidf_mm = self.mm.fit_transform(self.test_tfidf_SVD) print 'finished building all training and testing data...' new_line(50) print ' '
def parse_acquisitions(self): src_paths = [] for f in self._files: # Check if it is a DICOM try: ds = dicom.read_file(f) except InvalidDicomError: utils.warning('Not a DICOM file: {}'.format(f)) continue # Filter by serie description dsDesc = ds.SeriesDescription if any(_ in dsDesc for _ in self.exclusionCriteria): self._excluded.add(dsDesc) else: src_paths.append(f) groups = dcmstack.parse_and_group(src_paths) for key, group in groups.iteritems(): if self._isFromOneDirectory(group): #utils.ok("Same directory: {}".format(key[2])) stack = dcmstack.stack_group(group) inDir = os.path.dirname(group[0][2]) self._acquisitions.append( Acquisition(inDir, stack, self._session)) else: #TODO: regroup dicoms in a tmp directory utils.new_line() utils.fail( "DICOM of '{}' are not in the same directory.\nThis structure won't be compatible with dcm2niibatch" .format(key[2]))
def generateNSISInstaller( self ): """ runs makensis to generate the installer itself """ self.isInstalled() if self.package.endswith( "-package" ): shortPackage = self.package[ : -8 ] else: shortPackage = self.package if not "setupname" in self.defines or not self.defines[ "setupname" ]: self.defines[ "setupname" ] = "%s-%s-setup-%s.exe" % ( shortPackage, compiler.architecture(), self.buildTarget ) if not "srcdir" in self.defines or not self.defines[ "srcdir" ]: self.defines[ "srcdir" ] = self.imageDir() if not "company" in self.defines or not self.defines[ "company" ]: self.defines[ "company" ] = "KDE" if not "productname" in self.defines or not self.defines[ "productname" ]: self.defines[ "productname" ] = shortPackage.capitalize() if not "version" in self.defines or not self.defines[ "version" ]: self.defines[ "version" ] = self.buildTarget if not "executable" in self.defines or not self.defines[ "executable" ]: self.defines[ "executable" ] = "" if "license" in self.defines and self.defines[ "license" ]: self.defines[ "license" ] = "!insertmacro MUI_PAGE_LICENSE \"%s\"" % self.defines[ "license" ] else: self.defines[ "license" ] = "" if "icon" in self.defines and self.defines[ "icon" ]: self.defines[ "icon" ] = "!define MUI_ICON \"%s\"" % self.defines[ "icon" ] else: self.defines[ "icon" ] = "" if not self.scriptname: self.scriptname = os.path.join( os.path.dirname( __file__ ), "NullsoftInstaller.nsi" ) self.defines[ "architecture" ] = compiler.architecture() # make absolute path for output file if not os.path.isabs( self.defines[ "setupname" ] ): dstpath = self.packageDestinationDir() self.defines[ "setupname" ] = os.path.join( dstpath, self.defines[ "setupname" ] ) definestring = "" for key in self.defines: definestring += " /D" + key + "=\"" + self.defines[ key ] + "\"" utils.new_line() utils.debug( "generating installer %s" % self.defines[ "setupname" ] ) if self.isInstalled: if not utils.systemWithoutShell( "\"%s\" %s %s" % ( os.path.join( self.nsisInstallPath, 'makensis.exe' ), definestring, self.scriptname ), cwd = os.path.abspath( self.packageDir() ) ): utils.die("Error in makensis execution")
targetInstalled = installdb.isInstalled( mainCategory, mainPackage, mainVersion, os.getenv( "EMERGE_TARGET_PLATFORM" ) ) msg += portage.getHostAndTarget( hostEnabled and not hostInstalled, targetEnabled and not targetInstalled ) else: msg = "" utils.warning( "pretending %s/%s-%s%s" % ( mainCategory, mainPackage, mainVersion, msg ) ) else: mainAction = mainBuildAction if mainBuildAction == "install-deps": mainAction = "all" if not handlePackage( mainCategory, mainPackage, mainVersion, mainAction, mainOpts ): utils.error( "fatal error: package %s/%s-%s %s failed" % \ ( mainCategory, mainPackage, mainVersion, mainBuildAction ) ) exit( 1 ) utils.new_line() if len( nextArguments ) > 0: command = "\"" + sys.executable + "\" -u " + executableName + " " + " ".join( nextArguments ) #for element in environ.keys(): # if environ[ element ]: # os.environ[ element ] = environ[ element ] # elif element == "EMERGE_VERBOSE": # os.environ[ element ] = "1" # else: # os.environ[ element ] = "" if not utils.system(command): utils.die( "cannot execute next commands cmd: %s" % command ) utils.stopTimer("Emerge")
def run(self): utils.new_line() utils.info('Parse and group DICOM directory') self._parser.parse_acquisitions() utils.new_line() utils.info('Sort and set up acquisitions') self._parser.sort_acquisitions() #utils.new_line() #utils.ok('Acquisitions of interest:') #for _ in self._parser.caught: utils.info(_) utils.new_line() utils.warning('Acquisitions excluded:') for _ in self._parser._excluded: utils.info(_) utils.new_line() utils.info('Create YAML file for dcm2niibatch') for acq in self._parser.acquisitions: self._batch.add(acq) self._batch.write() utils.new_line() utils.ok('Batch file:') self._batch.show() if self._yes: launchBatch = True else: msg = "Do you want to launch dcm2niibatch ?" launchBatch = utils.query_yes_no(msg) if launchBatch: self._batch.launch() for acq in self._parser.acquisitions: acq.update_json() else: utils.new_line() utils.ok("To launch dcm2niibatch later:") utils.info("cd {}".format(self._codeDir)) utils.info(self._batch.command) return 0
def main(): """ Testing the class""" # add two databases tempdbpath1 = os.path.join( os.getenv("KDEROOT"), "tmp", "temp1.db" ) tempdbpath2 = os.path.join( os.getenv("KDEROOT"), "tmp", "temp2.db" ) if not os.path.exists( os.path.join( os.getenv( "KDEROOT" ), "tmp" ) ): os.makedirs( os.path.join( os.getenv( "KDEROOT" ), "tmp" ) ) if os.path.exists( tempdbpath1 ): os.remove( tempdbpath1 ) if os.path.exists( tempdbpath2 ): os.remove( tempdbpath2 ) db_temp = InstallDB( tempdbpath1 ) db = InstallDB( tempdbpath2 ) utils.debug( 'testing installation database' ) utils.debug( 'testing if package win32libs/dbus-src with version 1.4.0 is installed: %s' % db.isPkgInstalled( 'win32libs', 'dbus-src', '1.4.0' ) ) # in case the package is still installed, remove it first silently if db.isInstalled( 'win32libs', 'dbus-src', '1.4.0' ): packageList = db.remInstalled( 'win32libs', 'dbus-src', '1.4.0' ) # really commit uninstall for package in packageList: package.uninstall() utils.debug_line() utils.new_line() # add a package utils.debug( 'installing package win32libs/dbus-src-1.4.0 (release)' ) package = db.addInstalled( 'win32libs', 'dbus-src', '1.4.0', 'release' ) package.addFiles( dict().fromkeys( [ 'test', 'test1', 'test2' ], 'empty hash' ) ) # now really commit the package package.install() # add another package in a different prefix utils.debug( 'installing package win32libs/dbus-src-1.4.0 (debug)' ) package = db.addInstalled( 'win32libs', 'dbus-src', '1.4.0', 'debug' ) package.addFiles( dict().fromkeys( [ 'test', 'test1', 'test2' ], 'empty hash' ) ) # now really commit the package package.install() utils.debug_line() utils.new_line() utils.debug( 'checking installed packages' ) utils.debug( 'get installed package (release): %s' % db.getInstalled( 'win32libs', 'dbus-src', 'release' ) ) utils.debug( 'get installed package (debug): %s' % db.getInstalled( 'win32libs', 'dbus-src', 'debug' ) ) utils.new_line() utils.debug( 'now trying to remove package & revert it again later' ) # remove the package again packageList = db.remInstalled( 'win32libs', 'dbus-src', '1.4.0' ) for pac in packageList: for line in pac.getFiles(): # pylint: disable=W0612 # we could remove the file here # print line pass utils.debug_line() utils.new_line() utils.debug( 'checking installed packages' ) utils.debug( 'get installed package (release): %s' % db.getInstalled( 'win32libs', 'dbus-src', 'release' ) ) utils.debug( 'get installed package (debug): %s' % db.getInstalled( 'win32libs', 'dbus-src', 'debug' ) ) utils.debug_line() utils.new_line() utils.debug( 'reverting removal' ) # now instead of completing the removal, revert it for pac in packageList: pac.revert() utils.debug( 'checking installed packages' ) utils.debug( 'get installed package (release): %s' % db.getInstalled( 'win32libs', 'dbus-src', 'release' ) ) utils.debug( 'get installed package (debug): %s' % db.getInstalled( 'win32libs', 'dbus-src', 'debug' ) ) utils.debug_line() db.getInstalled() db.getInstalled( category='win32libs', prefix='debug' ) db.getInstalled( package='dbus-src' ) utils.new_line() utils.debug( 'now really remove the package' ) packageList = db.remInstalled( 'win32libs', 'dbus-src', '1.4.0') for pac in packageList: utils.debug( 'removing %s files' % len( pac.getFiles() ) ) pac.uninstall() utils.debug( 'get installed package (release): %s' % db.getInstalled( 'win32libs', 'dbus-src', 'release' ) ) utils.debug( 'get installed package (debug): %s' % db.getInstalled( 'win32libs', 'dbus-src', 'debug' ) ) utils.debug_line() # test the import from the old style (manifest based) databases utils.new_line() db_temp.importExistingDatabase() print("getInstalled:", db_temp.getInstalled()) print("findInstalled:", portage.findInstalled( 'win32libs', 'dbus-src' )) print("getFileListFromManifest:", len( utils.getFileListFromManifest( os.getenv( "KDEROOT" ), 'dbus-src' ) ))