You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@ctakes.apache.org by se...@apache.org on 2017/10/24 19:39:31 UTC

svn commit: r1813225 - in /ctakes/trunk/ctakes-core/src/main/java/org/apache/ctakes/core/patient: AbstractPatientConsumer.java PatientNoteCollector.java PatientNoteStore.java PatientViewUtil.java

Author: seanfinan
Date: Tue Oct 24 19:39:30 2017
New Revision: 1813225

URL: http://svn.apache.org/viewvc?rev=1813225&view=rev
Log:
patient store, etc. use total doc count instead of previous/next

Modified:
    ctakes/trunk/ctakes-core/src/main/java/org/apache/ctakes/core/patient/AbstractPatientConsumer.java
    ctakes/trunk/ctakes-core/src/main/java/org/apache/ctakes/core/patient/PatientNoteCollector.java
    ctakes/trunk/ctakes-core/src/main/java/org/apache/ctakes/core/patient/PatientNoteStore.java
    ctakes/trunk/ctakes-core/src/main/java/org/apache/ctakes/core/patient/PatientViewUtil.java

Modified: ctakes/trunk/ctakes-core/src/main/java/org/apache/ctakes/core/patient/AbstractPatientConsumer.java
URL: http://svn.apache.org/viewvc/ctakes/trunk/ctakes-core/src/main/java/org/apache/ctakes/core/patient/AbstractPatientConsumer.java?rev=1813225&r1=1813224&r2=1813225&view=diff
==============================================================================
--- ctakes/trunk/ctakes-core/src/main/java/org/apache/ctakes/core/patient/AbstractPatientConsumer.java (original)
+++ ctakes/trunk/ctakes-core/src/main/java/org/apache/ctakes/core/patient/AbstractPatientConsumer.java Tue Oct 24 19:39:30 2017
@@ -7,6 +7,8 @@ import org.apache.uima.fit.component.JCa
 import org.apache.uima.fit.descriptor.ConfigurationParameter;
 import org.apache.uima.jcas.JCas;
 
+import java.util.Collection;
+
 
 /**
  * Extend this annotator to consume a patient cas once the current patient name has changed.
@@ -29,15 +31,13 @@ abstract public class AbstractPatientCon
    @ConfigurationParameter(
          name = REMOVE_PATIENT,
          description = "The Patient Consumer should remove the patient from the cache when finished.",
-         defaultValue = "false"
+         defaultValue = "true"
    )
    private boolean _removePatient;
 
    private final String _action;
    private final Logger _logger;
 
-   private String _consumerPatient;
-
    protected AbstractPatientConsumer( final String aeName, final String action ) {
       _action = action;
       _logger = Logger.getLogger( aeName );
@@ -47,24 +47,14 @@ abstract public class AbstractPatientCon
     * {@inheritDoc}
     */
    @Override
-   final public void process( final JCas jCas ) throws AnalysisEngineProcessException {
-      final String storePatient = PatientNoteStore.getInstance().getCurrentPatientName();
-      if ( storePatient == null ) {
-         return;
-      }
-      if ( _consumerPatient == null ) {
-         _consumerPatient = storePatient;
-         return;
+   public void process( final JCas jCas ) throws AnalysisEngineProcessException {
+      final Collection<String> completedPatientIds = PatientNoteStore.getInstance().getCompletedPatientIds();
+      for ( String id : completedPatientIds ) {
+         process( id );
+         if ( _removePatient ) {
+            PatientNoteStore.getInstance().removePatient( id );
+         }
       }
-      if ( _consumerPatient.equals( storePatient ) ) {
-         return;
-      }
-      // The storePatient is not the current patient in this consumer, so process the consumer patient.
-      process( _consumerPatient );
-      if ( _removePatient ) {
-         PatientNoteStore.getInstance().removePatientCas( _consumerPatient );
-      }
-      _consumerPatient = storePatient;
    }
 
    /**
@@ -73,7 +63,13 @@ abstract public class AbstractPatientCon
    @Override
    final public void collectionProcessComplete() throws AnalysisEngineProcessException {
       super.collectionProcessComplete();
-      process( _consumerPatient );
+      final Collection<String> allPatientIds = PatientNoteStore.getInstance().getPatientIds();
+      for ( String id : allPatientIds ) {
+         process( id );
+         if ( _removePatient ) {
+            PatientNoteStore.getInstance().removePatient( id );
+         }
+      }
    }
 
    /**

Modified: ctakes/trunk/ctakes-core/src/main/java/org/apache/ctakes/core/patient/PatientNoteCollector.java
URL: http://svn.apache.org/viewvc/ctakes/trunk/ctakes-core/src/main/java/org/apache/ctakes/core/patient/PatientNoteCollector.java?rev=1813225&r1=1813224&r2=1813225&view=diff
==============================================================================
--- ctakes/trunk/ctakes-core/src/main/java/org/apache/ctakes/core/patient/PatientNoteCollector.java (original)
+++ ctakes/trunk/ctakes-core/src/main/java/org/apache/ctakes/core/patient/PatientNoteCollector.java Tue Oct 24 19:39:30 2017
@@ -8,8 +8,8 @@ import org.apache.uima.jcas.JCas;
 
 /**
  * Sends document cas to the {@link PatientNoteStore} to be cached
- * using the {@link PatientNoteStore#getDefaultPatientName(JCas)}
- * and {@link PatientNoteStore#getDefaultDocumentName(JCas)}
+ * using the {@link PatientNoteStore#getDefaultPatientId(JCas)}
+ * and {@link PatientNoteStore#getDefaultDocumentId(JCas)}
  *
  * @author SPF , chip-nlp
  * @version %I%
@@ -31,8 +31,8 @@ final public class PatientNoteCollector
     */
    @Override
    public void process( final JCas jCas ) throws AnalysisEngineProcessException {
-      LOGGER.info( "Caching Document " + PatientNoteStore.getInstance().getDefaultDocumentName( jCas )
-            + " into Patient " + PatientNoteStore.getInstance().getDefaultPatientName( jCas ) + " ..." );
+      LOGGER.info( "Caching Document " + PatientNoteStore.getInstance().getDefaultDocumentId( jCas )
+            + " into Patient " + PatientNoteStore.getInstance().getDefaultPatientId( jCas ) + " ..." );
 
       PatientNoteStore.getInstance().addDocument( jCas );
 

Modified: ctakes/trunk/ctakes-core/src/main/java/org/apache/ctakes/core/patient/PatientNoteStore.java
URL: http://svn.apache.org/viewvc/ctakes/trunk/ctakes-core/src/main/java/org/apache/ctakes/core/patient/PatientNoteStore.java?rev=1813225&r1=1813224&r2=1813225&view=diff
==============================================================================
--- ctakes/trunk/ctakes-core/src/main/java/org/apache/ctakes/core/patient/PatientNoteStore.java (original)
+++ ctakes/trunk/ctakes-core/src/main/java/org/apache/ctakes/core/patient/PatientNoteStore.java Tue Oct 24 19:39:30 2017
@@ -2,6 +2,7 @@ package org.apache.ctakes.core.patient;
 
 
 import org.apache.ctakes.core.util.DocumentIDAnnotationUtil;
+import org.apache.ctakes.core.util.SourceMetadataUtil;
 import org.apache.log4j.Logger;
 import org.apache.uima.UIMAException;
 import org.apache.uima.cas.CASException;
@@ -11,6 +12,7 @@ import org.apache.uima.jcas.JCas;
 import org.apache.uima.util.CasCopier;
 
 import java.util.*;
+import java.util.stream.Collectors;
 
 /**
  * Cache for multi-document patient cas objects
@@ -30,45 +32,76 @@ public enum PatientNoteStore {
    static private final Logger LOGGER = Logger.getLogger( "PatientNoteStore" );
 
    private final Map<String, JCas> _patientMap;
+   private final Map<String, Integer> _wantedDocCounts;
+   private final Map<String, Integer> _storedDocCounts;
    private String _currentPatientName;
    private String _previousPatientName;
 
    PatientNoteStore() {
       _patientMap = new HashMap<>();
+      _wantedDocCounts = new HashMap<>();
+      _storedDocCounts = new HashMap<>();
    }
 
    /**
-    * @return all patient names in the cache
+    * @return all patient identifiers in the cache
     */
-   synchronized public Collection<String> getPatientNames() {
+   synchronized public Collection<String> getPatientIds() {
       return Collections.unmodifiableList( new ArrayList<>( _patientMap.keySet() ) );
    }
 
    /**
-    * @return the name of the most recently stored patient
+    * @return all completed patient identifiers in the cache
     */
-   synchronized public String getCurrentPatientName() {
-      return _currentPatientName;
+   synchronized public Collection<String> getCompletedPatientIds() {
+      return _wantedDocCounts.entrySet().stream()
+            .filter( e -> _storedDocCounts.get( e.getKey() ).equals( e.getValue() ) )
+            .map( Map.Entry::getKey )
+            .collect( Collectors.toList() );
+   }
+
+
+   /**
+    *
+    * @param patientId -
+    * @return number of documents that exist for the patient or -1 if unknown
+    */
+   synchronized public int getDocCount( final String patientId ) {
+      return _wantedDocCounts.getOrDefault( patientId, -1 );
    }
 
    /**
-    * @return the name of the patient stored before the most recently stored patient
+    *
+    * @param patientId -
+    * @param count number of documents that exist for the patient
     */
-   synchronized public String getPreviousPatientName() {
-      return _previousPatientName;
+   synchronized public void setDocCount( final String patientId, final int count ) {
+      _wantedDocCounts.put( patientId, count );
    }
 
    /**
-    * @return the default name for a view of the document.  {@link DocumentIDAnnotationUtil#getDocumentID(JCas)}
+    * @param patientId -
+    * @return number of documents for the patient that have been completed and stored in the cache
     */
-   public String getDefaultDocumentName( final JCas viewCas ) {
+   synchronized public int getCompletedDocCount( final String patientId ) {
+      return _storedDocCounts.getOrDefault( patientId, 0 );
+   }
+
+   /**
+    * @return the default identifier for a view of the document.  {@link DocumentIDAnnotationUtil#getDocumentID(JCas)}
+    */
+   public String getDefaultDocumentId( final JCas viewCas ) {
       return DocumentIDAnnotationUtil.getDocumentID( viewCas );
    }
 
    /**
-    * @return the default name for a view of the document's patient.  {@link DocumentIDAnnotationUtil#getDocumentIdPrefix(JCas)}
+    * @return the default identifier for a view of the document's patient.  {@link DocumentIDAnnotationUtil#getDocumentIdPrefix(JCas)}
     */
-   public String getDefaultPatientName( final JCas viewCas ) {
+   public String getDefaultPatientId( final JCas viewCas ) {
+      final String patientIdentifier = SourceMetadataUtil.getPatientIdentifier( viewCas );
+      if ( patientIdentifier != null && !patientIdentifier.isEmpty() && !patientIdentifier.equals( SourceMetadataUtil.UNKNOWN_PATIENT ) ) {
+         return patientIdentifier;
+      }
       return DocumentIDAnnotationUtil.getDocumentIdPrefix( viewCas );
    }
 
@@ -76,7 +109,7 @@ public enum PatientNoteStore {
     * @param goldCas ye olde containing the gold in the default view
     */
    synchronized public void addGoldView( final JCas goldCas ) {
-      final int goldCount = getGoldViewNames( getDefaultPatientName( goldCas ) ).size();
+      final int goldCount = getGoldViewNames( getDefaultPatientId( goldCas ) ).size();
       addGoldView( "" + (goldCount + 1), goldCas );
    }
 
@@ -92,7 +125,7 @@ public enum PatientNoteStore {
     * @param documentCas ye olde containing the document in the default view
     */
    synchronized public void addDocument( final JCas documentCas ) {
-      addDocument( getDefaultDocumentName( documentCas ), documentCas );
+      addDocument( getDefaultDocumentId( documentCas ), documentCas );
    }
 
    /**
@@ -100,61 +133,64 @@ public enum PatientNoteStore {
     * @param documentCas ye olde containing the document in the default view
     */
    synchronized public void addDocument( final String viewName, final JCas documentCas ) {
-      addDocument( getDefaultPatientName( documentCas ), viewName, documentCas );
+      addDocument( getDefaultPatientId( documentCas ), viewName, documentCas );
    }
 
    /**
-    * @param patientName name to use for the cached patient cas
+    * @param patientId name to use for the cached patient cas
     * @param viewName    name to use for the cached document view
     * @param documentCas ye olde containing the document in the default view
     */
-   synchronized public void addDocument( final String patientName, final String viewName, final JCas documentCas ) {
-      if ( !patientName.equals( _currentPatientName ) ) {
+   synchronized public void addDocument( final String patientId, final String viewName, final JCas documentCas ) {
+      if ( !patientId.equals( _currentPatientName ) ) {
          _previousPatientName = _currentPatientName;
-         _currentPatientName = patientName;
+         _currentPatientName = patientId;
       }
       // don't use putIfAbsent or computeIfAbsent to better handle exceptions and lazy instantiation
-      JCas patientCas = _patientMap.get( patientName );
+      JCas patientCas = _patientMap.get( patientId );
       if ( patientCas == null ) {
          try {
             patientCas = JCasFactory.createJCas();
-            _patientMap.put( patientName, patientCas );
+            _patientMap.put( patientId, patientCas );
          } catch ( UIMAException uE ) {
             LOGGER.error( uE.getMessage() );
             return;
          }
       }
-      LOGGER.info( "Caching " + viewName + " for patient " + patientName + " ..." );
+      LOGGER.info( "Caching " + viewName + " for patient " + patientId + " ..." );
       try {
          final JCas mainView = documentCas.getView( PatientViewUtil.DEFAULT_VIEW );
          final CasCopier copier = new CasCopier( documentCas.getCas(), patientCas.getCas() );
          copier.copyCasView( mainView.getCas(), viewName, true );
+         final int stored = _storedDocCounts.getOrDefault( patientId, 0 );
+         _storedDocCounts.put( patientId, stored + 1 );
       } catch ( CASException | CASRuntimeException casE ) {
          LOGGER.error( casE.getMessage() );
       }
    }
 
    /**
-    * @param patientName name of patient
+    * @param patientId identifier of patient
     * @return cached cas representing patient with documents and gold as views, or null if none
     */
-   synchronized public JCas getPatientCas( final String patientName ) {
-      return _patientMap.get( patientName );
+   synchronized public JCas getPatientCas( final String patientId ) {
+      return _patientMap.get( patientId );
    }
 
    /**
-    * @param patientName name of patient to remove from cache
+    * @param patientId identifier of patient to remove from cache
     */
-   synchronized public void removePatientCas( final String patientName ) {
-      _patientMap.remove( patientName );
+   synchronized public void removePatient( final String patientId ) {
+      _patientMap.remove( patientId );
+      _wantedDocCounts.remove( patientId );
    }
 
    /**
-    * @param patientName name of patient
+    * @param patientId identifier of patient
     * @return All views, including gold and default
     */
-   synchronized public Collection<JCas> getAllViews( final String patientName ) {
-      final JCas patientCas = getPatientCas( patientName );
+   synchronized public Collection<JCas> getAllViews( final String patientId ) {
+      final JCas patientCas = getPatientCas( patientId );
       if ( patientCas == null ) {
          return Collections.emptyList();
       }
@@ -162,11 +198,11 @@ public enum PatientNoteStore {
    }
 
    /**
-    * @param patientName name of patient
+    * @param patientId identifier of patient
     * @return All document views, which are views that are not the default and not gold
     */
-   synchronized public Collection<JCas> getDocumentViews( final String patientName ) {
-      final JCas patientCas = getPatientCas( patientName );
+   synchronized public Collection<JCas> getDocumentViews( final String patientId ) {
+      final JCas patientCas = getPatientCas( patientId );
       if ( patientCas == null ) {
          return Collections.emptyList();
       }
@@ -174,11 +210,11 @@ public enum PatientNoteStore {
    }
 
    /**
-    * @param patientName name of patient
+    * @param patientId identifier of patient
     * @return All gold views, which are views with the prefix {@link PatientViewUtil#GOLD_PREFIX}
     */
-   synchronized public Collection<JCas> getGoldViews( final String patientName ) {
-      final JCas patientCas = getPatientCas( patientName );
+   synchronized public Collection<JCas> getGoldViews( final String patientId ) {
+      final JCas patientCas = getPatientCas( patientId );
       if ( patientCas == null ) {
          return Collections.emptyList();
       }
@@ -186,11 +222,11 @@ public enum PatientNoteStore {
    }
 
    /**
-    * @param patientName name of patient
+    * @param patientId identifier of patient
     * @return Names of all views, including gold and default
     */
-   synchronized public Collection<String> getAllViewNames( final String patientName ) {
-      final JCas patientCas = getPatientCas( patientName );
+   synchronized public Collection<String> getAllViewNames( final String patientId ) {
+      final JCas patientCas = getPatientCas( patientId );
       if ( patientCas == null ) {
          return Collections.emptyList();
       }
@@ -198,11 +234,11 @@ public enum PatientNoteStore {
    }
 
    /**
-    * @param patientName name of patient
+    * @param patientId identifier of patient
     * @return Names of all document views, which are views that are not the default and not gold
     */
-   synchronized public Collection<String> getDocumentViewNames( final String patientName ) {
-      final JCas patientCas = getPatientCas( patientName );
+   synchronized public Collection<String> getDocumentViewNames( final String patientId ) {
+      final JCas patientCas = getPatientCas( patientId );
       if ( patientCas == null ) {
          return Collections.emptyList();
       }
@@ -210,11 +246,11 @@ public enum PatientNoteStore {
    }
 
    /**
-    * @param patientName name of patient
+    * @param patientId identifier of patient
     * @return Names of all gold views, which are views with the prefix {@link PatientViewUtil#GOLD_PREFIX}
     */
-   synchronized public Collection<String> getGoldViewNames( final String patientName ) {
-      final JCas patientCas = getPatientCas( patientName );
+   synchronized public Collection<String> getGoldViewNames( final String patientId ) {
+      final JCas patientCas = getPatientCas( patientId );
       if ( patientCas == null ) {
          return Collections.emptyList();
       }

Modified: ctakes/trunk/ctakes-core/src/main/java/org/apache/ctakes/core/patient/PatientViewUtil.java
URL: http://svn.apache.org/viewvc/ctakes/trunk/ctakes-core/src/main/java/org/apache/ctakes/core/patient/PatientViewUtil.java?rev=1813225&r1=1813224&r2=1813225&view=diff
==============================================================================
--- ctakes/trunk/ctakes-core/src/main/java/org/apache/ctakes/core/patient/PatientViewUtil.java (original)
+++ ctakes/trunk/ctakes-core/src/main/java/org/apache/ctakes/core/patient/PatientViewUtil.java Tue Oct 24 19:39:30 2017
@@ -24,8 +24,8 @@ final public class PatientViewUtil {
 
    static public final String DEFAULT_VIEW = "_InitialView";
    static public final String GOLD_PREFIX = "GoldView";
-   static final Predicate<String> isNameGold = s -> s.startsWith( GOLD_PREFIX );
-   static final Predicate<JCas> isCasGold = c -> c.getViewName().startsWith( GOLD_PREFIX );
+   static private final Predicate<String> isNameGold = s -> s.startsWith( GOLD_PREFIX );
+   static private final Predicate<JCas> isCasGold = c -> c.getViewName().startsWith( GOLD_PREFIX );
 
    static private final Logger LOGGER = Logger.getLogger( "PatientViewUtil" );