def connectivityTrend2Patent(Trend,trendConnectivity,patentlist):
	'''given a dictionary trendConnectivity {trend: connectivity},
		output the dictionary for patent connectivity {patent,connectivity}
		handle the case that a patent in different trend'''
	Connectivity4patent = {} #{patent:connectivity}
	for trend, patentlist in Trend.iteritems():
		connectivity = trendConnectivity[trend]
		for patent in patentlist:
			addintoTrendDic(connectivity,patent,Connectivity4patent)
	for patent, connectivitylist in Connectivity4patent.iteritems():
		if len(connectivitylist) == 1:
			Connectivity4patent[patent] = connectivitylist[0]
			continue
		else:
			patentconnectivity = []
			for i in range(len(connectivitylist[0])):
				# try:
				conn = [x for x in zip(*connectivitylist)[i] if x]
				# except TypeError:
				# 	print zip(*connectivitylist)
				# 	print zip(*connectivitylist)[i][0]
				# 	sys.exit()
				conn = None if len(conn) == 0 else sum(conn)/float(len(conn))
				if conn == None:
					print 'connectivity list do not have same length'
				patentconnectivity.append(conn)
			Connectivity4patent[patent] = patentconnectivity
	logging.info('finished connectivity which has %s patents'%(len(Connectivity4patent)))
	return Connectivity4patent
def connectivityTrend2Patent(Trend,trendConnectivity,patentlist):
	'''given a dictionary trendConnectivity {trend: connectivity},
		output the dictionary for patent connectivity {patent,connectivity}
		handle the case that a patent in different trend'''
	Connectivity4patent = {} #{patent:connectivity}
	for trend, patentlist in Trend.iteritems():
		connectivity = trendConnectivity[trend]
		for patent in patentlist:
			addintoTrendDic(connectivity,patent,Connectivity4patent)
	miss = 0
	for patent, connectivitylist in Connectivity4patent.iteritems():
		if len(connectivitylist) == 1:
			Connectivity4patent[patent] = connectivitylist[0]
			continue
		elif len(connectivitylist) == 0:
			miss += 1
		else:
			patentconnectivity = []
			for i in range(len(connectivitylist[0])):
				# try:
				conn = [x for x in zip(*connectivitylist)[i] if x]
				# except TypeError:
				# 	print zip(*connectivitylist)
				# 	print zip(*connectivitylist)[i][0]
				# 	sys.exit()
				
				if len(conn) == 0:
					print 'connectivity list is zero'
					
				conn = 0 if len(conn) == 0 else sum(conn)/float(len(conn))
				patentconnectivity.append(conn)
			Connectivity4patent[patent] = patentconnectivity
	logging.warning('%s patents do not have connectivity feature possible because they do not belong to any trends'%miss)
	logging.info('finished connectivity which has %s patents'%(len(Connectivity4patent)))
	return Connectivity4patent
def connectivityTrend2Patent(Trend, trendConnectivity, patentlist):
    '''given a dictionary trendConnectivity {trend: connectivity},
		output the dictionary for patent connectivity {patent,connectivity}
		handle the case that a patent in different trend'''
    Connectivity4patent = {}  #{patent:connectivity}
    for trend, patentlist in Trend.iteritems():
        connectivity = trendConnectivity[trend]
        for patent in patentlist:
            addintoTrendDic(connectivity, patent, Connectivity4patent)
    for patent, connectivitylist in Connectivity4patent.iteritems():
        if len(connectivitylist) == 1:
            Connectivity4patent[patent] = connectivitylist[0]
            continue
        else:
            patentconnectivity = []
            for i in range(len(connectivitylist[0])):
                # try:
                conn = [x for x in zip(*connectivitylist)[i] if x]
                # except TypeError:
                # 	print zip(*connectivitylist)
                # 	print zip(*connectivitylist)[i][0]
                # 	sys.exit()
                conn = None if len(conn) == 0 else sum(conn) / float(len(conn))
                if conn == None:
                    print 'connectivity list do not have same length'
                patentconnectivity.append(conn)
            Connectivity4patent[patent] = patentconnectivity
    logging.info('finished connectivity which has %s patents' %
                 (len(Connectivity4patent)))
    return Connectivity4patent
def connectivityTrend2Patent(Trend, trendConnectivity, patentlist):
    '''given a dictionary trendConnectivity {trend: connectivity},
		output the dictionary for patent connectivity {patent,connectivity}
		handle the case that a patent in different trend'''
    Connectivity4patent = {}  #{patent:connectivity}
    for trend, patentlist in Trend.iteritems():
        connectivity = trendConnectivity[trend]
        for patent in patentlist:
            addintoTrendDic(connectivity, patent, Connectivity4patent)
    miss = 0
    for patent, connectivitylist in Connectivity4patent.iteritems():
        if len(connectivitylist) == 1:
            Connectivity4patent[patent] = connectivitylist[0]
            continue
        elif len(connectivitylist) == 0:
            miss += 1
        else:
            groupFeatures = zip(*connectivitylist)
            patentconnectivity = [
                sum(x) / float(len(x)) for x in groupFeatures
            ]
            Connectivity4patent[patent] = patentconnectivity
    if miss:
        logging.warning(
            '%s patents do not have connectivity feature possible because they do not belong to any trends'
            % miss)
    logging.info('finished connectivity which has %s patents' %
                 (len(Connectivity4patent)))
    return Connectivity4patent
def connectivityTrend2Patent(Trend, trendConnectivity, patentlist):
    """given a dictionary trendConnectivity {trend: connectivity},
		output the dictionary for patent connectivity {patent,connectivity}
		handle the case that a patent in different trend"""
    Connectivity4patent = {}  # {patent:connectivity}
    for trend, patentlist in Trend.iteritems():
        connectivity = trendConnectivity[trend]
        for patent in patentlist:
            addintoTrendDic(connectivity, patent, Connectivity4patent)
    miss = 0
    for patent, connectivitylist in Connectivity4patent.iteritems():
        if len(connectivitylist) == 1:
            Connectivity4patent[patent] = connectivitylist[0]
            continue
        elif len(connectivitylist) == 0:
            miss += 1
        else:
            patentconnectivity = []
            for i in range(len(connectivitylist[0])):
                # try:
                conn = [x for x in zip(*connectivitylist)[i] if x]
                # except TypeError:
                # 	print zip(*connectivitylist)
                # 	print zip(*connectivitylist)[i][0]
                # 	sys.exit()

                if len(conn) == 0:
                    print "connectivity list is zero"

                conn = 0 if len(conn) == 0 else sum(conn) / float(len(conn))
                patentconnectivity.append(conn)
            Connectivity4patent[patent] = patentconnectivity
    logging.warning(
        "%s patents do not have connectivity feature possible because they do not belong to any trends" % miss
    )
    logging.info("finished connectivity which has %s patents" % (len(Connectivity4patent)))
    return Connectivity4patent
def connectivityTrend2Patent(Trend,trendConnectivity,patentlist):
	'''given a dictionary trendConnectivity {trend: connectivity},
		output the dictionary for patent connectivity {patent,connectivity}
		handle the case that a patent in different trend'''
	Connectivity4patent = {} #{patent:connectivity}
	for trend, patentlist in Trend.iteritems():
		connectivity = trendConnectivity[trend]
		for patent in patentlist:
			addintoTrendDic(connectivity,patent,Connectivity4patent)
	miss = 0
	for patent, connectivitylist in Connectivity4patent.iteritems():
		if len(connectivitylist) == 1:
			Connectivity4patent[patent] = connectivitylist[0]
			continue
		elif len(connectivitylist) == 0:
			miss += 1
		else:
			groupFeatures = zip(*connectivitylist)
			patentconnectivity = [sum(x)/float(len(x)) for x in groupFeatures]
			Connectivity4patent[patent] = patentconnectivity
	if miss:
		logging.warning('%s patents do not have connectivity feature possible because they do not belong to any trends'%miss)
	logging.info('finished connectivity which has %s patents'%(len(Connectivity4patent)))
	return Connectivity4patent