Skip to content

Commit

Permalink
refractor(clusters): new functions and architecture
Browse files Browse the repository at this point in the history
  • Loading branch information
ahonestla committed Oct 18, 2023
1 parent 8be6a9d commit d73284c
Show file tree
Hide file tree
Showing 2 changed files with 54 additions and 25 deletions.
46 changes: 26 additions & 20 deletions client/src/layout/ClustersPanel.jsx
Original file line number Diff line number Diff line change
@@ -1,64 +1,70 @@
import '@react-sigma/core/lib/react-sigma.min.css';
import { Container, Title, Text, Col, Row, Badge, BadgeGroup } from '@dataesr/react-dsfr';
import { GetColorName } from 'hex-color-to-color-name';
import {
communityGetUniquePublications,
communityGetTopicsCount,
communityGetTypesCount,
communityGetBestAuthors,
} from '../utils/communityUtils';
import { fillAndSortCommunities } from '../utils/communityUtils';
import { COMMUNTIY_COLORS } from '../styles/colors';

export default function ClustersPanel({ graph, communities, publications }) {
if (!graph.order) return null;

// Fill communities
const filledCommunities = fillAndSortCommunities(communities, publications, { communitiesLimit: 6, authorsLimit: 10, topicsLimit: 5, typesLimit: 3 });
console.log(filledCommunities);

return (
<Container fluid className="fr-my-3w">
<Title as="h3">
{Object.keys(communities).length}
{Object.keys(filledCommunities).length}
{' '}
main clusters
</Title>
<Row gutters>
{Object.keys(communities).map((community) => (
<Col key={community} n="4">
{Object.entries(filledCommunities).map(([key, community]) => (
<Col key={key} n="4">
<div className="fr-card fr-card--shadow">
<p style={{ backgroundColor: COMMUNTIY_COLORS[community], color: '#f6f6f6' }}>
<p style={{ backgroundColor: COMMUNTIY_COLORS[key], color: '#f6f6f6' }}>
&nbsp;&nbsp;Community
{' '}
{GetColorName(COMMUNTIY_COLORS[community])}
{GetColorName(COMMUNTIY_COLORS[key])}
</p>
<div className="fr-card__body">
<Badge className="fr-mb-2w" colorFamily="green-emeraude" text={`${communityGetUniquePublications(communities[community]).length} publications`} />
<Title as="h6">5 main topics</Title>
<BadgeGroup className="fr-mb-2w">
{communityGetTopicsCount(communities[community], publications, 5).map((topic) => (
<Badge type="info" text={`${topic[0]} (${topic[1]})`} />
<Badge colorFamily="green-emeraude" text={`${community.publications.length} publications`} />
<Badge colorFamily="green-bourgeon" text={`${community.nodes.length} authors`} />
</BadgeGroup>
<Title as="h6">
{community.topics.length}
{' '}
main topics
</Title>
<BadgeGroup className="fr-mb-2w">
{community.topics.map(([topic, count]) => (
<Badge type="info" text={`${topic} (${count})`} />
))}
</BadgeGroup>
<Title as="h6">
{Math.min(10, communities[community].length)}
{community.authors.length}
{' '}
main authors
</Title>
<BadgeGroup className="fr-mb-2w">
{communityGetBestAuthors(communities[community], 10).map((node) => (
{community.authors.map((node) => (
<Badge
colorFamily="purple-glycine"
text={`${node.attributes.name} (${node.attributes.weight})`}
/>
))}
</BadgeGroup>
<Title as="h6">
{Math.min(3, communityGetTypesCount(communities[community], publications).length)}
{community.types.length}
{' '}
main types
</Title>
<BadgeGroup className="fr-mb-2w">
{communityGetTypesCount(communities[community], publications, 3).map((type) => (
{community.types.map(([type, count]) => (
<Badge
colorFamily="yellow-tournesol"
text={`${type[0]} (${type[1]})`}
text={`${type} (${count})`}
/>
))}
</BadgeGroup>
Expand Down
33 changes: 28 additions & 5 deletions client/src/utils/communityUtils.js
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
export const communityGetUniquePublications = (community) => (
const communityGetUniquePublications = (community) => (
community.reduce((acc, node) => [...acc, ...node.attributes.publications.flatMap((id) => (!acc.includes(id) ? id : []))], [])
);

export function communityGetTopicsCount(community, publications, limit = 0) {
function communityGetTopicsCount(community, publications, limit = 0) {
const topics = {};

// Count topics from unique publication ids
community.reduce((acc, node) => [...acc, ...node.attributes.publications.flatMap((id) => (!acc.includes(id) ? id : []))], []).forEach((id) => {
publications[id]?.topics.forEach(({ label }) => { topics[label] = topics[label] + 1 || 1; });
publications[id]?.topics?.forEach(({ label }) => { topics[label] = topics[label] + 1 || 1; });
});

const numberOfTopics = Object.keys(topics).length;
Expand All @@ -26,7 +26,7 @@ export function communityGetTopicsCount(community, publications, limit = 0) {
return Object.entries(topTopics);
}

export function communityGetTypesCount(community, publications, limit = 0) {
function communityGetTypesCount(community, publications, limit = 0) {
const types = {};

// Count types from unique publication ids
Expand All @@ -52,8 +52,31 @@ export function communityGetTypesCount(community, publications, limit = 0) {
return Object.entries(topTypes);
}

export function communityGetBestAuthors(community, limit = 0) {
function communityGetBestAuthors(community, limit = 0) {
const endSlice = limit > 0 ? limit : community.length;
// Count and sort coauthors
return community.sort((a, b) => b.attributes.publications.length - a.attributes.publications.length).slice(0, endSlice);
}

export function fillAndSortCommunities(communities, publications, { communitiesLimit = 0, topicsLimit = 0, typesLimit = 0, authorsLimit = 0 }) {
const filledCommunities = {};
const numberOfCommunities = Object.keys(communities).length;
const endSlice = communitiesLimit > 0 ? communitiesLimit : numberOfCommunities;

// Sort communities
const sortedCommunities = Object.entries(communities).sort((a, b) => b[1].length - a[1].length).slice(0, endSlice);

// Fill communities
sortedCommunities.forEach(([key, values]) => {
filledCommunities[key] = {
nodes: values,
size: values.length,
publications: communityGetUniquePublications(values),
topics: communityGetTopicsCount(values, publications, topicsLimit),
types: communityGetTypesCount(values, publications, typesLimit),
authors: communityGetBestAuthors(values, authorsLimit),
};
});

return filledCommunities;
}

0 comments on commit d73284c

Please sign in to comment.